Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Add metrics-registrar service for registering metrics (#2199)
Browse files Browse the repository at this point in the history
* Add metrics-registrar service for registering metrics

This adds a new service that registers metrics on boot. It also removes metrics registration from the other services boot sequences. This service is deployed but inaccessible and purely exists to register metrics.

* wat
  • Loading branch information
sethvargo authored Aug 18, 2021
1 parent fdd3b90 commit 41107ee
Show file tree
Hide file tree
Showing 15 changed files with 628 additions and 31 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,21 @@ jobs:
set +o pipefail
YEAR=$(date +"%Y")
find . -type f -name '*.go' -exec awk 'FNR==1{if ($0!~"Copyright '"${YEAR}"'") print FILENAME ":1:missing copyright or invalid copyright year";}' '{}' + | \
find . -type f -name '*.go' -exec awk 'FNR==1{if ($0!~"Copyright '"${YEAR}"'" && $0!~"Code generated") print FILENAME ":1:missing copyright or invalid copyright year";}' '{}' + | \
reviewdog -efm="%f:%l:%m" \
-name="copyright-check" \
-reporter="github-pr-check" \
-filter-mode="diff_context" \
-fail-on-error="true" \
-level="error"
- name: generate-check
shell: bash
run: |-
set -eEu
set +o pipefail
make generate-check
- name: tab-check
shell: bash
env:
Expand All @@ -167,7 +174,6 @@ jobs:
set -eEu
set +o pipefail
YEAR=$(date +"%Y")
make tabcheck | \
reviewdog -efm="%f:%l:%m" \
-name="tab-check" \
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ HTML_FILES = $(shell find . -name \*.html)
GO_FILES = $(shell find . -name \*.go)
MD_FILES = $(shell find . -name \*.md)

generate:
@go generate ./...
.PHONY: generate

generate-check: generate
@git update-index --refresh && git diff-index --quiet HEAD --
.PHONY: generate-check

# lint uses the same linter as CI and tries to report the same results running
# locally. There is a chance that CI detects linter errors that are not found
# locally, but it should be rare.
Expand Down
38 changes: 38 additions & 0 deletions builders/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,44 @@ steps:
waitFor:
- 'push-migrate'

#
# metrics-registrar
#
- id: 'dockerize-metrics-registrar'
name: 'docker:19'
args:
- 'build'
- '--file=builders/metrics-registrar.dockerfile'
- '--tag=gcr.io/${PROJECT_ID}/${_REPO}/metrics-registrar:${_TAG}'
- '.'
waitFor:
- 'build'

- id: 'push-metrics-registrar'
name: 'docker:19'
args:
- 'push'
- 'gcr.io/${PROJECT_ID}/${_REPO}/metrics-registrar:${_TAG}'
waitFor:
- 'dockerize-metrics-registrar'

- id: 'attest-metrics-registrar'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:335.0.0'
args:
- 'bash'
- '-eEuo'
- 'pipefail'
- '-c'
- |-
ARTIFACT_URL=$(docker inspect gcr.io/${PROJECT_ID}/${_REPO}/metrics-registrar:${_TAG} --format='{{index .RepoDigests 0}}')
gcloud beta container binauthz attestations sign-and-create \
--project "${PROJECT_ID}" \
--artifact-url "$${ARTIFACT_URL}" \
--attestor "${_BINAUTHZ_ATTESTOR}" \
--keyversion "${_BINAUTHZ_KEY_VERSION}"
waitFor:
- 'push-metrics-registrar'


#
# modeler
Expand Down
23 changes: 23 additions & 0 deletions builders/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,29 @@ steps:
waitFor:
- '-'


#
# metrics-registrar
#
- id: 'deploy-metrics-registrar'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:335.0.0-alpine'
args:
- 'bash'
- '-eEuo'
- 'pipefail'
- '-c'
- |-
gcloud run deploy "metrics-registrar" \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--image "gcr.io/${PROJECT_ID}/${_REPO}/metrics-registrar:${_TAG}" \
--no-traffic
waitFor:
- '-'


#
# modeler
#
Expand Down
20 changes: 20 additions & 0 deletions builders/promote.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,26 @@ steps:
waitFor:
- '-'

#
# metrics-registrar
#
- id: 'promote-metrics-registrar'
name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:335.0.0-alpine'
args:
- 'bash'
- '-eEuo'
- 'pipefail'
- '-c'
- |-
gcloud run services update-traffic "metrics-registrar" \
--quiet \
--project "${PROJECT_ID}" \
--platform "managed" \
--region "${_REGION}" \
--to-revisions "${_REVISION}=${_PERCENTAGE}"
waitFor:
- '-'

#
# rotation
#
Expand Down
120 changes: 120 additions & 0 deletions cmd/metrics-registrar/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright 2021 the Exposure Notifications Verification Server authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This server registers metrics for Stackdriver.
package main

import (
"context"
"fmt"
"net/http"
"os/signal"
"syscall"

"github.com/google/exposure-notifications-verification-server/internal/buildinfo"
"github.com/google/exposure-notifications-verification-server/pkg/config"
"github.com/google/exposure-notifications-verification-server/pkg/controller/metricsregistrar"
"github.com/google/exposure-notifications-verification-server/pkg/controller/middleware"
"github.com/google/exposure-notifications-verification-server/pkg/render"

"github.com/google/exposure-notifications-server/pkg/logging"
"github.com/google/exposure-notifications-server/pkg/observability"
"github.com/google/exposure-notifications-server/pkg/server"

"github.com/gorilla/mux"
)

func main() {
ctx, done := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)

logger := logging.NewLoggerFromEnv().
With("build_id", buildinfo.BuildID).
With("build_tag", buildinfo.BuildTag)
ctx = logging.WithLogger(ctx, logger)

defer func() {
done()
if r := recover(); r != nil {
logger.Fatalw("application panic", "panic", r)
}
}()

err := realMain(ctx)
done()

if err != nil {
logger.Fatal(err)
}
logger.Info("successful shutdown")
}

func realMain(ctx context.Context) error {
logger := logging.FromContext(ctx)

cfg, err := config.NewMetricsRegistrarConfig(ctx)
if err != nil {
return fmt.Errorf("failed to process config: %w", err)
}

// Setup monitoring
logger.Info("configuring observability exporter")
oeConfig := cfg.ObservabilityExporterConfig()
oe, err := observability.NewFromEnv(oeConfig)
if err != nil {
return fmt.Errorf("unable to create ObservabilityExporter provider: %w", err)
}
if err := oe.StartExporter(ctx); err != nil {
return fmt.Errorf("error initializing observability exporter: %w", err)
}
defer oe.Close()
ctx, obs := middleware.WithObservability(ctx)
logger.Infow("observability exporter", "config", oeConfig)

// Create the renderer
h, err := render.New(ctx, nil, cfg.DevMode)
if err != nil {
return fmt.Errorf("failed to create renderer: %w", err)
}

// Create the router
r := mux.NewRouter()

// Common observability context
r.Use(obs)

// Request ID injection
populateRequestID := middleware.PopulateRequestID(h)
r.Use(populateRequestID)

// Logger injection
populateLogger := middleware.PopulateLogger(logger)
r.Use(populateLogger)

// Recovery injection
recovery := middleware.Recovery(h)
r.Use(recovery)

metricsRegistrarController := metricsregistrar.New(cfg, h)
if err := metricsRegistrarController.CreateMetrics(ctx); err != nil {
return fmt.Errorf("failed to create metrics: %w", err)
}
r.Handle("/", metricsRegistrarController.HandleRoot()).Methods(http.MethodGet)

srv, err := server.New(cfg.Port)
if err != nil {
return fmt.Errorf("failed to create server: %w", err)
}
logger.Infow("server listening", "port", cfg.Port)
return srv.ServeHTTPHandler(ctx, r)
}
18 changes: 11 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ module github.com/google/exposure-notifications-verification-server
go 1.16

require (
cloud.google.com/go v0.91.1
cloud.google.com/go v0.93.3 // indirect
cloud.google.com/go/firestore v1.5.0 // indirect
cloud.google.com/go/kms v0.1.0 // indirect
cloud.google.com/go/monitoring v0.1.0
cloud.google.com/go/secretmanager v0.1.0
cloud.google.com/go/trace v0.1.0 // indirect
contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200615190824-f8c219d2d895 // indirect
contrib.go.opencensus.io/integrations/ocsql v0.1.7
firebase.google.com/go v3.13.0+incompatible
github.com/Azure/azure-sdk-for-go v56.2.0+incompatible // indirect
github.com/NYTimes/gziphandler v1.1.1
github.com/aws/aws-sdk-go v1.40.22 // indirect
github.com/aws/aws-sdk-go v1.40.24 // indirect
github.com/chromedp/cdproto v0.0.0-20210808225517-c36c1bd4c35e
github.com/chromedp/chromedp v0.7.4
github.com/dustin/go-humanize v1.0.0
Expand All @@ -25,7 +29,7 @@ require (
github.com/gonum/internal v0.0.0-20181124074243-f884aa714029 // indirect
github.com/gonum/lapack v0.0.0-20181123203213-e4cdc5a0bff9 // indirect
github.com/gonum/matrix v0.0.0-20181209220409-c518dec07be9
github.com/google/exposure-notifications-server v0.35.0
github.com/google/exposure-notifications-server v0.35.1-0.20210818171456-e90a92bf7dd0
github.com/google/go-cmp v0.5.6
github.com/google/uuid v1.3.0
github.com/gorilla/handlers v1.5.1
Expand All @@ -48,7 +52,7 @@ require (
github.com/opencensus-integrations/redigo v2.0.1+incompatible
github.com/ory/dockertest v3.3.5+incompatible
github.com/prometheus/common v0.30.0 // indirect
github.com/prometheus/procfs v0.7.2 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rakutentech/jwk-go v1.0.1
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0
Expand All @@ -61,12 +65,12 @@ require (
github.com/unrolled/secure v1.0.9
go.opencensus.io v0.23.0
go.uber.org/zap v1.19.0
golang.org/x/crypto v0.0.0-20210813211128-0a44fdfbc16e // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/mod v0.5.0 // indirect
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d
golang.org/x/oauth2 v0.0.0-20210810183815-faf39c7919d5 // indirect
golang.org/x/oauth2 v0.0.0-20210817223510-7df4dd6e12ab // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71 // indirect
golang.org/x/sys v0.0.0-20210818153620-00dd8d7831e7 // indirect
golang.org/x/text v0.3.7
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
golang.org/x/tools v0.1.5
Expand Down
Loading

0 comments on commit 41107ee

Please sign in to comment.