Skip to content

Commit f0c8a85

Browse files
authored
[baseserver] Emit metrics for logs produced by level ENG-349 (#19063)
* [baseserver] Emit metrics for logs produced by level * fix * fix * fix * fix * Fix * Fix * fix * retest * fix
1 parent 64852fa commit f0c8a85

File tree

21 files changed

+311
-26
lines changed

21 files changed

+311
-26
lines changed

components/common-go/baseserver/server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"syscall"
1717

1818
common_grpc "github.com/gitpod-io/gitpod/common-go/grpc"
19+
"github.com/gitpod-io/gitpod/common-go/log"
1920
"github.com/gitpod-io/gitpod/common-go/pprof"
2021
"github.com/gitpod-io/gitpod/common-go/tracing"
2122
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
@@ -348,6 +349,10 @@ func (s *Server) initializeMetrics() error {
348349
return fmt.Errorf("failed to register baseserver metrics: %w", err)
349350
}
350351

352+
if err := s.MetricsRegistry().Register(log.DefaultMetrics); err != nil {
353+
return fmt.Errorf("failed to register log metrics: %w", err)
354+
}
355+
351356
reportServerVersion(s.options.version)
352357

353358
return nil

components/common-go/log/log.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func Init(service, version string, json, verbose bool) {
5454
Log = log.WithFields(ServiceContext(service, version))
5555
log.SetReportCaller(true)
5656

57+
log.AddHook(NewLogHook(DefaultMetrics))
58+
5759
if json {
5860
Log.Logger.SetFormatter(newGcpFormatter(false))
5961
} else {

components/common-go/log/metrics.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License.AGPL.txt in the project root for license information.
4+
5+
package log
6+
7+
import (
8+
"github.com/prometheus/client_golang/prometheus"
9+
"github.com/sirupsen/logrus"
10+
)
11+
12+
var (
13+
DefaultMetrics = NewMetrics()
14+
)
15+
16+
type Metrics struct {
17+
logEmitedCounter *prometheus.CounterVec
18+
}
19+
20+
func (m *Metrics) ReportLog(level logrus.Level) {
21+
m.logEmitedCounter.WithLabelValues(level.String()).Inc()
22+
}
23+
24+
func NewMetrics() *Metrics {
25+
return &Metrics{
26+
logEmitedCounter: prometheus.NewCounterVec(prometheus.CounterOpts{
27+
Name: "gitpod_logs_total",
28+
Help: "Total number of logs produced by level",
29+
}, []string{"level"}),
30+
}
31+
}
32+
33+
// Describe sends the super-set of all possible descriptors of metrics
34+
// collected by this Collector to the provided channel and returns once
35+
// the last descriptor has been sent.
36+
func (m *Metrics) Describe(ch chan<- *prometheus.Desc) {
37+
m.logEmitedCounter.Describe(ch)
38+
}
39+
40+
// Collect is called by the Prometheus registry when collecting
41+
// metrics. The implementation sends each collected metric via the
42+
// provided channel and returns once the last metric has been sent.
43+
func (m *Metrics) Collect(ch chan<- prometheus.Metric) {
44+
m.logEmitedCounter.Collect(ch)
45+
}
46+
47+
func NewLogHook(metrics *Metrics) *LogHook {
48+
return &LogHook{metrics: metrics}
49+
}
50+
51+
type LogHook struct {
52+
metrics *Metrics
53+
}
54+
55+
func (h *LogHook) Levels() []logrus.Level {
56+
return logrus.AllLevels
57+
}
58+
59+
func (h *LogHook) Fire(entry *logrus.Entry) error {
60+
h.metrics.ReportLog(entry.Level)
61+
return nil
62+
}

components/gitpod-cli/go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,21 @@ require (
3030
)
3131

3232
require (
33+
github.com/beorn7/perks v1.0.1 // indirect
34+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
3335
github.com/gitpod-io/gitpod/components/scrubber v0.0.0-00010101000000-000000000000 // indirect
3436
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
3537
github.com/hashicorp/golang-lru v1.0.2 // indirect
3638
github.com/mattn/go-colorable v0.0.9 // indirect
3739
github.com/mattn/go-isatty v0.0.3 // indirect
40+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
3841
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
3942
github.com/mitchellh/reflectwalk v1.0.2 // indirect
4043
github.com/onsi/ginkgo v1.16.5 // indirect
4144
github.com/onsi/gomega v1.24.2 // indirect
45+
github.com/prometheus/client_golang v1.16.0 // indirect
46+
github.com/prometheus/client_model v0.3.0 // indirect
47+
github.com/prometheus/common v0.42.0 // indirect
4248
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
4349
golang.org/x/sys v0.11.0 // indirect
4450
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect

components/gitpod-cli/go.sum

Lines changed: 20 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/gitpod-db/go/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ require (
2020
)
2121

2222
require (
23+
github.com/beorn7/perks v1.0.1 // indirect
24+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
2325
github.com/davecgh/go-spew v1.1.1 // indirect
2426
github.com/gitpod-io/gitpod/components/scrubber v0.0.0-00010101000000-000000000000 // indirect
2527
github.com/go-logr/logr v1.2.4 // indirect
@@ -29,8 +31,13 @@ require (
2931
github.com/hashicorp/golang-lru v1.0.2 // indirect
3032
github.com/jinzhu/inflection v1.0.0 // indirect
3133
github.com/jinzhu/now v1.1.5 // indirect
34+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
3235
github.com/mitchellh/reflectwalk v1.0.2 // indirect
3336
github.com/pmezard/go-difflib v1.0.0 // indirect
37+
github.com/prometheus/client_golang v1.16.0 // indirect
38+
github.com/prometheus/client_model v0.3.0 // indirect
39+
github.com/prometheus/common v0.42.0 // indirect
40+
github.com/prometheus/procfs v0.10.1 // indirect
3441
go.opentelemetry.io/otel v1.16.0 // indirect
3542
go.opentelemetry.io/otel/metric v1.16.0 // indirect
3643
go.opentelemetry.io/otel/trace v1.16.0 // indirect

components/gitpod-db/go/go.sum

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/ide/code/codehelper/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ require (
1414
require golang.org/x/sys v0.11.0 // indirect
1515

1616
require (
17+
github.com/beorn7/perks v1.0.1 // indirect
1718
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
19+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
1820
github.com/gitpod-io/gitpod/components/scrubber v0.0.0-00010101000000-000000000000 // indirect
1921
github.com/golang/mock v1.6.0 // indirect
2022
github.com/golang/protobuf v1.5.3 // indirect
@@ -23,7 +25,13 @@ require (
2325
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 // indirect
2426
github.com/hashicorp/golang-lru v1.0.2 // indirect
2527
github.com/kr/text v0.2.0 // indirect
28+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
2629
github.com/mitchellh/reflectwalk v1.0.2 // indirect
30+
github.com/prometheus/client_golang v1.16.0 // indirect
31+
github.com/prometheus/client_model v0.3.0 // indirect
32+
github.com/prometheus/common v0.42.0 // indirect
33+
github.com/prometheus/procfs v0.10.1 // indirect
34+
github.com/rogpeppe/go-internal v1.11.0 // indirect
2735
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37 // indirect
2836
golang.org/x/net v0.8.0 // indirect
2937
golang.org/x/text v0.8.0 // indirect

0 commit comments

Comments
 (0)