diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 501dd1e..5033d7e 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 @@ -77,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: | @@ -104,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 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/flake.nix b/flake.nix index 9f43442..67048b3 100644 --- a/flake.nix +++ b/flake.nix @@ -40,8 +40,8 @@ service_names = [ "cartservice" - "currencyexternalapi" "frontend" + "metrics" "productcatalogservice" ]; architectures = ["amd64" "arm64"]; @@ -166,7 +166,7 @@ inherit pkgs; }; - packages.currencyexternalapi = pkgs.callPackage ./src/currencyexternalapi { + packages.metrics = pkgs.callPackage ./src/metrics { inherit pkgs; }; 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/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 d020422..898ad86 100644 --- a/src/frontend/go.mod +++ b/src/frontend/go.mod @@ -6,25 +6,30 @@ 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/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 - ) 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/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 ( 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..1e6abc7 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= @@ -63,11 +69,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= @@ -98,8 +103,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= @@ -141,6 +146,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/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/frontend/handlers.go b/src/frontend/handlers.go index 24e25fb..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,11 +49,8 @@ var ( plat platformDetails ) -const ( - userID = "0494c5e0-dde0-48fa-a6d8-f7962f5476bf" -) - func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) { + currencies, err := fe.currencyService.GetSupportedCurrencies(r.Context()) if err != nil { renderHTTPError(r, w, errors.Wrapf(err, "error retrieving currencies"), http.StatusInternalServerError) @@ -99,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, @@ -432,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 cb83253..d3a5a0a 100644 --- a/src/frontend/main.go +++ b/src/frontend/main.go @@ -2,6 +2,8 @@ package main import ( "fmt" + "github.com/kurtosis-tech/new-obd/src/libraries/events" + "github.com/kurtosis-tech/new-obd/src/libraries/tracing" "net/http" "os" "time" @@ -23,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{} @@ -90,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/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/libraries/events/eventsmanager.go b/src/libraries/events/eventsmanager.go new file mode 100644 index 0000000..f34f1e2 --- /dev/null +++ b/src/libraries/events/eventsmanager.go @@ -0,0 +1,93 @@ +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" + "os" + "time" +) + +type EventsManager struct { + snsTopicARN string + queueUrl string +} + +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 == "" || 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") + 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) + + _, 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/libraries/events/go.mod b/src/libraries/events/go.mod new file mode 100644 index 0000000..7eae33e --- /dev/null +++ b/src/libraries/events/go.mod @@ -0,0 +1,13 @@ +module github.com/kurtosis-tech/new-obd/src/libraries/events + +go 1.19 + +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 + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect +) diff --git a/src/libraries/events/go.sum b/src/libraries/events/go.sum new file mode 100644 index 0000000..e7d6ac7 --- /dev/null +++ b/src/libraries/events/go.sum @@ -0,0 +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/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 new file mode 100644 index 0000000..b935fd8 --- /dev/null +++ b/src/libraries/events/gomod2nix.toml @@ -0,0 +1,15 @@ +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=" + [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 new file mode 100644 index 0000000..d1798a8 --- /dev/null +++ b/src/libraries/events/middleware.go @@ -0,0 +1,48 @@ +package events + +import ( + "fmt" + "github.com/sirupsen/logrus" + "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) + 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 { + next.ServeHTTP(w, r) + } + + }) + } +} diff --git a/src/libraries/events/notification.go b/src/libraries/events/notification.go new file mode 100644 index 0000000..6fa0bd2 --- /dev/null +++ b/src/libraries/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/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/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/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/currencyexternalapi/default.nix b/src/metrics/default.nix similarity index 91% rename from src/currencyexternalapi/default.nix rename to src/metrics/default.nix index f2b4107..054e7e5 100644 --- a/src/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 diff --git a/src/metrics/go.mod b/src/metrics/go.mod new file mode 100644 index 0000000..e1af042 --- /dev/null +++ b/src/metrics/go.mod @@ -0,0 +1,20 @@ +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/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/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 0000000..30acb27 Binary files /dev/null and b/src/metrics/static/favicon.ico differ diff --git a/src/metrics/static/styles/styles.css b/src/metrics/static/styles/styles.css new file mode 100644 index 0000000..fd274cc --- /dev/null +++ b/src/metrics/static/styles/styles.css @@ -0,0 +1,59 @@ +body { + font-family: Arial, sans-serif; + background-color: #f4f4f4; + margin: 0; + padding: 20px; +} +header { + background: linear-gradient(135deg, #007bff, #00c6ff); + color: #fff; + padding: 30px 20px; + text-align: center; + position: relative; + overflow: hidden; + border-radius: 10px; /* Rounded corners */ +} +header img { + position: absolute; + top: 0; + right: 0; + width: 50%; + height: auto; + opacity: 0.2; /* Slightly faded for a background effect */ +} +.container { + margin: 20px auto; + background: #fff; + padding: 20px; + border-radius: 8px; + box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); +} +h1 { + margin: 0; + font-size: 2.5em; +} +h2 { + margin-top: 20px; + font-size: 1.8em; +} +#messages { + margin-top: 20px; + max-height: 400px; + overflow-y: auto; + border: 1px solid #ddd; + border-radius: 4px; + padding: 10px; + background: #f9f9f9; +} +.message { + padding: 10px; + border-bottom: 1px solid #eee; +} +.message:last-child { + border-bottom: none; +} +.footer { + margin-top: 20px; + text-align: center; + color: #666; +} diff --git a/src/metrics/templates/home.html b/src/metrics/templates/home.html new file mode 100644 index 0000000..84f9ef8 --- /dev/null +++ b/src/metrics/templates/home.html @@ -0,0 +1,36 @@ +{{ define "home" }} + + +
+ + +