From f1f97dbb6137daeba4bb95b3532001d01418c2d9 Mon Sep 17 00:00:00 2001 From: Tobi Okedeji Date: Wed, 3 Jul 2024 19:31:28 +0100 Subject: [PATCH] testing prometheus --- .github/workflows/ecr.yml | 56 +++++++++++++++++++++++++++++++++++++++ cmd/node/main.go | 24 +++++++++++++++++ docker/Dockerfile_worker | 2 +- go.mod | 4 +-- go.sum | 4 +++ 5 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/ecr.yml diff --git a/.github/workflows/ecr.yml b/.github/workflows/ecr.yml new file mode 100644 index 0000000..86fed62 --- /dev/null +++ b/.github/workflows/ecr.yml @@ -0,0 +1,56 @@ +# This workflow will build and push a new container image to Amazon ECR, +# and then will deploy a new task definition to Amazon ECS which will be run by Fargate when a release is created +name: Build and Push docker image to ECR + +on: + push: + branches: + - add-prometheus-metrics + + +concurrency: + group: ${{ github.ref }} + cancel-in-progress: true +jobs: + build-push: + name: Build and Push docker image + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v2 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: us-east-1 + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + + - name: Build, tag, and push image to Amazon ECR + id: build-push-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + ECR_REPOSITORY: worker-prometheus-test + run: | + + IMAGE_TAG="test-latest" + + # compute-node HEADS + ECR_REPOSITORY_HEAD="${ECR_REPOSITORY}-head" + + docker build --pull -f docker/Dockerfile_head \ + --build-arg "GH_TOKEN=${{ secrets.GH_READONLY_PAT }}" \ + -t $ECR_REGISTRY/$ECR_REPOSITORY_HEAD:$IMAGE_TAG . + docker push $ECR_REGISTRY/$ECR_REPOSITORY_HEAD:$IMAGE_TAG + + # Build a docker container and push it to ECR so that it can be deployed to ECS. + # compute-node workers + docker build --pull -f docker/Dockerfile_worker \ + --build-arg "GH_TOKEN=${{ secrets.GH_READONLY_PAT }}" \ + -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG diff --git a/cmd/node/main.go b/cmd/node/main.go index 4256ac8..ded30a2 100644 --- a/cmd/node/main.go +++ b/cmd/node/main.go @@ -33,6 +33,8 @@ import ( "github.com/allora-network/b7s/node" "github.com/allora-network/b7s/peerstore" "github.com/allora-network/b7s/store" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" ) const ( @@ -41,6 +43,17 @@ const ( notFoundValue = -1 ) +var ( + opsProcessed = prometheus.NewCounter(prometheus.CounterOpts{ + Name: "allora_node_operations", + Help: "The total number of processed operations", + }) +) + +func init() { + prometheus.MustRegister(opsProcessed) +} + func main() { os.Exit(run()) } @@ -410,6 +423,8 @@ func (e *AlloraExecutor) ExecuteFunction(requestID string, req execute.Request) result.Result.Stdout = outputJson } } + + opsProcessed.Inc() return result, err } @@ -433,6 +448,15 @@ func run() int { } log = log.Level(level) + // Start HTTP server for Prometheus metrics. + http.Handle("/metrics", promhttp.Handler()) + go func() { + log.Info().Msg("Starting metrics server on :2112") + if err := http.ListenAndServe(":2112", nil); err != nil { + log.Error().Err(err).Str("level", cfg.Log.Level).Msg("could not start metric server") + } + }() + // Determine node role. role, err := parseNodeRole(cfg.Role) if err != nil { diff --git a/docker/Dockerfile_worker b/docker/Dockerfile_worker index 8e94579..78d0d02 100644 --- a/docker/Dockerfile_worker +++ b/docker/Dockerfile_worker @@ -80,6 +80,6 @@ RUN groupadd -g 1001 ${USERNAME} \ USER ${USERNAME} -EXPOSE 8080 9527 +EXPOSE 8080 9527 2112 ENTRYPOINT ["allora-node"] diff --git a/go.mod b/go.mod index 78a4815..672fd39 100644 --- a/go.mod +++ b/go.mod @@ -198,9 +198,9 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/polydawn/refmt v0.89.0 // indirect - github.com/prometheus/client_golang v1.18.0 // indirect + github.com/prometheus/client_golang v1.19.1 // indirect github.com/prometheus/client_model v0.6.0 // indirect - github.com/prometheus/common v0.47.0 // indirect + github.com/prometheus/common v0.48.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect github.com/quic-go/qpack v0.4.0 // indirect github.com/quic-go/quic-go v0.41.0 // indirect diff --git a/go.sum b/go.sum index f5e3994..36a8bb7 100644 --- a/go.sum +++ b/go.sum @@ -797,6 +797,8 @@ github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3O github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk= github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA= +github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE= +github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= @@ -814,6 +816,8 @@ github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB8 github.com/prometheus/common v0.15.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16Clt/msog/s= github.com/prometheus/common v0.47.0 h1:p5Cz0FNHo7SnWOmWmoRozVcjEp0bIVU8cV7OShpjL1k= github.com/prometheus/common v0.47.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc= +github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE= +github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc= github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=