-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor run into functions Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * renamed Apps struct as App Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * redirect logs to files. set informational logs. set command dirs Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * fix linter errors Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * fix e2e test on non-existent resources-path Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * more fixes Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * fix merge conflict Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * set default ports for zero values Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * fix linter errors Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * add more details in error, fix logs in run -f Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * address review comments Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * address review comments Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * add more fixes in template run based on e2e tests Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * add basic happy path e2e for dapr run -f Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * add positive and negative e2e tests for run template Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> * address review comments Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com> Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>
- Loading branch information
1 parent
14c04f9
commit 17dbe5b
Showing
26 changed files
with
694 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2023 The Dapr Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
"os" | ||
"time" | ||
) | ||
|
||
type Metrics struct { | ||
MetricsID int `json:"metricsID"` | ||
} | ||
|
||
func main() { | ||
var host string | ||
var port string | ||
client := http.Client{} | ||
if val, ok := os.LookupEnv("DAPR_HTTP_PORT"); !ok { | ||
log.Fatalf("DAPR_HTTP_PORT not automatically injected") | ||
} else { | ||
log.Println("DAPR_HTTP_PORT set to", val) | ||
port = val | ||
} | ||
// DAPR_HOST_ADD needs to be an env set in dapr.yaml file | ||
if val, ok := os.LookupEnv("DAPR_HOST_ADD"); !ok { | ||
log.Fatalf("DAPR_HOST_ADD not set") | ||
} else { | ||
log.Println("DAPR_HOST_ADD set to", val) | ||
host = val | ||
} | ||
finalURL := "http://" + host + ":" + port + "/metrics" | ||
log.Println("Sending metrics to ", finalURL) | ||
for i := 0; i < 2000; i++ { | ||
time.Sleep(1 * time.Second) | ||
metrics := Metrics{ | ||
MetricsID: i, | ||
} | ||
b, err := json.Marshal(metrics) | ||
if err != nil { | ||
log.Println("Got error while marshalling metrics ", err) | ||
continue | ||
} | ||
// Send metrics to Dapr | ||
req, _ := http.NewRequest(http.MethodPost, finalURL, bytes.NewBuffer(b)) | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("dapr-app-id", "processor") | ||
r, err := client.Do(req) | ||
if err != nil { | ||
log.Println("Got error while sending a request to 'processor' app ", err) | ||
continue | ||
} | ||
defer r.Body.Close() | ||
if r.StatusCode != http.StatusOK { | ||
log.Printf("Error sending metrics with %d to 'processor' app got status code %d\n", i, r.StatusCode) | ||
log.Printf("Status %s \n", r.Status) | ||
continue | ||
} | ||
log.Printf("Metrics with ID %d sent \n", i) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module emit-metrics | ||
|
||
go 1.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
Copyright 2023 The Dapr Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
type handler struct{} | ||
|
||
type Metrics struct { | ||
MetricsID int `json:"metricsID"` | ||
} | ||
|
||
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
fmt.Println("Received request: ", r.Method) | ||
defer r.Body.Close() | ||
var metrics Metrics | ||
err := json.NewDecoder(r.Body).Decode(&metrics) | ||
if err != nil { | ||
fmt.Println("Error decoding body: ", err) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
fmt.Println("Received metrics: ", metrics) | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
func main() { | ||
fmt.Println("Starting server in port 9081...") | ||
StartServer(9081, &handler{}) | ||
} | ||
|
||
// StartServer starts a HTTP or HTTP2 server | ||
func StartServer(port int, handler http.Handler) { | ||
// Create a listener | ||
addr := fmt.Sprintf(":%d", port) | ||
ln, err := net.Listen("tcp", addr) | ||
if err != nil { | ||
log.Fatalf("Failed to create listener: %v", err) | ||
} | ||
//nolint:gosec | ||
server := &http.Server{ | ||
Addr: addr, | ||
Handler: handler, | ||
} | ||
|
||
// Stop the server when we get a termination signal | ||
stopCh := make(chan os.Signal, 1) | ||
signal.Notify(stopCh, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGINT) //nolint:staticcheck | ||
go func() { | ||
// Wait for cancelation signal | ||
<-stopCh | ||
log.Println("Shutdown signal received") | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
server.Shutdown(ctx) | ||
}() | ||
|
||
err = server.Serve(ln) | ||
|
||
if err != http.ErrServerClosed { | ||
log.Fatalf("Failed to run server: %v", err) | ||
} | ||
|
||
log.Println("Server shut down") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module processor | ||
|
||
go 1.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.