|
| 1 | +// Copyright 2023 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | + |
| 11 | +package logmetrics |
| 12 | + |
| 13 | +import ( |
| 14 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/util/metric" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/util/syncutil" |
| 17 | + "github.com/cockroachdb/errors" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + // logMetricsReg is a singleton instance of the LogMetricsRegistry. |
| 22 | + logMetricsReg = newLogMetricsRegistry() |
| 23 | + FluentSinkConnErrors = metric.Metadata{ |
| 24 | + Name: string(log.FluentSinkConnectionError), |
| 25 | + Help: "Number of connection errors experienced by fluent-server logging sinks", |
| 26 | + Measurement: "fluent-server log sink connection errors", |
| 27 | + Unit: metric.Unit_COUNT, |
| 28 | + } |
| 29 | +) |
| 30 | + |
| 31 | +// logMetricsStruct is a struct used to contain all metrics |
| 32 | +// tracked by the LogMetricsRegistry. This container is necessary |
| 33 | +// to register all the metrics with the Registry internal to the |
| 34 | +// LogMetricsRegistry. |
| 35 | +type logMetricsStruct struct { |
| 36 | + FluentSinkConnErrors *metric.Counter |
| 37 | +} |
| 38 | + |
| 39 | +// LogMetricsRegistry is a log.LogMetrics implementation used in the |
| 40 | +// logging package to give it access to metrics without introducing a |
| 41 | +// circular dependency. |
| 42 | +// |
| 43 | +// All metrics meant to be available to the logging package must be |
| 44 | +// registered at the time of initialization. |
| 45 | +// |
| 46 | +// LogMetricsRegistry is thread-safe. |
| 47 | +type LogMetricsRegistry struct { |
| 48 | + mu struct { |
| 49 | + syncutil.Mutex |
| 50 | + // metricsStruct holds the same metrics as the below structures, but |
| 51 | + // provides an easy way to inject them into metric.Registry's on demand |
| 52 | + // in NewRegistry(). |
| 53 | + metricsStruct logMetricsStruct |
| 54 | + counters map[log.MetricName]*metric.Counter |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +var _ log.LogMetrics = (*LogMetricsRegistry)(nil) |
| 59 | + |
| 60 | +func newLogMetricsRegistry() *LogMetricsRegistry { |
| 61 | + registry := &LogMetricsRegistry{} |
| 62 | + registry.registerCounters() |
| 63 | + return registry |
| 64 | +} |
| 65 | + |
| 66 | +func (l *LogMetricsRegistry) registerCounters() { |
| 67 | + l.mu.Lock() |
| 68 | + defer l.mu.Unlock() |
| 69 | + l.mu.counters = make(map[log.MetricName]*metric.Counter) |
| 70 | + // Create the metrics struct for us to add to registries as they're |
| 71 | + // requested. |
| 72 | + l.mu.metricsStruct = logMetricsStruct{ |
| 73 | + FluentSinkConnErrors: metric.NewCounter(FluentSinkConnErrors), |
| 74 | + } |
| 75 | + // Be sure to also add the metrics to our internal store, for |
| 76 | + // recall in functions such as IncrementCounter. |
| 77 | + l.mu.counters[log.MetricName(FluentSinkConnErrors.Name)] = l.mu.metricsStruct.FluentSinkConnErrors |
| 78 | +} |
| 79 | + |
| 80 | +// NewRegistry initializes and returns a new metric.Registry, populated with metrics |
| 81 | +// tracked by the LogMetricsRegistry. While the metrics tracked by the logmetrics package |
| 82 | +// are global, they may be shared by multiple servers, test servers, etc. Therefore, we |
| 83 | +// need the means to label the metrics separately depending on the server, tenant, etc. |
| 84 | +// serving them. For this reason, we provide the ability to track the same log metrics |
| 85 | +// across multiple registries. |
| 86 | +func NewRegistry() *metric.Registry { |
| 87 | + if logMetricsReg == nil { |
| 88 | + panic(errors.AssertionFailedf("LogMetricsRegistry was not initialized")) |
| 89 | + } |
| 90 | + reg := metric.NewRegistry() |
| 91 | + logMetricsReg.mu.Lock() |
| 92 | + defer logMetricsReg.mu.Unlock() |
| 93 | + reg.AddMetricStruct(logMetricsReg.mu.metricsStruct) |
| 94 | + return reg |
| 95 | +} |
| 96 | + |
| 97 | +// IncrementCounter increments thegi Counter held by the given alias. If a log.MetricName |
| 98 | +// is provided as an argument, but is not registered with the LogMetricsRegistry, this function |
| 99 | +// panics. |
| 100 | +func (l *LogMetricsRegistry) IncrementCounter(metric log.MetricName, amount int64) { |
| 101 | + l.mu.Lock() |
| 102 | + defer l.mu.Unlock() |
| 103 | + counter, ok := l.mu.counters[metric] |
| 104 | + if !ok { |
| 105 | + panic(errors.AssertionFailedf("MetricName not registered in LogMetricsRegistry: %q", string(metric))) |
| 106 | + } |
| 107 | + counter.Inc(amount) |
| 108 | +} |
0 commit comments