Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/contour: Add contour version to metrics #2383

Merged
merged 1 commit into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ RUN go mod download
COPY cmd cmd
COPY internal internal
COPY apis apis
RUN CGO_ENABLED=0 GOOS=linux GOFLAGS=-ldflags=-w go build -o /go/bin/contour -ldflags=-s -v github.com/projectcontour/contour/cmd/contour
COPY Makefile Makefile

ARG BUILD_BRANCH
ARG BUILD_SHA
ARG BUILD_VERSION

RUN make install \
CGO_ENABLED=0 \
GOOS=linux \
BUILD_VERSION=${BUILD_VERSION} \
BUILD_SHA=${BUILD_SHA} \
BUILD_BRANCH=${BUILD_BRANCH}

FROM scratch AS final
COPY --from=build /go/bin/contour /bin/contour
17 changes: 15 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,20 @@ VERSION ?= $(GIT_REF)
# set outside this Makefile, as a safety valve.
LATEST_VERSION ?= NOLATEST

# Sets the current Git sha
BUILD_SHA = $(shell git rev-parse --verify HEAD)
# Sets the current branch
BUILD_BRANCH = $(shell git branch --show-current)
# Sets the current tagged git version
BUILD_VERSION = $(VERSION)

GO_BUILD_VARS = \
github.com/projectcontour/contour/internal/build.Version=${BUILD_VERSION} \
github.com/projectcontour/contour/internal/build.Sha=${BUILD_SHA} \
github.com/projectcontour/contour/internal/build.Branch=${BUILD_BRANCH}

GO_TAGS := -tags "oidc gcp"
GO_LDFLAGS := -s $(patsubst %,-X %, $(GO_BUILD_VARS))

export GO111MODULE=on

Expand All @@ -39,7 +52,7 @@ check: install check-test check-test-race ## Install and run tests
checkall: check lint check-generate

install: ## Build and install the contour binary
go install -mod=readonly -v $(GO_TAGS) $(MODULE)/cmd/contour
go install -mod=readonly -v -ldflags="$(GO_LDFLAGS)" $(GO_TAGS) $(MODULE)/cmd/contour

race:
go install -mod=readonly -v -race $(GO_TAGS) $(MODULE)/cmd/contour
Expand All @@ -48,7 +61,7 @@ download: ## Download Go modules
go mod download

container: ## Build the Contour container image
docker build . -t $(IMAGE):$(VERSION)
docker build --build-arg BUILD_VERSION=$(BUILD_VERSION) --build-arg BUILD_BRANCH=$(BUILD_BRANCH) --build-arg BUILD_SHA=$(BUILD_SHA) . -t $(IMAGE):$(VERSION)

push: ## Push the Contour container image to the Docker registry
push: container
Expand Down
23 changes: 23 additions & 0 deletions internal/build/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright © 2020 VMware
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package build

// Branch allows for a queryable branch name set at build time.
var Branch string

// Sha allows for a queryable git sha set at build time.
var Sha string

// Version allows for a queryable version set at build time.
var Version string
12 changes: 12 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import (

"k8s.io/client-go/kubernetes"

"github.com/projectcontour/contour/internal/build"
"github.com/projectcontour/contour/internal/httpsvc"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// Metrics provide Prometheus metrics for the app
type Metrics struct {
buildInfoGauge *prometheus.GaugeVec
ingressRouteTotalGauge *prometheus.GaugeVec
ingressRouteRootTotalGauge *prometheus.GaugeVec
ingressRouteInvalidGauge *prometheus.GaugeVec
Expand Down Expand Up @@ -64,6 +66,7 @@ type Meta struct {
}

const (
BuildInfoGauge = "contour_build_info"
IngressRouteTotalGauge = "contour_ingressroute_total"
IngressRouteRootTotalGauge = "contour_ingressroute_root_total"
IngressRouteInvalidGauge = "contour_ingressroute_invalid_total"
Expand All @@ -89,6 +92,13 @@ const (
// to regenerate the metrics documentation.
func NewMetrics(registry *prometheus.Registry) *Metrics {
m := Metrics{
buildInfoGauge: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: BuildInfoGauge,
Help: "Build information for Contour. Labels include the branch and git SHA that Contour was built from, and the Contour version.",
},
[]string{"branch", "revision", "version"},
),
poidag-zz marked this conversation as resolved.
Show resolved Hide resolved
ingressRouteMetricCache: &RouteMetric{},
proxyMetricCache: &RouteMetric{},
ingressRouteTotalGauge: prometheus.NewGaugeVec(
Expand Down Expand Up @@ -181,13 +191,15 @@ func NewMetrics(registry *prometheus.Registry) *Metrics {
[]string{"op", "kind"},
),
}
m.buildInfoGauge.WithLabelValues(build.Branch, build.Sha, build.Version).Set(1)
m.register(registry)
return &m
}

// register registers the Metrics with the supplied registry.
func (m *Metrics) register(registry *prometheus.Registry) {
registry.MustRegister(
m.buildInfoGauge,
m.ingressRouteTotalGauge,
m.ingressRouteRootTotalGauge,
m.ingressRouteInvalidGauge,
Expand Down
7 changes: 7 additions & 0 deletions site/_metrics/contour_build_info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
name: 'contour_build_info'
type: '[GAUGE](https://prometheus.io/docs/concepts/metric_types/#gauge)'
labels: 'branch, revision, version'
---

Build information for Contour. Labels include the branch and git SHA that Contour was built from, and the Contour version.