From 7105f6898c537b621ca811423183c37071060507 Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Wed, 18 Sep 2024 18:44:39 -0300 Subject: [PATCH 01/10] first approach to SNS, SQS impl in the app --- src/events/go.mod | 7 ++++ src/events/go.sum | 14 +++++++ src/events/notification.go | 14 +++++++ src/events/siteevents.go | 70 +++++++++++++++++++++++++++++++ src/frontend/go.mod | 5 ++- src/frontend/go.sum | 9 ++++ src/frontend/handlers.go | 4 ++ src/frontend/main.go | 8 ++++ src/productcatalogservice/go.mod | 7 +++- src/productcatalogservice/go.sum | 7 ++++ src/productcatalogservice/main.go | 22 ++++++++++ 11 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 src/events/go.mod create mode 100644 src/events/go.sum create mode 100644 src/events/notification.go create mode 100644 src/events/siteevents.go diff --git a/src/events/go.mod b/src/events/go.mod new file mode 100644 index 0000000..e200f94 --- /dev/null +++ b/src/events/go.mod @@ -0,0 +1,7 @@ +module github.com/kurtosis-tech/new-obd/src/events + +go 1.19 + +require github.com/aws/aws-sdk-go v1.55.5 + +require github.com/jmespath/go-jmespath v0.4.0 // indirect diff --git a/src/events/go.sum b/src/events/go.sum new file mode 100644 index 0000000..d323266 --- /dev/null +++ b/src/events/go.sum @@ -0,0 +1,14 @@ +github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU= +github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/src/events/notification.go b/src/events/notification.go new file mode 100644 index 0000000..6fa0bd2 --- /dev/null +++ b/src/events/notification.go @@ -0,0 +1,14 @@ +package events + +// SNSNotification represents the structure of an SNS notification. +type SNSNotification struct { + Type string `json:"Type"` + MessageId string `json:"MessageId"` + TopicArn string `json:"TopicArn"` + Message string `json:"Message"` + Timestamp string `json:"Timestamp"` + SignatureVersion string `json:"SignatureVersion"` + Signature string `json:"Signature"` + SigningCertURL string `json:"SigningCertURL"` + UnsubscribeURL string `json:"UnsubscribeURL"` +} diff --git a/src/events/siteevents.go b/src/events/siteevents.go new file mode 100644 index 0000000..44c2f86 --- /dev/null +++ b/src/events/siteevents.go @@ -0,0 +1,70 @@ +package events + +import ( + "encoding/json" + "errors" + "fmt" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/sns" + "github.com/aws/aws-sdk-go/service/sqs" + "time" +) + +type EventsManager struct { + snsTopicARN string + queueUrl string +} + +func NewPageVisitsEventsManager(snsTopicARN string, queueUrl string) *EventsManager { + return &EventsManager{snsTopicARN: snsTopicARN, queueUrl: queueUrl} +} + +func (manager *EventsManager) PublishMessage(message string) error { + sess := session.Must(session.NewSession()) + svc := sns.New(sess) + + _, err := svc.Publish(&sns.PublishInput{ + Message: aws.String(message), + TopicArn: aws.String(manager.snsTopicARN), + }) + return err +} + +func (manager *EventsManager) ReceiveMessages(msgChan chan<- string, errChan chan<- error) { + sess := session.Must(session.NewSession()) + svc := sqs.New(sess) + + for { + result, err := svc.ReceiveMessage(&sqs.ReceiveMessageInput{ + QueueUrl: aws.String(manager.queueUrl), + MaxNumberOfMessages: aws.Int64(1), + WaitTimeSeconds: aws.Int64(10), // Long polling + }) + if err != nil { + errChan <- errors.New(fmt.Sprintf("error receiving message: %s", err)) + } + + for _, message := range result.Messages { + + var notification SNSNotification + err = json.Unmarshal([]byte(*message.Body), ¬ification) + if err != nil { + errChan <- errors.New(fmt.Sprintf("Error unmarshalling JSON: %v\n", err)) + } + + msgChan <- notification.Message + // Delete the message after processing + _, err = svc.DeleteMessage(&sqs.DeleteMessageInput{ + QueueUrl: aws.String(manager.queueUrl), + ReceiptHandle: message.ReceiptHandle, + }) + if err != nil { + errChan <- errors.New(fmt.Sprintf("error deleting message: %s", err)) + } + } + + time.Sleep(1 * time.Second) // Prevent tight loop + } + +} diff --git a/src/frontend/go.mod b/src/frontend/go.mod index d020422..0942eb4 100644 --- a/src/frontend/go.mod +++ b/src/frontend/go.mod @@ -7,8 +7,8 @@ toolchain go1.22.4 replace ( github.com/kurtosis-tech/new-obd/src/cartservice => ../cartservice github.com/kurtosis-tech/new-obd/src/currencyexternalapi => ../currencyexternalapi + github.com/kurtosis-tech/new-obd/src/events => ../events github.com/kurtosis-tech/new-obd/src/productcatalogservice => ../productcatalogservice - ) require ( @@ -16,6 +16,7 @@ require ( github.com/gorilla/mux v1.8.1 github.com/kurtosis-tech/new-obd/src/cartservice v0.0.0 github.com/kurtosis-tech/new-obd/src/currencyexternalapi v0.0.0 + github.com/kurtosis-tech/new-obd/src/events v0.0.0-00010101000000-000000000000 github.com/kurtosis-tech/new-obd/src/productcatalogservice v0.0.0 github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.8.1 @@ -23,8 +24,10 @@ require ( require ( github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect + github.com/aws/aws-sdk-go v1.55.5 // indirect github.com/golang/protobuf v1.5.0 // indirect github.com/google/go-cmp v0.5.8 // indirect + github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/oapi-codegen/runtime v1.1.1 // indirect golang.org/x/sys v0.20.0 // indirect google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect diff --git a/src/frontend/go.sum b/src/frontend/go.sum index e410746..278f698 100644 --- a/src/frontend/go.sum +++ b/src/frontend/go.sum @@ -5,6 +5,8 @@ github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMz github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ= github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk= +github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU= +github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= @@ -54,6 +56,10 @@ github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro= github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg= @@ -141,6 +147,9 @@ google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/frontend/handlers.go b/src/frontend/handlers.go index 24e25fb..dc94f3e 100644 --- a/src/frontend/handlers.go +++ b/src/frontend/handlers.go @@ -54,6 +54,10 @@ const ( ) func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) { + + eventMsg := fmt.Sprintf("User: %s Visiting the Home page", userID) + fe.eventsManager.PublishMessage(eventMsg) + currencies, err := fe.currencyService.GetSupportedCurrencies(r.Context()) if err != nil { renderHTTPError(r, w, errors.Wrapf(err, "error retrieving currencies"), http.StatusInternalServerError) diff --git a/src/frontend/main.go b/src/frontend/main.go index cb83253..24c5678 100644 --- a/src/frontend/main.go +++ b/src/frontend/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/kurtosis-tech/new-obd/src/events" "net/http" "os" "time" @@ -40,6 +41,7 @@ type frontendServer struct { cartService *cartservice_rest_client.ClientWithResponses productCatalogService *productcatalogservice_rest_client.ClientWithResponses currencyService *currencyexternalservice.CurrencyExternalService + eventsManager *events.EventsManager } func main() { @@ -73,10 +75,16 @@ func main() { apiKey := os.Getenv("JSDELIVRAPIKEY") + snsTopicARN := os.Getenv("SNS_TOPIC_ARN") + queueUrl := os.Getenv("QUEUE_URL") + + eventsManager := events.NewPageVisitsEventsManager(snsTopicARN, queueUrl) + svc := &frontendServer{ cartService: cartServiceClient, productCatalogService: productCatalogServiceClient, currencyService: currencyexternalservice.CreateService(apiKey), + eventsManager: eventsManager, } r := mux.NewRouter() diff --git a/src/productcatalogservice/go.mod b/src/productcatalogservice/go.mod index 312fcb0..92c15d9 100644 --- a/src/productcatalogservice/go.mod +++ b/src/productcatalogservice/go.mod @@ -1,10 +1,13 @@ module github.com/kurtosis-tech/new-obd/src/productcatalogservice -go 1.21 +go 1.21.9 + +replace github.com/kurtosis-tech/new-obd/src/events => ../events require ( github.com/deepmap/oapi-codegen/v2 v2.2.1-0.20240604070534-2f0ff757704b github.com/getkin/kin-openapi v0.124.0 + github.com/kurtosis-tech/new-obd/src/events v0.0.0-00010101000000-000000000000 github.com/labstack/echo/v4 v4.12.0 github.com/oapi-codegen/runtime v1.1.1 github.com/sirupsen/logrus v1.8.1 @@ -12,11 +15,13 @@ require ( require ( github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect + github.com/aws/aws-sdk-go v1.55.5 // indirect github.com/go-openapi/jsonpointer v0.20.2 // indirect github.com/go-openapi/swag v0.22.8 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/google/uuid v1.5.0 // indirect github.com/invopop/yaml v0.2.0 // indirect + github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/labstack/gommon v0.4.2 // indirect github.com/mailru/easyjson v0.7.7 // indirect diff --git a/src/productcatalogservice/go.sum b/src/productcatalogservice/go.sum index c99bdc2..9dc6405 100644 --- a/src/productcatalogservice/go.sum +++ b/src/productcatalogservice/go.sum @@ -1,6 +1,8 @@ github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ= github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk= +github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU= +github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -21,6 +23,10 @@ github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY= github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= @@ -85,6 +91,7 @@ golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/productcatalogservice/main.go b/src/productcatalogservice/main.go index 6cc2808..d5ab002 100644 --- a/src/productcatalogservice/main.go +++ b/src/productcatalogservice/main.go @@ -2,11 +2,13 @@ package main import ( "fmt" + "github.com/kurtosis-tech/new-obd/src/events" productcatalogservice_server_rest_server "github.com/kurtosis-tech/new-obd/src/productcatalogservice/api/http_rest/server" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "github.com/sirupsen/logrus" "net" + "os" ) const ( @@ -38,5 +40,25 @@ func main() { productcatalogservice_server_rest_server.RegisterHandlers(echoRouter, productcatalogservice_server_rest_server.NewStrictHandler(server, nil)) + snsTopicARN := os.Getenv("SNS_TOPIC_ARN") + queueUrl := os.Getenv("QUEUE_URL") + + eventsManager := events.NewPageVisitsEventsManager(snsTopicARN, queueUrl) + + msgChan := make(chan string) + errorChan := make(chan error) + go eventsManager.ReceiveMessages(msgChan, errorChan) + //receive msgs and errors + go func() { + for { + select { + case err := <-errorChan: + logrus.Error(err) + case msg := <-msgChan: + logrus.Info(msg) + } + } + }() + echoRouter.Start(net.JoinHostPort(restAPIHostIP, fmt.Sprint(restAPIPortAddr))) } From e00a1d3b73765dcba50144de05627941284158e4 Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Fri, 20 Sep 2024 18:40:23 -0300 Subject: [PATCH 02/10] implementing the metrics service --- .../Dockerfile => cartservice.dockerfile} | 12 ++-- src/cartservice/.dockerignore | 1 - src/frontend.dockerfile | 2 +- .../currencyexternalservice/service.go | 2 +- .../currencyexternalservice/servicefactory.go | 4 +- src/frontend/go.mod | 13 ++-- src/frontend/go.sum | 9 +-- src/frontend/handlers.go | 30 +++----- src/frontend/main.go | 38 ++++------- src/frontend/middleware.go | 5 ++ .../currencyexternalapi/cache.go | 0 .../config/currencyapiconfig.go | 0 .../freecurrency/freecurrencyapiconfig.go | 2 +- .../config/ghgist/ghgistapiconfig.go | 2 +- .../config/jsdelivr/jsdelivr.go | 2 +- .../currencyexternalapi/currencyapi.go | 2 +- .../currencyexternalapi/currencyapi_test.go | 6 +- .../currencyexternalapi/default.nix | 0 .../currencyexternalapi/go.mod | 2 +- .../currencyexternalapi/go.sum | 0 .../currencyexternalapi/gomod2nix.toml | 0 .../events/eventsmanager.go} | 26 ++++++- src/{ => libraries}/events/go.mod | 2 +- src/{ => libraries}/events/go.sum | 0 src/libraries/events/middleware.go | 44 ++++++++++++ src/{ => libraries}/events/notification.go | 0 .../consts => libraries/tracing}/consts.go | 2 +- src/libraries/tracing/go.mod | 7 ++ src/libraries/tracing/go.sum | 15 ++++ .../tracing}/tracing.go | 5 +- src/metrics.dockerfile | 32 +++++++++ src/metrics/.dockerignore | 4 ++ src/metrics/.gitignore | 21 ++++++ src/metrics/Makefile | 21 ++++++ src/metrics/go.mod | 22 ++++++ src/metrics/go.sum | 28 ++++++++ src/metrics/handlers.go | 64 ++++++++++++++++++ src/metrics/main.go | 57 ++++++++++++++++ src/metrics/static/favicon.ico | Bin 0 -> 16958 bytes src/metrics/static/styles/styles.css | 59 ++++++++++++++++ src/metrics/templates/home.html | 36 ++++++++++ ...rfile => productcatalogservice.dockerfile} | 16 ++--- src/productcatalogservice/go.mod | 5 -- src/productcatalogservice/main.go | 22 ------ 44 files changed, 502 insertions(+), 118 deletions(-) rename src/{cartservice/Dockerfile => cartservice.dockerfile} (68%) rename src/{ => libraries}/currencyexternalapi/cache.go (100%) rename src/{ => libraries}/currencyexternalapi/config/currencyapiconfig.go (100%) rename src/{ => libraries}/currencyexternalapi/config/freecurrency/freecurrencyapiconfig.go (97%) rename src/{ => libraries}/currencyexternalapi/config/ghgist/ghgistapiconfig.go (96%) rename src/{ => libraries}/currencyexternalapi/config/jsdelivr/jsdelivr.go (97%) rename src/{ => libraries}/currencyexternalapi/currencyapi.go (97%) rename src/{ => libraries}/currencyexternalapi/currencyapi_test.go (82%) rename src/{ => libraries}/currencyexternalapi/default.nix (100%) rename src/{ => libraries}/currencyexternalapi/go.mod (87%) rename src/{ => libraries}/currencyexternalapi/go.sum (100%) rename src/{ => libraries}/currencyexternalapi/gomod2nix.toml (100%) rename src/{events/siteevents.go => libraries/events/eventsmanager.go} (65%) rename src/{ => libraries}/events/go.mod (64%) rename src/{ => libraries}/events/go.sum (100%) create mode 100644 src/libraries/events/middleware.go rename src/{ => libraries}/events/notification.go (100%) rename src/{frontend/consts => libraries/tracing}/consts.go (79%) create mode 100644 src/libraries/tracing/go.mod create mode 100644 src/libraries/tracing/go.sum rename src/{frontend => libraries/tracing}/tracing.go (71%) create mode 100644 src/metrics.dockerfile create mode 100644 src/metrics/.dockerignore create mode 100644 src/metrics/.gitignore create mode 100644 src/metrics/Makefile create mode 100644 src/metrics/go.mod create mode 100644 src/metrics/go.sum create mode 100644 src/metrics/handlers.go create mode 100644 src/metrics/main.go create mode 100755 src/metrics/static/favicon.ico create mode 100644 src/metrics/static/styles/styles.css create mode 100644 src/metrics/templates/home.html rename src/{productcatalogservice/Dockerfile => productcatalogservice.dockerfile} (58%) diff --git a/src/cartservice/Dockerfile b/src/cartservice.dockerfile similarity index 68% rename from src/cartservice/Dockerfile rename to src/cartservice.dockerfile index 2e5c159..68fb3b7 100644 --- a/src/cartservice/Dockerfile +++ b/src/cartservice.dockerfile @@ -5,20 +5,18 @@ ENV CGO_ENABLED=0 GOOS=linux WORKDIR /go/src/hipstershop # Build Go binary -COPY Makefile go.mod go.sum ./ +COPY ./cartservice ./cartservice +WORKDIR /go/src/hipstershop/cartservice RUN go env -w GOPROXY=https://goproxy.io,direct/ RUN go mod download -COPY . . -# Skaffold passes in debug-oriented compiler flags -ARG SKAFFOLD_GO_GCFLAGS -RUN go build -gcflags="${SKAFFOLD_GO_GCFLAGS}" -o /go/src/hipstershop/cartservice . +RUN go build -o /go/src/hipstershop/cartservicebin . # # Deployment container FROM alpine:latest # Install dependencies -RUN apk --update --no-cache add ca-certificates make protoc +RUN apk --update --no-cache add ca-certificates protoc # These tools are for debuggin the containres RUN apk add --no-cache \ @@ -35,6 +33,6 @@ RUN apk add --no-cache \ ENV GOTRACEBACK=single COPY --from=builder /etc/ssl/certs /etc/ssl/certs -COPY --from=builder /go/src/hipstershop/cartservice /hipstershop/cartservice +COPY --from=builder /go/src/hipstershop/cartservicebin /hipstershop/cartservice ENTRYPOINT ["/hipstershop/cartservice"] diff --git a/src/cartservice/.dockerignore b/src/cartservice/.dockerignore index 89b7e45..a1046de 100644 --- a/src/cartservice/.dockerignore +++ b/src/cartservice/.dockerignore @@ -1,4 +1,3 @@ .gitignore -Dockerfile default.nix gomod2nix.toml diff --git a/src/frontend.dockerfile b/src/frontend.dockerfile index 54d1f78..b83505b 100644 --- a/src/frontend.dockerfile +++ b/src/frontend.dockerfile @@ -10,8 +10,8 @@ RUN apk --update --no-cache add ca-certificates make # Build Go binary COPY ./frontend ./frontend COPY ./cartservice ./cartservice -COPY ./currencyexternalapi ./currencyexternalapi COPY ./productcatalogservice ./productcatalogservice +COPY ./libraries ./libraries WORKDIR /go/src/hipstershop/frontend RUN go env -w GOPROXY=https://goproxy.io,direct/ diff --git a/src/frontend/currencyexternalservice/service.go b/src/frontend/currencyexternalservice/service.go index 5b89a0c..151cdc7 100644 --- a/src/frontend/currencyexternalservice/service.go +++ b/src/frontend/currencyexternalservice/service.go @@ -2,7 +2,7 @@ package currencyexternalservice import ( "context" - "github.com/kurtosis-tech/new-obd/src/currencyexternalapi" + "github.com/kurtosis-tech/new-obd/src/libraries/currencyexternalapi" productcatalogservice_rest_types "github.com/kurtosis-tech/new-obd/src/productcatalogservice/api/http_rest/types" ) diff --git a/src/frontend/currencyexternalservice/servicefactory.go b/src/frontend/currencyexternalservice/servicefactory.go index 940f0f9..f52e652 100644 --- a/src/frontend/currencyexternalservice/servicefactory.go +++ b/src/frontend/currencyexternalservice/servicefactory.go @@ -1,8 +1,8 @@ package currencyexternalservice import ( - "github.com/kurtosis-tech/new-obd/src/currencyexternalapi" - "github.com/kurtosis-tech/new-obd/src/currencyexternalapi/config/jsdelivr" + "github.com/kurtosis-tech/new-obd/src/libraries/currencyexternalapi" + "github.com/kurtosis-tech/new-obd/src/libraries/currencyexternalapi/config/jsdelivr" ) func CreateService(apiKey string) *CurrencyExternalService { diff --git a/src/frontend/go.mod b/src/frontend/go.mod index 0942eb4..a16bece 100644 --- a/src/frontend/go.mod +++ b/src/frontend/go.mod @@ -6,8 +6,9 @@ toolchain go1.22.4 replace ( github.com/kurtosis-tech/new-obd/src/cartservice => ../cartservice - github.com/kurtosis-tech/new-obd/src/currencyexternalapi => ../currencyexternalapi - github.com/kurtosis-tech/new-obd/src/events => ../events + github.com/kurtosis-tech/new-obd/src/libraries/currencyexternalapi => ./../libraries/currencyexternalapi + github.com/kurtosis-tech/new-obd/src/libraries/events => ./../libraries/events + github.com/kurtosis-tech/new-obd/src/libraries/tracing => ./../libraries/tracing github.com/kurtosis-tech/new-obd/src/productcatalogservice => ../productcatalogservice ) @@ -15,11 +16,13 @@ require ( github.com/google/uuid v1.5.0 github.com/gorilla/mux v1.8.1 github.com/kurtosis-tech/new-obd/src/cartservice v0.0.0 - github.com/kurtosis-tech/new-obd/src/currencyexternalapi v0.0.0 - github.com/kurtosis-tech/new-obd/src/events v0.0.0-00010101000000-000000000000 + github.com/kurtosis-tech/new-obd/src/currencyexternalapi v0.0.0-20240905173058-8d378bef698b + github.com/kurtosis-tech/new-obd/src/libraries/currencyexternalapi v0.0.0 + github.com/kurtosis-tech/new-obd/src/libraries/events v0.0.0 + github.com/kurtosis-tech/new-obd/src/libraries/tracing v0.0.0 github.com/kurtosis-tech/new-obd/src/productcatalogservice v0.0.0 github.com/pkg/errors v0.9.1 - github.com/sirupsen/logrus v1.8.1 + github.com/sirupsen/logrus v1.9.3 ) require ( diff --git a/src/frontend/go.sum b/src/frontend/go.sum index 278f698..6cbd217 100644 --- a/src/frontend/go.sum +++ b/src/frontend/go.sum @@ -61,6 +61,8 @@ github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHW github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= +github.com/kurtosis-tech/new-obd/src/currencyexternalapi v0.0.0-20240905173058-8d378bef698b h1:zGP1/QT+Rxq9+U6y3XaciZh/7gsmbr2lY6nYbhxL3Vc= +github.com/kurtosis-tech/new-obd/src/currencyexternalapi v0.0.0-20240905173058-8d378bef698b/go.mod h1:RofAgi1WBxLMEGQvNe0grFm0NSOFtHaeWtccTWh7Fss= github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro= github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -69,11 +71,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= -github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= -github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -104,8 +105,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/src/frontend/handlers.go b/src/frontend/handlers.go index dc94f3e..d7410aa 100644 --- a/src/frontend/handlers.go +++ b/src/frontend/handlers.go @@ -17,6 +17,7 @@ package main import ( "context" "fmt" + "github.com/kurtosis-tech/new-obd/src/libraries/tracing" "html/template" "log" "net/http" @@ -27,7 +28,6 @@ import ( "github.com/gorilla/mux" cartservice_rest_types "github.com/kurtosis-tech/new-obd/src/cartservice/api/http_rest/types" - "github.com/kurtosis-tech/new-obd/src/frontend/consts" "github.com/kurtosis-tech/new-obd/src/frontend/money" productcatalogservice_rest_types "github.com/kurtosis-tech/new-obd/src/productcatalogservice/api/http_rest/types" "github.com/pkg/errors" @@ -49,15 +49,8 @@ var ( plat platformDetails ) -const ( - userID = "0494c5e0-dde0-48fa-a6d8-f7962f5476bf" -) - func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) { - eventMsg := fmt.Sprintf("User: %s Visiting the Home page", userID) - fe.eventsManager.PublishMessage(eventMsg) - currencies, err := fe.currencyService.GetSupportedCurrencies(r.Context()) if err != nil { renderHTTPError(r, w, errors.Wrapf(err, "error retrieving currencies"), http.StatusInternalServerError) @@ -103,15 +96,14 @@ func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) { } if err := templates.ExecuteTemplate(w, "home", map[string]interface{}{ - "session_id": sessionID(r), - "request_id": r.Context().Value(ctxKeyRequestID{}), - "user_currency": currentCurrency(r), - "show_currency": true, - "currencies": currencies, - "products": ps, - "cart_size": cartSize(*cart.Items), - "banner_color": os.Getenv("BANNER_COLOR"), // illustrates canary deployments - //"ad": fe.chooseAd(r.Context(), []string{}, log), //TODO fix + "session_id": sessionID(r), + "request_id": r.Context().Value(ctxKeyRequestID{}), + "user_currency": currentCurrency(r), + "show_currency": true, + "currencies": currencies, + "products": ps, + "cart_size": cartSize(*cart.Items), + "banner_color": os.Getenv("BANNER_COLOR"), // illustrates canary deployments "platform_css": plat.css, "platform_name": plat.provider, "is_cymbal_brand": isCymbalBrand, @@ -436,10 +428,10 @@ func cartSize(c []cartservice_rest_types.CartItem) int { } func getSetTraceIdHeaderRequestEditorFcn(upsTreamRequest *http.Request) func(ctx context.Context, req *http.Request) error { - traceID := upsTreamRequest.Header.Get(consts.KardinalTraceIdHeaderKey) + traceID := upsTreamRequest.Header.Get(tracing.KardinalTraceIdHeaderKey) setKardinalReqEditorFcn := func(ctx context.Context, req *http.Request) error { - req.Header.Set(consts.KardinalTraceIdHeaderKey, traceID) + req.Header.Set(tracing.KardinalTraceIdHeaderKey, traceID) return nil } diff --git a/src/frontend/main.go b/src/frontend/main.go index 24c5678..d3a5a0a 100644 --- a/src/frontend/main.go +++ b/src/frontend/main.go @@ -2,7 +2,8 @@ package main import ( "fmt" - "github.com/kurtosis-tech/new-obd/src/events" + "github.com/kurtosis-tech/new-obd/src/libraries/events" + "github.com/kurtosis-tech/new-obd/src/libraries/tracing" "net/http" "os" "time" @@ -24,16 +25,9 @@ const ( cookiePrefix = "shop_" cookieSessionID = cookiePrefix + "session-id" cookieCurrency = cookiePrefix + "currency" -) -var whitelistedCurrencies = map[string]bool{ - "USD": true, - "EUR": true, - "CAD": true, - "JPY": true, - "GBP": true, - "TRY": true, -} + userID = "0494c5e0-dde0-48fa-a6d8-f7962f5476bf" +) type ctxKeySessionID struct{} @@ -41,7 +35,6 @@ type frontendServer struct { cartService *cartservice_rest_client.ClientWithResponses productCatalogService *productcatalogservice_rest_client.ClientWithResponses currencyService *currencyexternalservice.CurrencyExternalService - eventsManager *events.EventsManager } func main() { @@ -75,16 +68,10 @@ func main() { apiKey := os.Getenv("JSDELIVRAPIKEY") - snsTopicARN := os.Getenv("SNS_TOPIC_ARN") - queueUrl := os.Getenv("QUEUE_URL") - - eventsManager := events.NewPageVisitsEventsManager(snsTopicARN, queueUrl) - svc := &frontendServer{ cartService: cartServiceClient, productCatalogService: productCatalogServiceClient, currencyService: currencyexternalservice.CreateService(apiKey), - eventsManager: eventsManager, } r := mux.NewRouter() @@ -98,18 +85,17 @@ func main() { r.HandleFunc("/robots.txt", func(w http.ResponseWriter, _ *http.Request) { fmt.Fprint(w, "User-agent: *\nDisallow: /") }) r.HandleFunc("/_healthz", func(w http.ResponseWriter, _ *http.Request) { fmt.Fprint(w, "ok") }) - //TODO - /* - r.HandleFunc("/logout", svc.logoutHandler).Methods(http.MethodGet) - r.HandleFunc("/cart/checkout", svc.placeOrderHandler).Methods(http.MethodPost) - */ - var handler http.Handler = r handler = &logHandler{log: log, next: handler} // add logging handler = ensureSessionID(handler) // add session ID - // handler = tracing(handler) // add opentelemetry instrumentation - // r.Use(otelmux.Middleware(name)) - r.Use(KardinalTracingContextWrapper) + r.Use(tracing.KardinalTracingContextWrapper) + + // add the events manage middleware + eventsManager, err := events.CreateEventsManager() + if err != nil { + logrus.Errorf("An error occurred initializing events manager! No site events will be tracked due this error.\nError was: %s", err) + } + r.Use(events.GetWrapsWithEventsManagerMiddleware(eventsManager)) // Start the server http.Handle("/", r) diff --git a/src/frontend/middleware.go b/src/frontend/middleware.go index 4d4221a..eb184c3 100644 --- a/src/frontend/middleware.go +++ b/src/frontend/middleware.go @@ -23,6 +23,10 @@ import ( "github.com/sirupsen/logrus" ) +const ( + ctxKeyUserID = "user-id" +) + type ctxKeyLog struct{} type ctxKeyRequestID struct{} @@ -57,6 +61,7 @@ func (lh *logHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx := r.Context() requestID, _ := uuid.NewRandom() ctx = context.WithValue(ctx, ctxKeyRequestID{}, requestID.String()) + ctx = context.WithValue(ctx, ctxKeyUserID, userID) start := time.Now() rr := &responseRecorder{w: w} diff --git a/src/currencyexternalapi/cache.go b/src/libraries/currencyexternalapi/cache.go similarity index 100% rename from src/currencyexternalapi/cache.go rename to src/libraries/currencyexternalapi/cache.go diff --git a/src/currencyexternalapi/config/currencyapiconfig.go b/src/libraries/currencyexternalapi/config/currencyapiconfig.go similarity index 100% rename from src/currencyexternalapi/config/currencyapiconfig.go rename to src/libraries/currencyexternalapi/config/currencyapiconfig.go diff --git a/src/currencyexternalapi/config/freecurrency/freecurrencyapiconfig.go b/src/libraries/currencyexternalapi/config/freecurrency/freecurrencyapiconfig.go similarity index 97% rename from src/currencyexternalapi/config/freecurrency/freecurrencyapiconfig.go rename to src/libraries/currencyexternalapi/config/freecurrency/freecurrencyapiconfig.go index 068c056..a08ba44 100644 --- a/src/currencyexternalapi/config/freecurrency/freecurrencyapiconfig.go +++ b/src/libraries/currencyexternalapi/config/freecurrency/freecurrencyapiconfig.go @@ -3,7 +3,7 @@ package freecurrency import ( "encoding/json" "fmt" - "github.com/kurtosis-tech/new-obd/src/currencyexternalapi/config" + "github.com/kurtosis-tech/new-obd/src/libraries/currencyexternalapi/config" "net/url" "strings" "time" diff --git a/src/currencyexternalapi/config/ghgist/ghgistapiconfig.go b/src/libraries/currencyexternalapi/config/ghgist/ghgistapiconfig.go similarity index 96% rename from src/currencyexternalapi/config/ghgist/ghgistapiconfig.go rename to src/libraries/currencyexternalapi/config/ghgist/ghgistapiconfig.go index 1585cbc..e3fe7ff 100644 --- a/src/currencyexternalapi/config/ghgist/ghgistapiconfig.go +++ b/src/libraries/currencyexternalapi/config/ghgist/ghgistapiconfig.go @@ -3,7 +3,7 @@ package ghgist import ( "encoding/json" "fmt" - "github.com/kurtosis-tech/new-obd/src/currencyexternalapi/config" + "github.com/kurtosis-tech/new-obd/src/libraries/currencyexternalapi/config" "net/url" "time" ) diff --git a/src/currencyexternalapi/config/jsdelivr/jsdelivr.go b/src/libraries/currencyexternalapi/config/jsdelivr/jsdelivr.go similarity index 97% rename from src/currencyexternalapi/config/jsdelivr/jsdelivr.go rename to src/libraries/currencyexternalapi/config/jsdelivr/jsdelivr.go index 49db42f..517d003 100644 --- a/src/currencyexternalapi/config/jsdelivr/jsdelivr.go +++ b/src/libraries/currencyexternalapi/config/jsdelivr/jsdelivr.go @@ -3,7 +3,7 @@ package jsdelivr import ( "encoding/json" "fmt" - "github.com/kurtosis-tech/new-obd/src/currencyexternalapi/config" + "github.com/kurtosis-tech/new-obd/src/libraries/currencyexternalapi/config" "net/url" "strings" "time" diff --git a/src/currencyexternalapi/currencyapi.go b/src/libraries/currencyexternalapi/currencyapi.go similarity index 97% rename from src/currencyexternalapi/currencyapi.go rename to src/libraries/currencyexternalapi/currencyapi.go index 8ce8b32..e1ab7f4 100644 --- a/src/currencyexternalapi/currencyapi.go +++ b/src/libraries/currencyexternalapi/currencyapi.go @@ -2,7 +2,7 @@ package currencyexternalapi import ( "context" - "github.com/kurtosis-tech/new-obd/src/currencyexternalapi/config" + "github.com/kurtosis-tech/new-obd/src/libraries/currencyexternalapi/config" "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" diff --git a/src/currencyexternalapi/currencyapi_test.go b/src/libraries/currencyexternalapi/currencyapi_test.go similarity index 82% rename from src/currencyexternalapi/currencyapi_test.go rename to src/libraries/currencyexternalapi/currencyapi_test.go index 7e0acaf..3bb2070 100644 --- a/src/currencyexternalapi/currencyapi_test.go +++ b/src/libraries/currencyexternalapi/currencyapi_test.go @@ -2,8 +2,8 @@ package currencyexternalapi import ( "context" - "github.com/kurtosis-tech/new-obd/src/currencyexternalapi/config/ghgist" - "github.com/kurtosis-tech/new-obd/src/currencyexternalapi/config/jsdelivr" + "github.com/kurtosis-tech/new-obd/src/libraries/currencyexternalapi/config/ghgist" + "github.com/kurtosis-tech/new-obd/src/libraries/currencyexternalapi/config/jsdelivr" "github.com/stretchr/testify/require" "testing" ) @@ -28,7 +28,7 @@ func Test(t *testing.T) { } func Test2(t *testing.T) { - currencyAPI := NewCurrencyAPI(jsdelivr.JsdelivrAPIConfig) + currencyAPI := NewCurrencyAPI(jsdelivr.GetJsdelivrAPIConfig("apiKey")) supported, err := currencyAPI.GetSupportedCurrencies(context.Background()) require.NoError(t, err) diff --git a/src/currencyexternalapi/default.nix b/src/libraries/currencyexternalapi/default.nix similarity index 100% rename from src/currencyexternalapi/default.nix rename to src/libraries/currencyexternalapi/default.nix diff --git a/src/currencyexternalapi/go.mod b/src/libraries/currencyexternalapi/go.mod similarity index 87% rename from src/currencyexternalapi/go.mod rename to src/libraries/currencyexternalapi/go.mod index 29670fb..4bd6705 100644 --- a/src/currencyexternalapi/go.mod +++ b/src/libraries/currencyexternalapi/go.mod @@ -1,4 +1,4 @@ -module github.com/kurtosis-tech/new-obd/src/currencyexternalapi +module github.com/kurtosis-tech/new-obd/src/libraries/currencyexternalapi go 1.19 diff --git a/src/currencyexternalapi/go.sum b/src/libraries/currencyexternalapi/go.sum similarity index 100% rename from src/currencyexternalapi/go.sum rename to src/libraries/currencyexternalapi/go.sum diff --git a/src/currencyexternalapi/gomod2nix.toml b/src/libraries/currencyexternalapi/gomod2nix.toml similarity index 100% rename from src/currencyexternalapi/gomod2nix.toml rename to src/libraries/currencyexternalapi/gomod2nix.toml diff --git a/src/events/siteevents.go b/src/libraries/events/eventsmanager.go similarity index 65% rename from src/events/siteevents.go rename to src/libraries/events/eventsmanager.go index 44c2f86..b49a24c 100644 --- a/src/events/siteevents.go +++ b/src/libraries/events/eventsmanager.go @@ -8,6 +8,7 @@ import ( "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sns" "github.com/aws/aws-sdk-go/service/sqs" + "os" "time" ) @@ -16,10 +17,33 @@ type EventsManager struct { queueUrl string } -func NewPageVisitsEventsManager(snsTopicARN string, queueUrl string) *EventsManager { +func newEventsManager(snsTopicARN string, queueUrl string) *EventsManager { return &EventsManager{snsTopicARN: snsTopicARN, queueUrl: queueUrl} } +func CreateEventsManager() (*EventsManager, error) { + + //awsKey := os.Getenv("AWS_ACCESS_KEY_ID") + //awsSecret := os.Getenv("AWS_SECRET_ACCESS_KEY") + awsRegion := os.Getenv("AWS_REGION") + + if awsRegion == "" { + //return nil, errors.New("imposible to init the events manager component because the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY or AWS_REGION environment variable not set") + return nil, errors.New("imposible to init the events manager component because AWS_REGION environment variable not set") + } + + snsTopicARN := os.Getenv("SNS_TOPIC_ARN") + queueUrl := os.Getenv("QUEUE_URL") + + if snsTopicARN == "" || queueUrl == "" { + return nil, errors.New("imposible to init the events manager component because the SNS_TOPIC_ARN or QUEUE_URL environment variable not set") + } + + manager := newEventsManager(snsTopicARN, queueUrl) + + return manager, nil +} + func (manager *EventsManager) PublishMessage(message string) error { sess := session.Must(session.NewSession()) svc := sns.New(sess) diff --git a/src/events/go.mod b/src/libraries/events/go.mod similarity index 64% rename from src/events/go.mod rename to src/libraries/events/go.mod index e200f94..8bb830a 100644 --- a/src/events/go.mod +++ b/src/libraries/events/go.mod @@ -1,4 +1,4 @@ -module github.com/kurtosis-tech/new-obd/src/events +module github.com/kurtosis-tech/new-obd/src/libraries/events go 1.19 diff --git a/src/events/go.sum b/src/libraries/events/go.sum similarity index 100% rename from src/events/go.sum rename to src/libraries/events/go.sum diff --git a/src/libraries/events/middleware.go b/src/libraries/events/middleware.go new file mode 100644 index 0000000..184ecb3 --- /dev/null +++ b/src/libraries/events/middleware.go @@ -0,0 +1,44 @@ +package events + +import ( + "fmt" + "net/http" + "strings" +) + +const ( + ctxKeyUserID = "user-id" +) + +var avoidURLsPrefix = []string{"/_", "/static"} + +func GetWrapsWithEventsManagerMiddleware(manager *EventsManager) func(next http.Handler) http.Handler { + + return func(next http.Handler) http.Handler { + + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + + userId := r.Context().Value(ctxKeyUserID) + + urlToTrack := r.URL + + shouldTrackURL := true + + for _, prefix := range avoidURLsPrefix { + if strings.HasPrefix(urlToTrack.String(), prefix) { + shouldTrackURL = false + } + } + + if manager != nil && shouldTrackURL { + eventMsg := fmt.Sprintf("USER: %s VISITED: %s", userId, r.URL) + manager.PublishMessage(eventMsg) + } + + if next != nil && r != nil { + next.ServeHTTP(w, r) + } + + }) + } +} diff --git a/src/events/notification.go b/src/libraries/events/notification.go similarity index 100% rename from src/events/notification.go rename to src/libraries/events/notification.go diff --git a/src/frontend/consts/consts.go b/src/libraries/tracing/consts.go similarity index 79% rename from src/frontend/consts/consts.go rename to src/libraries/tracing/consts.go index 8ee3f38..d2758b5 100644 --- a/src/frontend/consts/consts.go +++ b/src/libraries/tracing/consts.go @@ -1,4 +1,4 @@ -package consts +package tracing const ( KardinalTraceIdHeaderKey = "X-Kardinal-Trace-Id" diff --git a/src/libraries/tracing/go.mod b/src/libraries/tracing/go.mod new file mode 100644 index 0000000..cfcb762 --- /dev/null +++ b/src/libraries/tracing/go.mod @@ -0,0 +1,7 @@ +module github.com/kurtosis-tech/new-obd/src/libraries/tracing + +go 1.19 + +require github.com/sirupsen/logrus v1.9.3 + +require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect diff --git a/src/libraries/tracing/go.sum b/src/libraries/tracing/go.sum new file mode 100644 index 0000000..21f9bfb --- /dev/null +++ b/src/libraries/tracing/go.sum @@ -0,0 +1,15 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/frontend/tracing.go b/src/libraries/tracing/tracing.go similarity index 71% rename from src/frontend/tracing.go rename to src/libraries/tracing/tracing.go index 22c9a38..f963eb0 100644 --- a/src/frontend/tracing.go +++ b/src/libraries/tracing/tracing.go @@ -1,7 +1,6 @@ -package main +package tracing import ( - "github.com/kurtosis-tech/new-obd/src/frontend/consts" "github.com/sirupsen/logrus" "net/http" ) @@ -9,7 +8,7 @@ import ( func KardinalTracingContextWrapper(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - traceID := r.Header.Get(consts.KardinalTraceIdHeaderKey) + traceID := r.Header.Get(KardinalTraceIdHeaderKey) logrus.Infof("[KARDINAL-DEBUG] Trace ID: %s", traceID) if next != nil && r != nil { diff --git a/src/metrics.dockerfile b/src/metrics.dockerfile new file mode 100644 index 0000000..fc90af5 --- /dev/null +++ b/src/metrics.dockerfile @@ -0,0 +1,32 @@ +FROM golang:1.21.9-alpine AS builder + +# Set Go env +ENV CGO_ENABLED=0 GOOS=linux +WORKDIR /go/src/hipstershop + +# Build Go binary +COPY ./metrics ./metrics +COPY ./libraries ./libraries +WORKDIR /go/src/hipstershop/metrics +RUN go env -w GOPROXY=https://goproxy.io,direct/ +RUN go mod download + +RUN go build -o /go/src/hipstershop/metricsbin . + +# # Deployment container +FROM alpine:latest + +# Install dependencies +RUN apk --update --no-cache add ca-certificates protoc + +# Definition of this variable is used by 'skaffold debug' to identify a golang binary. +# Default behavior - a failure prints a stack trace for the current goroutine. +# See https://golang.org/pkg/runtime/ +ENV GOTRACEBACK=single + +COPY --from=builder /etc/ssl/certs /etc/ssl/certs +COPY --from=builder /go/src/hipstershop/metricsbin /hipstershop/metrics +COPY ./metrics/templates ./templates +COPY ./metrics/static ./static + +ENTRYPOINT ["/hipstershop/metrics"] diff --git a/src/metrics/.dockerignore b/src/metrics/.dockerignore new file mode 100644 index 0000000..a69a59e --- /dev/null +++ b/src/metrics/.dockerignore @@ -0,0 +1,4 @@ +.gitignore +../Dockerfile +default.nix +gomod2nix.toml diff --git a/src/metrics/.gitignore b/src/metrics/.gitignore new file mode 100644 index 0000000..3e9796e --- /dev/null +++ b/src/metrics/.gitignore @@ -0,0 +1,21 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# IDE +.vscode + +# don't commit the service binary to vcs +metrics diff --git a/src/metrics/Makefile b/src/metrics/Makefile new file mode 100644 index 0000000..42557c6 --- /dev/null +++ b/src/metrics/Makefile @@ -0,0 +1,21 @@ +GOPATH:=$(shell go env GOPATH) + +.PHONY: update +update: + @go get -u + +.PHONY: tidy +tidy: + @go mod tidy + +.PHONY: build +build: + @go build -o metrics *.go + +.PHONY: test +test: + @go test -v ./... -cover + +.PHONY: docker +docker: + @docker build -t metrics:latest . \ No newline at end of file diff --git a/src/metrics/go.mod b/src/metrics/go.mod new file mode 100644 index 0000000..d37f955 --- /dev/null +++ b/src/metrics/go.mod @@ -0,0 +1,22 @@ +module github.com/kurtosis-tech/new-obd/src/metrics + +go 1.21.9 + +toolchain go1.22.4 + +replace ( + github.com/kurtosis-tech/new-obd/src/libraries/events => ./../libraries/events +) + +require ( + github.com/gorilla/mux v1.8.1 + github.com/gorilla/websocket v1.5.3 + github.com/kurtosis-tech/new-obd/src/libraries/events v0.0.0 + github.com/sirupsen/logrus v1.9.3 +) + +require ( + github.com/aws/aws-sdk-go v1.55.5 // indirect + github.com/jmespath/go-jmespath v0.4.0 // indirect + golang.org/x/sys v0.25.0 // indirect +) diff --git a/src/metrics/go.sum b/src/metrics/go.sum new file mode 100644 index 0000000..e74ae08 --- /dev/null +++ b/src/metrics/go.sum @@ -0,0 +1,28 @@ +github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU= +github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/metrics/handlers.go b/src/metrics/handlers.go new file mode 100644 index 0000000..fbf4632 --- /dev/null +++ b/src/metrics/handlers.go @@ -0,0 +1,64 @@ +package main + +import ( + "fmt" + "github.com/gorilla/websocket" + "github.com/sirupsen/logrus" + "html/template" + "net/http" +) + +var ( + templates = template.Must(template.New("").ParseGlob("templates/*.html")) +) + +var upgrader = websocket.Upgrader{} + +func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) { + + if err := templates.ExecuteTemplate(w, "home", map[string]interface{}{}); err != nil { + logrus.Error(err) + } +} + +// WebSocket connection handler +func (fe *frontendServer) handleWebSocket(w http.ResponseWriter, r *http.Request) { + + conn, err := upgrader.Upgrade(w, r, nil) + if err != nil { + fmt.Println("Error while upgrading connection:", err) + return + } + fe.startSQSReceiver(conn) + +} + +func (fe *frontendServer) startSQSReceiver(conn *websocket.Conn) { + + msgChan := make(chan string) + errorChan := make(chan error) + if fe.eventsManager == nil { + msg := "No events will be received because the events manager component has not been initialized." + if err := conn.WriteJSON(map[string]string{"Message": msg}); err != nil { + fmt.Println("Error broadcasting message:", err) + } + conn.Close() + return + } + go fe.eventsManager.ReceiveMessages(msgChan, errorChan) + //receive msgs and errors + go func() { + defer conn.Close() + for { + select { + case err := <-errorChan: + logrus.Error(err) + case msg := <-msgChan: + if err := conn.WriteJSON(map[string]string{"Message": msg}); err != nil { + fmt.Println("Error broadcasting message:", err) + conn.Close() + } + } + } + }() +} diff --git a/src/metrics/main.go b/src/metrics/main.go new file mode 100644 index 0000000..d650098 --- /dev/null +++ b/src/metrics/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "github.com/kurtosis-tech/new-obd/src/libraries/events" + "net/http" + "os" + "time" + + "github.com/gorilla/mux" + "github.com/sirupsen/logrus" +) + +const ( + name = "frontend" +) + +type frontendServer struct { + eventsManager *events.EventsManager +} + +func main() { + log := logrus.New() + log.Level = logrus.DebugLevel + log.Formatter = &logrus.JSONFormatter{ + FieldMap: logrus.FieldMap{ + logrus.FieldKeyTime: "timestamp", + logrus.FieldKeyLevel: "severity", + logrus.FieldKeyMsg: "message", + }, + TimestampFormat: time.RFC3339Nano, + } + log.Out = os.Stdout + + eventsManager, err := events.CreateEventsManager() + if err != nil { + logrus.Errorf("An error occurred initializing events manager! No site events will be received.\nError was: %s", err) + } + + svc := &frontendServer{ + eventsManager: eventsManager, + } + + r := mux.NewRouter() + r.HandleFunc("/", svc.homeHandler).Methods(http.MethodGet, http.MethodHead) + r.HandleFunc("/ws", svc.handleWebSocket).Methods(http.MethodGet) + r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))) + r.HandleFunc("/robots.txt", func(w http.ResponseWriter, _ *http.Request) { fmt.Fprint(w, "User-agent: *\nDisallow: /") }) + r.HandleFunc("/_healthz", func(w http.ResponseWriter, _ *http.Request) { fmt.Fprint(w, "ok") }) + + var handler http.Handler = r + + // Start the server + http.Handle("/", r) + fmt.Println("Server starting on port 8091...") + http.ListenAndServe(":8091", handler) +} diff --git a/src/metrics/static/favicon.ico b/src/metrics/static/favicon.ico new file mode 100755 index 0000000000000000000000000000000000000000..30acb2725be995c3ad752a21cf5a001a3577124f GIT binary patch literal 16958 zcmd^GTc}i37~bQkgO@yt8D7FU=ONVepkZhS51vtcD)c2$6C^{*9z+Di3y3q2BKlJ5 zi{V2^`XcH}l97@Vl}2DzRtO1AX4wrp-N!hc>HGHC>#)vp{r~K_syOnUf9-$$m+xD% zZ?k6%f&~6$G6DX!22&RX!9zh1OaS2+^D9AN0UbChmJwOq9OTpBt*tk3Opa``TTO+dqi!2Fk^`^UPtc>zB64 zX;mfrL8bape@g{?&Lj3Y;6)|pJIoQ1Cg!pRYe_M#e@omos6;>d?~cjg9OAmj0haeW zKPY)CG53rz&)o2S2(}AgZC9^vdv}~Qh^rqxc^~5IWBa~G%z)2w*xsw;@%){Lpj|)q z+FB650$?w@w{N`3&hw4q=tm#EN51;D?+wI^wR>QD3h=Lx`yc#|nRt!?`s|u!%owL4{EjrHkAKF`Hcjo&ag(d4@pb4QijV{W{(jT*<4I!BefiO3^IiGTk!hcyKK zCzX8d*qx{3{f)WF5^vYfSb#Z_j(Kj5*+!0bE;wo8XkGot9S=yK=f1?8wBPet&&Pno zH*5EeGe)hfQtuMxyw8N-=W#%;J}%TY-t_nA-zem<)@qaITFk9g^1jBL_xj`3vA3B6 zFxRJpzO{C|J;vN>C2ymN_2|DSMENhhMF~e5_Iofq>8FL){`;p_@U$~#$c7M-&_|FMBtn&(E>hVIY zeM+Bmz8;9@=L6}u+|L|GzqFqn7wRunIQ@jV(Z0NDe5?hqrtkfsZ>g^?{Ktiy@VIpJ zUlU?)yLKUt=jLyOc=CDP$EOw_YbyQ7<+f`Kd|4sq8o3z#h~f2HTHEC=Z^Y)5cwR%i z*Jig)CEKhCNY^KF&$;#BTMkIN23h*Ke@Sr}yDs8d0qwbDhaF$fG5U5WJjtom?uYG+ z5>0htJe1*B8)#|AGULHPjl< zo(dmwC1-N?t;Rbh^`->g+jwfXF=@FHh&_vydv(SJgSy#&AXe3j4D z9;udpt$FMXkb_SnIe#L|o$%=2A;e1B*>g{=8fwia2Z1;FN!)jM^mi(CeCx~apdP4| zf35lC0FaB1qvY%K=pTw2e7-8>xc-X1YpzwNmJiPSPTu7wZBt`-t+LO5tPj6*4sdIo zN38cVvBtAebL0YC09>BF?eN$q7mxlWF>3Ibn%yWZ)z&2^Ksw&-by#A0c=QiJ4e6PI zsd32U-)$D$4*`a!&r;hE!`szQKjx1pHMqWfPL{b#L2lYUaKvb*b@d~c=Robg(zmAf zlJ6=tnl96Zq}f~ z9RJXM2Iv;R?=eh{&vKsol$75nKYPaECiOpK^qcd6&!R^ga2@bzH$Fa(Qh#gZt6i;+ zy#VbQ74N z1XZIyS_A&ZBnL>pS&hrN9&zMAF5chfXlqfeep3@Y`5k-Q&)xOV>egUy?{5 z1bhb6x)!X#S_6IqZ}+Qq+$jAvAMob)FiU}*0KYr_6!;PN3ovWLF@2n43}cxS{!YoR zSI=>zzc-5kUOio*>9@sn-*zDt+MXcj;-EVST4?h@kfbFe7)*x&McQ;276ul$jS6}E zBqD=^9R54Yh6K_cPUM~lO1(j6GT#|wdxByzpANcu65XjnORlS!q|K*`$wEtaDxbzy zx(H8q=<9A_Oc + + + + + Metrics Viewer + + + + +
+

Metrics Dashboard

+
+
+

Page Visits

+
+
+ + + + +{{ end }} diff --git a/src/productcatalogservice/Dockerfile b/src/productcatalogservice.dockerfile similarity index 58% rename from src/productcatalogservice/Dockerfile rename to src/productcatalogservice.dockerfile index 3798505..5449f37 100644 --- a/src/productcatalogservice/Dockerfile +++ b/src/productcatalogservice.dockerfile @@ -4,19 +4,13 @@ FROM golang:1.21.9-alpine AS builder ENV CGO_ENABLED=0 GOOS=linux WORKDIR /go/src/hipstershop -# Install dependencies -RUN apk --update --no-cache add ca-certificates make protoc - - # Build Go binary -COPY Makefile go.mod go.sum ./ +COPY ./productcatalogservice ./productcatalogservice +WORKDIR /go/src/hipstershop/productcatalogservice RUN go env -w GOPROXY=https://goproxy.io,direct/ RUN go mod download -COPY . . -# Skaffold passes in debug-oriented compiler flags -ARG SKAFFOLD_GO_GCFLAGS -RUN go build -gcflags="${SKAFFOLD_GO_GCFLAGS}" -o /go/src/hipstershop/productcatalogservice . +RUN go build -o /go/src/hipstershop/productcatalogservicebin . # Deployment container FROM scratch @@ -28,8 +22,8 @@ WORKDIR /hipstershop # See https://golang.org/pkg/runtime/ ENV GOTRACEBACK=single -COPY ./data /hipstershop/data/ +COPY productcatalogservice/data /hipstershop/data/ COPY --from=builder /etc/ssl/certs /etc/ssl/certs -COPY --from=builder /go/src/hipstershop/productcatalogservice /hipstershop/productcatalogservice +COPY --from=builder /go/src/hipstershop/productcatalogservicebin /hipstershop/productcatalogservice ENTRYPOINT ["/hipstershop/productcatalogservice"] diff --git a/src/productcatalogservice/go.mod b/src/productcatalogservice/go.mod index 92c15d9..a1bf4da 100644 --- a/src/productcatalogservice/go.mod +++ b/src/productcatalogservice/go.mod @@ -2,12 +2,9 @@ module github.com/kurtosis-tech/new-obd/src/productcatalogservice go 1.21.9 -replace github.com/kurtosis-tech/new-obd/src/events => ../events - require ( github.com/deepmap/oapi-codegen/v2 v2.2.1-0.20240604070534-2f0ff757704b github.com/getkin/kin-openapi v0.124.0 - github.com/kurtosis-tech/new-obd/src/events v0.0.0-00010101000000-000000000000 github.com/labstack/echo/v4 v4.12.0 github.com/oapi-codegen/runtime v1.1.1 github.com/sirupsen/logrus v1.8.1 @@ -15,13 +12,11 @@ require ( require ( github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect - github.com/aws/aws-sdk-go v1.55.5 // indirect github.com/go-openapi/jsonpointer v0.20.2 // indirect github.com/go-openapi/swag v0.22.8 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/google/uuid v1.5.0 // indirect github.com/invopop/yaml v0.2.0 // indirect - github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/labstack/gommon v0.4.2 // indirect github.com/mailru/easyjson v0.7.7 // indirect diff --git a/src/productcatalogservice/main.go b/src/productcatalogservice/main.go index d5ab002..6cc2808 100644 --- a/src/productcatalogservice/main.go +++ b/src/productcatalogservice/main.go @@ -2,13 +2,11 @@ package main import ( "fmt" - "github.com/kurtosis-tech/new-obd/src/events" productcatalogservice_server_rest_server "github.com/kurtosis-tech/new-obd/src/productcatalogservice/api/http_rest/server" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "github.com/sirupsen/logrus" "net" - "os" ) const ( @@ -40,25 +38,5 @@ func main() { productcatalogservice_server_rest_server.RegisterHandlers(echoRouter, productcatalogservice_server_rest_server.NewStrictHandler(server, nil)) - snsTopicARN := os.Getenv("SNS_TOPIC_ARN") - queueUrl := os.Getenv("QUEUE_URL") - - eventsManager := events.NewPageVisitsEventsManager(snsTopicARN, queueUrl) - - msgChan := make(chan string) - errorChan := make(chan error) - go eventsManager.ReceiveMessages(msgChan, errorChan) - //receive msgs and errors - go func() { - for { - select { - case err := <-errorChan: - logrus.Error(err) - case msg := <-msgChan: - logrus.Info(msg) - } - } - }() - echoRouter.Start(net.JoinHostPort(restAPIHostIP, fmt.Sprint(restAPIPortAddr))) } From 65b5ade18d2c409fc975457b838781cdc280fab4 Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Fri, 20 Sep 2024 18:40:52 -0300 Subject: [PATCH 03/10] update Tiltfile and kardinal manifest --- .github/workflows/ci.yaml | 1 + Tiltfile | 22 +++++-- release/obd-kardinal.yaml | 89 +++++++++++++++++++++++++-- src/libraries/events/eventsmanager.go | 9 ++- 4 files changed, 106 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 501dd1e..a24cbbf 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -33,6 +33,7 @@ jobs: uses: DeterminateSystems/flake-checker-action@main with: fail-mode: true + check-outdated: false # Nix-specific logic begins here - name: Nix Check diff --git a/Tiltfile b/Tiltfile index 90d048a..9ae5e20 100644 --- a/Tiltfile +++ b/Tiltfile @@ -2,10 +2,12 @@ FRONTEND_SERVICE_NAME='frontend' CARTSERVICE_SERVICE_NAME='cartservice' PRODUCTCATALOG_SERVICE_NAME='productcatalogservice' +METRICS_SERVICE_NAME='metrics' DEV_IMAGE_SUFFIX='-dev-image' FRONTEND_REF=FRONTEND_SERVICE_NAME + DEV_IMAGE_SUFFIX CARTSERVICE_REF=CARTSERVICE_SERVICE_NAME + DEV_IMAGE_SUFFIX PRODUCTCATALOG_REF=PRODUCTCATALOG_SERVICE_NAME + DEV_IMAGE_SUFFIX +METRICS_REF=METRICS_SERVICE_NAME + DEV_IMAGE_SUFFIX # enforces a minimum Tilt version version_settings(constraint='>=0.22.2') @@ -26,7 +28,7 @@ if 'build' in arguments: build_arg_len = len(build_arg) # validate the 'build' argument -valid_build_arguments = [FRONTEND_SERVICE_NAME, CARTSERVICE_SERVICE_NAME, PRODUCTCATALOG_SERVICE_NAME] +valid_build_arguments = [FRONTEND_SERVICE_NAME, CARTSERVICE_SERVICE_NAME, PRODUCTCATALOG_SERVICE_NAME, METRICS_SERVICE_NAME] for bu_arg in build_arg: if bu_arg not in valid_build_arguments: fail('build argument {} is not valid. Valid arguments are: {}'.format(bu_arg, valid_build_arguments)) @@ -40,7 +42,7 @@ if k8s_context() == 'minikube': local('eval $(minikube docker-env)') # set the env var to use the local Kontrol -local('export KARDINAL_CLI_DEV_MODE=TRUE') +os.environ["KARDINAL_CLI_DEV_MODE"] = "TRUE" # clean current flows before creating new ones, it's just for the tilt up cmd if config.tilt_subcommand == 'up': @@ -70,16 +72,16 @@ if config.tilt_subcommand == 'up': if 'cartservice' in build_arg: docker_build( CARTSERVICE_REF, - context='./src/cartservice', - dockerfile='./src/cartservice/Dockerfile', + context='./src', + dockerfile='./src/cartservice.dockerfile', ) # PRODUCT CATALOG SERVICE if 'productcatalogservice' in build_arg: docker_build( PRODUCTCATALOG_REF, - context='./src/productcatalogservice', - dockerfile='./src/productcatalogservice/Dockerfile', + context='./src', + dockerfile='./src/productcatalogservice.dockerfile', ) # FRONTEND @@ -90,6 +92,14 @@ if 'frontend' in build_arg: dockerfile='./src/frontend.dockerfile', ) +# METRICS +if 'metrics' in build_arg: + docker_build( + METRICS_REF, + context='./src', + dockerfile='./src/metrics.dockerfile', + ) + kardinal_topology_yaml = local(['kardinal', 'topology', 'print-manifest', '--add-trace-router'], quiet=True) kardinal_topology_yaml_str = str(kardinal_topology_yaml) diff --git a/release/obd-kardinal.yaml b/release/obd-kardinal.yaml index 937acee..1762708 100644 --- a/release/obd-kardinal.yaml +++ b/release/obd-kardinal.yaml @@ -129,14 +129,22 @@ spec: - name: "Cookie" value: "shop_session-id=x-liveness-probe" env: - - name: ADDRESS - value: ":8080" - name: JSDELIVRAPIKEY value: "prod" - name: CARTSERVICEHOST value: cartservice - name: PRODUCTCATALOGSERVICEHOST value: productcatalogservice + #- name: AWS_REGION + # value: us-east-1 + #- name: AWS_ACCESS_KEY_ID + # value: + #- name: AWS_SECRET_ACCESS_KEY + # value: + #- name: QUEUE_URL + # value: + #- name: SNS_TOPIC_ARN + # value: --- apiVersion: v1 kind: Service @@ -170,7 +178,7 @@ metadata: name: frontend-external annotations: kardinal.dev.service/ingress: "true" - kardinal.dev.service/host: "prod.app.localhost" + kardinal.dev.service/host: "baseline.app.localhost" spec: type: LoadBalancer selector: @@ -331,4 +339,77 @@ spec: port: 8070 targetPort: 8070 protocol: TCP - appProtocol: HTTP \ No newline at end of file + appProtocol: HTTP + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: metrics-v1 + labels: + app: metrics + version: v1 +spec: + selector: + matchLabels: + app: metrics + version: v1 + template: + metadata: + labels: + app: metrics + version: v1 + annotations: + sidecar.istio.io/rewriteAppHTTPProbers: "true" + spec: + containers: + - name: server + image: kurtosistech/metrics:main + imagePullPolicy: IfNotPresent + ports: + - containerPort: 8091 + readinessProbe: + initialDelaySeconds: 10 + httpGet: + path: "/_healthz" + port: 8091 + httpHeaders: + - name: "Cookie" + value: "shop_session-id=x-readiness-probe" + livenessProbe: + initialDelaySeconds: 10 + httpGet: + path: "/_healthz" + port: 8091 + httpHeaders: + - name: "Cookie" + value: "shop_session-id=x-liveness-probe" + #env: + #- name: AWS_REGION + # value: us-east-1 + #- name: AWS_ACCESS_KEY_ID + # value: + #- name: AWS_SECRET_ACCESS_KEY + # value: + #- name: QUEUE_URL + # value: + #- name: SNS_TOPIC_ARN + # value: +--- +apiVersion: v1 +kind: Service +metadata: + name: metrics + labels: + app: metrics + version: v1 +spec: + type: ClusterIP + selector: + app: metrics + ports: + - name: http + port: 8091 + protocol: TCP + appProtocol: HTTP + targetPort: 8091 diff --git a/src/libraries/events/eventsmanager.go b/src/libraries/events/eventsmanager.go index b49a24c..f34f1e2 100644 --- a/src/libraries/events/eventsmanager.go +++ b/src/libraries/events/eventsmanager.go @@ -23,13 +23,12 @@ func newEventsManager(snsTopicARN string, queueUrl string) *EventsManager { func CreateEventsManager() (*EventsManager, error) { - //awsKey := os.Getenv("AWS_ACCESS_KEY_ID") - //awsSecret := os.Getenv("AWS_SECRET_ACCESS_KEY") + awsKey := os.Getenv("AWS_ACCESS_KEY_ID") + awsSecret := os.Getenv("AWS_SECRET_ACCESS_KEY") awsRegion := os.Getenv("AWS_REGION") - if awsRegion == "" { - //return nil, errors.New("imposible to init the events manager component because the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY or AWS_REGION environment variable not set") - return nil, errors.New("imposible to init the events manager component because AWS_REGION environment variable not set") + if awsRegion == "" || awsKey == "" || awsSecret == "" { + return nil, errors.New("imposible to init the events manager component because the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY or AWS_REGION environment variable not set") } snsTopicARN := os.Getenv("SNS_TOPIC_ARN") From 526830974d73090096f62c01277c4623715c6cbd Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Tue, 24 Sep 2024 08:06:13 -0300 Subject: [PATCH 04/10] update flake.nix --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 9f43442..ee43d41 100644 --- a/flake.nix +++ b/flake.nix @@ -40,8 +40,8 @@ service_names = [ "cartservice" - "currencyexternalapi" "frontend" + "metrics" "productcatalogservice" ]; architectures = ["amd64" "arm64"]; From 3f471935e26308f809e9651fa6e0f76a3525c23f Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Tue, 24 Sep 2024 08:15:42 -0300 Subject: [PATCH 05/10] update gomod2nix files --- src/frontend/go.mod | 1 - src/frontend/go.sum | 2 -- src/frontend/gomod2nix.toml | 10 ++++++++-- src/libraries/events/gomod2nix.toml | 9 +++++++++ src/libraries/tracing/gomod2nix.toml | 9 +++++++++ src/metrics/go.mod | 4 +--- src/metrics/gomod2nix.toml | 21 +++++++++++++++++++++ src/productcatalogservice/go.sum | 7 ------- 8 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 src/libraries/events/gomod2nix.toml create mode 100644 src/libraries/tracing/gomod2nix.toml create mode 100644 src/metrics/gomod2nix.toml diff --git a/src/frontend/go.mod b/src/frontend/go.mod index a16bece..898ad86 100644 --- a/src/frontend/go.mod +++ b/src/frontend/go.mod @@ -16,7 +16,6 @@ require ( github.com/google/uuid v1.5.0 github.com/gorilla/mux v1.8.1 github.com/kurtosis-tech/new-obd/src/cartservice v0.0.0 - github.com/kurtosis-tech/new-obd/src/currencyexternalapi v0.0.0-20240905173058-8d378bef698b github.com/kurtosis-tech/new-obd/src/libraries/currencyexternalapi v0.0.0 github.com/kurtosis-tech/new-obd/src/libraries/events v0.0.0 github.com/kurtosis-tech/new-obd/src/libraries/tracing v0.0.0 diff --git a/src/frontend/go.sum b/src/frontend/go.sum index 6cbd217..1e6abc7 100644 --- a/src/frontend/go.sum +++ b/src/frontend/go.sum @@ -61,8 +61,6 @@ github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHW github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= -github.com/kurtosis-tech/new-obd/src/currencyexternalapi v0.0.0-20240905173058-8d378bef698b h1:zGP1/QT+Rxq9+U6y3XaciZh/7gsmbr2lY6nYbhxL3Vc= -github.com/kurtosis-tech/new-obd/src/currencyexternalapi v0.0.0-20240905173058-8d378bef698b/go.mod h1:RofAgi1WBxLMEGQvNe0grFm0NSOFtHaeWtccTWh7Fss= github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro= github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= diff --git a/src/frontend/gomod2nix.toml b/src/frontend/gomod2nix.toml index 4f80fc3..9718997 100644 --- a/src/frontend/gomod2nix.toml +++ b/src/frontend/gomod2nix.toml @@ -4,6 +4,9 @@ schema = 3 [mod."github.com/apapsch/go-jsonmerge/v2"] version = "v2.0.0" hash = "sha256-xp/1B6XUN2EbddBfoUkTV3oTk+34m4kOZP+66HhfLg4=" + [mod."github.com/aws/aws-sdk-go"] + version = "v1.55.5" + hash = "sha256-Duod/yk0bGmbcqgaZg+4XoWwY7Ysq4RA/cFBV8nFX6E=" [mod."github.com/golang/protobuf"] version = "v1.5.0" hash = "sha256-UbjypOyvcr3GwutjMR6Y3kUYcc4F7SjhCrak2PSxRPE=" @@ -16,6 +19,9 @@ schema = 3 [mod."github.com/gorilla/mux"] version = "v1.8.1" hash = "sha256-nDABvAhlYgxUW2N/brrep7NkQXoSGcHhA+XI4+tK0F0=" + [mod."github.com/jmespath/go-jmespath"] + version = "v0.4.0" + hash = "sha256-xpT9g2qIXmPq7eeHUXHiDqJeQoHCudh44G/KCSFbcuo=" [mod."github.com/oapi-codegen/runtime"] version = "v1.1.1" hash = "sha256-GRvzpQngQPAy59KvvcwhjYqjx4gttIhOtQKjqfQcNvI=" @@ -23,8 +29,8 @@ schema = 3 version = "v0.9.1" hash = "sha256-mNfQtcrQmu3sNg/7IwiieKWOgFQOVVe2yXgKBpe/wZw=" [mod."github.com/sirupsen/logrus"] - version = "v1.8.1" - hash = "sha256-vUIDlLXYBD74+JqdoSx+W3J6r5cOk63heo0ElsHizoM=" + version = "v1.9.3" + hash = "sha256-EnxsWdEUPYid+aZ9H4/iMTs1XMvCLbXZRDyvj89Ebms=" [mod."golang.org/x/sys"] version = "v0.20.0" hash = "sha256-mowlaoG2k4n1c1rApWef5EMiXd3I77CsUi8jPh6pTYA=" diff --git a/src/libraries/events/gomod2nix.toml b/src/libraries/events/gomod2nix.toml new file mode 100644 index 0000000..b6928b6 --- /dev/null +++ b/src/libraries/events/gomod2nix.toml @@ -0,0 +1,9 @@ +schema = 3 + +[mod] + [mod."github.com/aws/aws-sdk-go"] + version = "v1.55.5" + hash = "sha256-Duod/yk0bGmbcqgaZg+4XoWwY7Ysq4RA/cFBV8nFX6E=" + [mod."github.com/jmespath/go-jmespath"] + version = "v0.4.0" + hash = "sha256-xpT9g2qIXmPq7eeHUXHiDqJeQoHCudh44G/KCSFbcuo=" diff --git a/src/libraries/tracing/gomod2nix.toml b/src/libraries/tracing/gomod2nix.toml new file mode 100644 index 0000000..6475b00 --- /dev/null +++ b/src/libraries/tracing/gomod2nix.toml @@ -0,0 +1,9 @@ +schema = 3 + +[mod] + [mod."github.com/sirupsen/logrus"] + version = "v1.9.3" + hash = "sha256-EnxsWdEUPYid+aZ9H4/iMTs1XMvCLbXZRDyvj89Ebms=" + [mod."golang.org/x/sys"] + version = "v0.0.0-20220715151400-c0bba94af5f8" + hash = "sha256-8LGu1Rwel+ZWaOXHjOxoQDthPPDpJxWD9SoXdCJQq04=" diff --git a/src/metrics/go.mod b/src/metrics/go.mod index d37f955..e1af042 100644 --- a/src/metrics/go.mod +++ b/src/metrics/go.mod @@ -4,9 +4,7 @@ go 1.21.9 toolchain go1.22.4 -replace ( - github.com/kurtosis-tech/new-obd/src/libraries/events => ./../libraries/events -) +replace github.com/kurtosis-tech/new-obd/src/libraries/events => ./../libraries/events require ( github.com/gorilla/mux v1.8.1 diff --git a/src/metrics/gomod2nix.toml b/src/metrics/gomod2nix.toml new file mode 100644 index 0000000..6032f34 --- /dev/null +++ b/src/metrics/gomod2nix.toml @@ -0,0 +1,21 @@ +schema = 3 + +[mod] + [mod."github.com/aws/aws-sdk-go"] + version = "v1.55.5" + hash = "sha256-Duod/yk0bGmbcqgaZg+4XoWwY7Ysq4RA/cFBV8nFX6E=" + [mod."github.com/gorilla/mux"] + version = "v1.8.1" + hash = "sha256-nDABvAhlYgxUW2N/brrep7NkQXoSGcHhA+XI4+tK0F0=" + [mod."github.com/gorilla/websocket"] + version = "v1.5.3" + hash = "sha256-vTIGEFMEi+30ZdO6ffMNJ/kId6pZs5bbyqov8xe9BM0=" + [mod."github.com/jmespath/go-jmespath"] + version = "v0.4.0" + hash = "sha256-xpT9g2qIXmPq7eeHUXHiDqJeQoHCudh44G/KCSFbcuo=" + [mod."github.com/sirupsen/logrus"] + version = "v1.9.3" + hash = "sha256-EnxsWdEUPYid+aZ9H4/iMTs1XMvCLbXZRDyvj89Ebms=" + [mod."golang.org/x/sys"] + version = "v0.25.0" + hash = "sha256-PXZ9EQZ7SFpcL7d3E1+KGTxziYlHEIZPfoXEbnaVD3I=" diff --git a/src/productcatalogservice/go.sum b/src/productcatalogservice/go.sum index 9dc6405..c99bdc2 100644 --- a/src/productcatalogservice/go.sum +++ b/src/productcatalogservice/go.sum @@ -1,8 +1,6 @@ github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ= github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk= -github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU= -github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -23,10 +21,6 @@ github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY= github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q= -github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= -github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= -github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= -github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= @@ -91,7 +85,6 @@ golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From b3f0e440d478ac85d27374a2971beee0f1ce8c75 Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Tue, 24 Sep 2024 08:16:16 -0300 Subject: [PATCH 06/10] remove packages.currencyexternalapi from flake.nix file --- flake.nix | 4 ---- 1 file changed, 4 deletions(-) diff --git a/flake.nix b/flake.nix index ee43d41..0bf5f0b 100644 --- a/flake.nix +++ b/flake.nix @@ -166,10 +166,6 @@ inherit pkgs; }; - packages.currencyexternalapi = pkgs.callPackage ./src/currencyexternalapi { - inherit pkgs; - }; - packages.frontend = pkgs.callPackage ./src/frontend { inherit pkgs; }; From 68d37a756f49ada1b0727fffa922988077639a30 Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Tue, 24 Sep 2024 08:18:24 -0300 Subject: [PATCH 07/10] adding the metrics service package in flake.nix file --- flake.nix | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/flake.nix b/flake.nix index 0bf5f0b..67048b3 100644 --- a/flake.nix +++ b/flake.nix @@ -166,6 +166,10 @@ inherit pkgs; }; + packages.metrics = pkgs.callPackage ./src/metrics { + inherit pkgs; + }; + packages.frontend = pkgs.callPackage ./src/frontend { inherit pkgs; }; From c326d3c1982b5e1025defb1ae930d739264654e0 Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Tue, 24 Sep 2024 08:21:06 -0300 Subject: [PATCH 08/10] adding default.nix in metrics and removing it from currencyexternalapi --- src/{libraries/currencyexternalapi => metrics}/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/{libraries/currencyexternalapi => metrics}/default.nix (91%) diff --git a/src/libraries/currencyexternalapi/default.nix b/src/metrics/default.nix similarity index 91% rename from src/libraries/currencyexternalapi/default.nix rename to src/metrics/default.nix index f2b4107..054e7e5 100644 --- a/src/libraries/currencyexternalapi/default.nix +++ b/src/metrics/default.nix @@ -1,5 +1,5 @@ {pkgs}: let - pname = "currencyexternalapi"; + pname = "metrics"; in pkgs.buildGoApplication { # pname has to match the location (folder) where the main function is or use From f836798c4b325d629656419347f018fa67c20b47 Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Tue, 24 Sep 2024 08:28:54 -0300 Subject: [PATCH 09/10] adding metrics in ci.yaml and removing currencyexternalapi from it --- .github/workflows/ci.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index a24cbbf..5033d7e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -78,10 +78,10 @@ jobs: nix build ./#containers.x86_64-linux.cartservice.arm64 --no-link --print-out-paths nix build ./#containers.x86_64-linux.cartservice.amd64 --no-link --print-out-paths - - name: Build currencyexternalapi images + - name: Build metrics images run: | - nix build ./#containers.x86_64-linux.currencyexternalapi.arm64 --no-link --print-out-paths - nix build ./#containers.x86_64-linux.currencyexternalapi.amd64 --no-link --print-out-paths + nix build ./#containers.x86_64-linux.metrics.arm64 --no-link --print-out-paths + nix build ./#containers.x86_64-linux.metrics.amd64 --no-link --print-out-paths - name: Build frontend images run: | @@ -105,13 +105,13 @@ jobs: # Set tag to be the branch name and version nix develop --command tag-branch-version nix run ./#publish-cartservice-container - nix run ./#publish-currencyexternalapi-container + nix run ./#publish-metrics-container nix run ./#publish-frontend-container nix run ./#publish-productcatalogservice-container # Set tag to be the branch name nix develop --command tag-branch nix run ./#publish-cartservice-container - nix run ./#publish-currencyexternalapi-container + nix run ./#publish-metrics-container nix run ./#publish-frontend-container nix run ./#publish-productcatalogservice-container From 1d90485880b27b83df5756d2e11519ffa73b69d4 Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Wed, 25 Sep 2024 12:48:01 -0300 Subject: [PATCH 10/10] handling error when publishing message in the events middleware --- src/libraries/events/go.mod | 10 ++++++++-- src/libraries/events/go.sum | 11 ++++++++++- src/libraries/events/gomod2nix.toml | 6 ++++++ src/libraries/events/middleware.go | 6 +++++- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/libraries/events/go.mod b/src/libraries/events/go.mod index 8bb830a..7eae33e 100644 --- a/src/libraries/events/go.mod +++ b/src/libraries/events/go.mod @@ -2,6 +2,12 @@ module github.com/kurtosis-tech/new-obd/src/libraries/events go 1.19 -require github.com/aws/aws-sdk-go v1.55.5 +require ( + github.com/aws/aws-sdk-go v1.55.5 + github.com/sirupsen/logrus v1.9.3 +) -require github.com/jmespath/go-jmespath v0.4.0 // indirect +require ( + github.com/jmespath/go-jmespath v0.4.0 // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect +) diff --git a/src/libraries/events/go.sum b/src/libraries/events/go.sum index d323266..e7d6ac7 100644 --- a/src/libraries/events/go.sum +++ b/src/libraries/events/go.sum @@ -1,14 +1,23 @@ github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU= github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/libraries/events/gomod2nix.toml b/src/libraries/events/gomod2nix.toml index b6928b6..b935fd8 100644 --- a/src/libraries/events/gomod2nix.toml +++ b/src/libraries/events/gomod2nix.toml @@ -7,3 +7,9 @@ schema = 3 [mod."github.com/jmespath/go-jmespath"] version = "v0.4.0" hash = "sha256-xpT9g2qIXmPq7eeHUXHiDqJeQoHCudh44G/KCSFbcuo=" + [mod."github.com/sirupsen/logrus"] + version = "v1.9.3" + hash = "sha256-EnxsWdEUPYid+aZ9H4/iMTs1XMvCLbXZRDyvj89Ebms=" + [mod."golang.org/x/sys"] + version = "v0.0.0-20220715151400-c0bba94af5f8" + hash = "sha256-8LGu1Rwel+ZWaOXHjOxoQDthPPDpJxWD9SoXdCJQq04=" diff --git a/src/libraries/events/middleware.go b/src/libraries/events/middleware.go index 184ecb3..d1798a8 100644 --- a/src/libraries/events/middleware.go +++ b/src/libraries/events/middleware.go @@ -2,6 +2,7 @@ package events import ( "fmt" + "github.com/sirupsen/logrus" "net/http" "strings" ) @@ -32,7 +33,10 @@ func GetWrapsWithEventsManagerMiddleware(manager *EventsManager) func(next http. if manager != nil && shouldTrackURL { eventMsg := fmt.Sprintf("USER: %s VISITED: %s", userId, r.URL) - manager.PublishMessage(eventMsg) + err := manager.PublishMessage(eventMsg) + if err != nil { + logrus.Errorf("error publishing the page visit message with events manager: %v", err) + } } if next != nil && r != nil {