| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package prometheus // import "go.opentelemetry.io/otel/exporters/prometheus" |
| 5 |
|
| 6 |
import ( |
| 7 |
"github.com/prometheus/client_golang/prometheus" |
| 8 |
"github.com/prometheus/otlptranslator" |
| 9 |
|
| 10 |
"go.opentelemetry.io/otel/attribute" |
| 11 |
"go.opentelemetry.io/otel/sdk/metric" |
| 12 |
) |
| 13 |
|
| 14 |
// config contains options for the exporter. |
| 15 |
type config struct { |
| 16 |
registerer prometheus.Registerer |
| 17 |
disableTargetInfo bool |
| 18 |
translationStrategy otlptranslator.TranslationStrategyOption |
| 19 |
withoutUnits bool |
| 20 |
withoutCounterSuffixes bool |
| 21 |
readerOpts []metric.ManualReaderOption |
| 22 |
disableScopeInfo bool |
| 23 |
namespace string |
| 24 |
resourceAttributesFilter attribute.Filter |
| 25 |
} |
| 26 |
|
| 27 |
// newConfig creates a validated config configured with options. |
| 28 |
func newConfig(opts ...Option) config { |
| 29 |
cfg := config{} |
| 30 |
for _, opt := range opts { |
| 31 |
cfg = opt.apply(cfg) |
| 32 |
} |
| 33 |
|
| 34 |
if cfg.translationStrategy == "" { |
| 35 |
cfg.translationStrategy = otlptranslator.UnderscoreEscapingWithSuffixes |
| 36 |
} else if !cfg.translationStrategy.ShouldAddSuffixes() { |
| 37 |
// Note, if the translation strategy implies that suffixes should be added, |
| 38 |
// the user can still use WithoutUnits and WithoutCounterSuffixes to |
| 39 |
// explicitly disable specific suffixes. We do not override their preference |
| 40 |
// in this case. However if the chosen strategy disables suffixes, we should |
| 41 |
// forcibly disable all of them. |
| 42 |
cfg.withoutCounterSuffixes = true |
| 43 |
cfg.withoutUnits = true |
| 44 |
} |
| 45 |
|
| 46 |
if cfg.registerer == nil { |
| 47 |
cfg.registerer = prometheus.DefaultRegisterer |
| 48 |
} |
| 49 |
|
| 50 |
return cfg |
| 51 |
} |
| 52 |
|
| 53 |
// Option sets exporter option values. |
| 54 |
type Option interface { |
| 55 |
apply(config) config |
| 56 |
} |
| 57 |
|
| 58 |
type optionFunc func(config) config |
| 59 |
|
| 60 |
func (fn optionFunc) apply(cfg config) config { |
| 61 |
return fn(cfg) |
| 62 |
} |
| 63 |
|
| 64 |
// WithRegisterer configures which prometheus Registerer the Exporter will |
| 65 |
// register with. If no registerer is used the prometheus DefaultRegisterer is |
| 66 |
// used. |
| 67 |
func WithRegisterer(reg prometheus.Registerer) Option { |
| 68 |
return optionFunc(func(cfg config) config { |
| 69 |
cfg.registerer = reg |
| 70 |
return cfg |
| 71 |
}) |
| 72 |
} |
| 73 |
|
| 74 |
// WithAggregationSelector configure the Aggregation Selector the exporter will |
| 75 |
// use. If no AggregationSelector is provided the DefaultAggregationSelector is |
| 76 |
// used. |
| 77 |
func WithAggregationSelector(agg metric.AggregationSelector) Option { |
| 78 |
return optionFunc(func(cfg config) config { |
| 79 |
cfg.readerOpts = append(cfg.readerOpts, metric.WithAggregationSelector(agg)) |
| 80 |
return cfg |
| 81 |
}) |
| 82 |
} |
| 83 |
|
| 84 |
// WithProducer configure the metric Producer the exporter will use as a source |
| 85 |
// of external metric data. |
| 86 |
func WithProducer(producer metric.Producer) Option { |
| 87 |
return optionFunc(func(cfg config) config { |
| 88 |
cfg.readerOpts = append(cfg.readerOpts, metric.WithProducer(producer)) |
| 89 |
return cfg |
| 90 |
}) |
| 91 |
} |
| 92 |
|
| 93 |
// WithoutTargetInfo configures the Exporter to not export the resource target_info metric. |
| 94 |
// If not specified, the Exporter will create a target_info metric containing |
| 95 |
// the metrics' resource.Resource attributes. |
| 96 |
func WithoutTargetInfo() Option { |
| 97 |
return optionFunc(func(cfg config) config { |
| 98 |
cfg.disableTargetInfo = true |
| 99 |
return cfg |
| 100 |
}) |
| 101 |
} |
| 102 |
|
| 103 |
// WithTranslationStrategy provides a standardized way to define how metric and |
| 104 |
// label names should be handled during translation to Prometheus format. See: |
| 105 |
// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.48.0/specification/metrics/sdk_exporters/prometheus.md#configuration. |
| 106 |
// The recommended approach is to use either |
| 107 |
// [otlptranslator.UnderscoreEscapingWithSuffixes] for full Prometheus-style |
| 108 |
// compatibility or [otlptranslator.NoTranslation] for OpenTelemetry-style names. |
| 109 |
// |
| 110 |
// By default, if the NameValidationScheme variable in |
| 111 |
// [github.com/prometheus/common/model] is "legacy", the default strategy is |
| 112 |
// [otlptranslator.UnderscoreEscapingWithSuffixes]. If the validation scheme is |
| 113 |
// "utf8", then currently the default Strategy is |
| 114 |
// [otlptranslator.NoUTF8EscapingWithSuffixes]. |
| 115 |
// |
| 116 |
// Notice: It is planned that a future release of this SDK will change the |
| 117 |
// default to always be [otlptranslator.UnderscoreEscapingWithSuffixes] in all |
| 118 |
// circumstances. Users wanting a different translation strategy should specify |
| 119 |
// it explicitly. |
| 120 |
func WithTranslationStrategy(strategy otlptranslator.TranslationStrategyOption) Option { |
| 121 |
return optionFunc(func(cfg config) config { |
| 122 |
cfg.translationStrategy = strategy |
| 123 |
return cfg |
| 124 |
}) |
| 125 |
} |
| 126 |
|
| 127 |
// WithoutUnits disables exporter's addition of unit suffixes to metric names, |
| 128 |
// and will also prevent unit comments from being added in OpenMetrics once |
| 129 |
// unit comments are supported. |
| 130 |
// |
| 131 |
// By default, metric names include a unit suffix to follow Prometheus naming |
| 132 |
// conventions. For example, the counter metric request.duration, with unit |
| 133 |
// milliseconds would become request_duration_milliseconds_total. |
| 134 |
// With this option set, the name would instead be request_duration_total. |
| 135 |
// |
| 136 |
// Can be used in conjunction with [WithTranslationStrategy] to disable unit |
| 137 |
// suffixes in strategies that would otherwise add suffixes, but this behavior |
| 138 |
// is not recommended and may be removed in a future release. |
| 139 |
// |
| 140 |
// Deprecated: Use [WithTranslationStrategy] instead. |
| 141 |
func WithoutUnits() Option { |
| 142 |
return optionFunc(func(cfg config) config { |
| 143 |
cfg.withoutUnits = true |
| 144 |
return cfg |
| 145 |
}) |
| 146 |
} |
| 147 |
|
| 148 |
// WithoutCounterSuffixes disables exporter's addition _total suffixes on |
| 149 |
// counters. |
| 150 |
// |
| 151 |
// By default, metric names include a _total suffix to follow Prometheus naming |
| 152 |
// conventions. For example, the counter metric happy.people would become |
| 153 |
// happy_people_total. With this option set, the name would instead be |
| 154 |
// happy_people. |
| 155 |
// |
| 156 |
// Can be used in conjunction with [WithTranslationStrategy] to disable counter |
| 157 |
// suffixes in strategies that would otherwise add suffixes, but this behavior |
| 158 |
// is not recommended and may be removed in a future release. |
| 159 |
// |
| 160 |
// Deprecated: Use [WithTranslationStrategy] instead. |
| 161 |
func WithoutCounterSuffixes() Option { |
| 162 |
return optionFunc(func(cfg config) config { |
| 163 |
cfg.withoutCounterSuffixes = true |
| 164 |
return cfg |
| 165 |
}) |
| 166 |
} |
| 167 |
|
| 168 |
// WithoutScopeInfo configures the Exporter to not export |
| 169 |
// labels about Instrumentation Scope to all metric points. |
| 170 |
func WithoutScopeInfo() Option { |
| 171 |
return optionFunc(func(cfg config) config { |
| 172 |
cfg.disableScopeInfo = true |
| 173 |
return cfg |
| 174 |
}) |
| 175 |
} |
| 176 |
|
| 177 |
// WithNamespace configures the Exporter to prefix metric with the given |
| 178 |
// namespace. Metadata metrics such as target_info are not prefixed since these |
| 179 |
// have special behavior based on their name. Namespaces will be prepended even |
| 180 |
// if [otlptranslator.NoTranslation] is set as a translation strategy. If the provided namespace |
| 181 |
// is empty, nothing will be prepended to metric names. |
| 182 |
func WithNamespace(ns string) Option { |
| 183 |
return optionFunc(func(cfg config) config { |
| 184 |
cfg.namespace = ns |
| 185 |
return cfg |
| 186 |
}) |
| 187 |
} |
| 188 |
|
| 189 |
// WithResourceAsConstantLabels configures the Exporter to add the resource attributes the |
| 190 |
// resourceFilter returns true for as attributes on all exported metrics. |
| 191 |
// |
| 192 |
// The does not affect the target info generated from resource attributes. |
| 193 |
func WithResourceAsConstantLabels(resourceFilter attribute.Filter) Option { |
| 194 |
return optionFunc(func(cfg config) config { |
| 195 |
cfg.resourceAttributesFilter = resourceFilter |
| 196 |
return cfg |
| 197 |
}) |
| 198 |
} |