summaryrefslogtreecommitdiff
path: root/vendor/go.opentelemetry.io/otel/exporters/prometheus
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/go.opentelemetry.io/otel/exporters/prometheus')
-rw-r--r--vendor/go.opentelemetry.io/otel/exporters/prometheus/config.go78
-rw-r--r--vendor/go.opentelemetry.io/otel/exporters/prometheus/exporter.go136
2 files changed, 164 insertions, 50 deletions
diff --git a/vendor/go.opentelemetry.io/otel/exporters/prometheus/config.go b/vendor/go.opentelemetry.io/otel/exporters/prometheus/config.go
index 4757b793d..dc3542637 100644
--- a/vendor/go.opentelemetry.io/otel/exporters/prometheus/config.go
+++ b/vendor/go.opentelemetry.io/otel/exporters/prometheus/config.go
@@ -7,6 +7,8 @@ import (
"sync"
"github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/common/model"
+ "github.com/prometheus/otlptranslator"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/internal/global"
@@ -17,6 +19,7 @@ import (
type config struct {
registerer prometheus.Registerer
disableTargetInfo bool
+ translationStrategy otlptranslator.TranslationStrategyOption
withoutUnits bool
withoutCounterSuffixes bool
readerOpts []metric.ManualReaderOption
@@ -25,9 +28,9 @@ type config struct {
resourceAttributesFilter attribute.Filter
}
-var logDeprecatedLegacyScheme = sync.OnceFunc(func() {
+var logTemporaryDefault = sync.OnceFunc(func() {
global.Warn(
- "prometheus exporter legacy scheme deprecated: support for the legacy NameValidationScheme will be removed in a future release",
+ "The default Prometheus naming translation strategy is planned to be changed from otlptranslator.NoUTF8EscapingWithSuffixes to otlptranslator.UnderscoreEscapingWithSuffixes in a future release. Add prometheus.WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes) to preserve the existing behavior, or prometheus.WithTranslationStrategy(otlptranslator.UnderscoreEscapingWithSuffixes) to opt into the future default behavior.",
)
})
@@ -38,6 +41,30 @@ func newConfig(opts ...Option) config {
cfg = opt.apply(cfg)
}
+ if cfg.translationStrategy == "" {
+ // If no translation strategy was specified, deduce one based on the global
+ // NameValidationScheme. NOTE: this logic will change in the future, always
+ // defaulting to UnderscoreEscapingWithSuffixes
+
+ //nolint:staticcheck // NameValidationScheme is deprecated but we still need it for now.
+ if model.NameValidationScheme == model.UTF8Validation {
+ logTemporaryDefault()
+ cfg.translationStrategy = otlptranslator.NoUTF8EscapingWithSuffixes
+ } else {
+ cfg.translationStrategy = otlptranslator.UnderscoreEscapingWithSuffixes
+ }
+ } else {
+ // Note, if the translation strategy implies that suffixes should be added,
+ // the user can still use WithoutUnits and WithoutCounterSuffixes to
+ // explicitly disable specific suffixes. We do not override their preference
+ // in this case. However if the chosen strategy disables suffixes, we should
+ // forcibly disable all of them.
+ if !cfg.translationStrategy.ShouldAddSuffixes() {
+ cfg.withoutCounterSuffixes = true
+ cfg.withoutUnits = true
+ }
+ }
+
if cfg.registerer == nil {
cfg.registerer = prometheus.DefaultRegisterer
}
@@ -95,6 +122,30 @@ func WithoutTargetInfo() Option {
})
}
+// WithTranslationStrategy provides a standardized way to define how metric and
+// label names should be handled during translation to Prometheus format. See:
+// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.48.0/specification/metrics/sdk_exporters/prometheus.md#configuration.
+// The recommended approach is to use either
+// [otlptranslator.UnderscoreEscapingWithSuffixes] for full Prometheus-style
+// compatibility or [otlptranslator.NoTranslation] for OpenTelemetry-style names.
+//
+// By default, if the NameValidationScheme variable in
+// [github.com/prometheus/common/model] is "legacy", the default strategy is
+// [otlptranslator.UnderscoreEscapingWithSuffixes]. If the validation scheme is
+// "utf8", then currently the default Strategy is
+// [otlptranslator.NoUTF8EscapingWithSuffixes].
+//
+// Notice: It is planned that a future release of this SDK will change the
+// default to always be [otlptranslator.UnderscoreEscapingWithSuffixes] in all
+// circumstances. Users wanting a different translation strategy should specify
+// it explicitly.
+func WithTranslationStrategy(strategy otlptranslator.TranslationStrategyOption) Option {
+ return optionFunc(func(cfg config) config {
+ cfg.translationStrategy = strategy
+ return cfg
+ })
+}
+
// WithoutUnits disables exporter's addition of unit suffixes to metric names,
// and will also prevent unit comments from being added in OpenMetrics once
// unit comments are supported.
@@ -103,6 +154,12 @@ func WithoutTargetInfo() Option {
// conventions. For example, the counter metric request.duration, with unit
// milliseconds would become request_duration_milliseconds_total.
// With this option set, the name would instead be request_duration_total.
+//
+// Can be used in conjunction with [WithTranslationStrategy] to disable unit
+// suffixes in strategies that would otherwise add suffixes, but this behavior
+// is not recommended and may be removed in a future release.
+//
+// Deprecated: Use [WithTranslationStrategy] instead.
func WithoutUnits() Option {
return optionFunc(func(cfg config) config {
cfg.withoutUnits = true
@@ -110,12 +167,19 @@ func WithoutUnits() Option {
})
}
-// WithoutCounterSuffixes disables exporter's addition _total suffixes on counters.
+// WithoutCounterSuffixes disables exporter's addition _total suffixes on
+// counters.
//
// By default, metric names include a _total suffix to follow Prometheus naming
// conventions. For example, the counter metric happy.people would become
// happy_people_total. With this option set, the name would instead be
// happy_people.
+//
+// Can be used in conjunction with [WithTranslationStrategy] to disable counter
+// suffixes in strategies that would otherwise add suffixes, but this behavior
+// is not recommended and may be removed in a future release.
+//
+// Deprecated: Use [WithTranslationStrategy] instead.
func WithoutCounterSuffixes() Option {
return optionFunc(func(cfg config) config {
cfg.withoutCounterSuffixes = true
@@ -132,9 +196,11 @@ func WithoutScopeInfo() Option {
})
}
-// WithNamespace configures the Exporter to prefix metric with the given namespace.
-// Metadata metrics such as target_info are not prefixed since these
-// have special behavior based on their name.
+// WithNamespace configures the Exporter to prefix metric with the given
+// namespace. Metadata metrics such as target_info are not prefixed since these
+// have special behavior based on their name. Namespaces will be prepended even
+// if [otlptranslator.NoTranslation] is set as a translation strategy. If the provided namespace
+// is empty, nothing will be prepended to metric names.
func WithNamespace(ns string) Option {
return optionFunc(func(cfg config) config {
cfg.namespace = ns
diff --git a/vendor/go.opentelemetry.io/otel/exporters/prometheus/exporter.go b/vendor/go.opentelemetry.io/otel/exporters/prometheus/exporter.go
index 9f3e5414a..0f29c0abb 100644
--- a/vendor/go.opentelemetry.io/otel/exporters/prometheus/exporter.go
+++ b/vendor/go.opentelemetry.io/otel/exporters/prometheus/exporter.go
@@ -15,7 +15,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
- "github.com/prometheus/common/model"
"github.com/prometheus/otlptranslator"
"google.golang.org/protobuf/proto"
@@ -37,7 +36,7 @@ const (
)
var metricsPool = sync.Pool{
- New: func() interface{} {
+ New: func() any {
return &metricdata.ResourceMetrics{}
},
}
@@ -49,7 +48,7 @@ type Exporter struct {
}
// MarshalLog returns logging data about the Exporter.
-func (e *Exporter) MarshalLog() interface{} {
+func (e *Exporter) MarshalLog() any {
const t = "Prometheus exporter"
if r, ok := e.Reader.(*metric.ManualReader); ok {
@@ -104,12 +103,18 @@ func New(opts ...Option) (*Exporter, error) {
// TODO (#3244): Enable some way to configure the reader, but not change temporality.
reader := metric.NewManualReader(cfg.readerOpts...)
- utf8Allowed := model.NameValidationScheme == model.UTF8Validation // nolint:staticcheck // We need this check to keep supporting the legacy scheme.
- if !utf8Allowed {
- // Only sanitize if prometheus does not support UTF-8.
- logDeprecatedLegacyScheme()
+ labelNamer := otlptranslator.LabelNamer{UTF8Allowed: !cfg.translationStrategy.ShouldEscape()}
+ escapedNamespace := cfg.namespace
+ if escapedNamespace != "" {
+ var err error
+ // If the namespace needs to be escaped, do that now when creating the new
+ // Collector object. The escaping is not persisted in the Config itself.
+ escapedNamespace, err = labelNamer.Build(escapedNamespace)
+ if err != nil {
+ return nil, err
+ }
}
- labelNamer := otlptranslator.LabelNamer{UTF8Allowed: utf8Allowed}
+
collector := &collector{
reader: reader,
disableTargetInfo: cfg.disableTargetInfo,
@@ -117,18 +122,11 @@ func New(opts ...Option) (*Exporter, error) {
withoutCounterSuffixes: cfg.withoutCounterSuffixes,
disableScopeInfo: cfg.disableScopeInfo,
metricFamilies: make(map[string]*dto.MetricFamily),
- namespace: labelNamer.Build(cfg.namespace),
+ namespace: escapedNamespace,
resourceAttributesFilter: cfg.resourceAttributesFilter,
- metricNamer: otlptranslator.MetricNamer{
- Namespace: cfg.namespace,
- // We decide whether to pass type and unit to the netricNamer based
- // on whether units or counter suffixes are enabled, and keep this
- // always enabled.
- WithMetricSuffixes: true,
- UTF8Allowed: utf8Allowed,
- },
- unitNamer: otlptranslator.UnitNamer{UTF8Allowed: utf8Allowed},
- labelNamer: labelNamer,
+ metricNamer: otlptranslator.NewMetricNamer(escapedNamespace, cfg.translationStrategy),
+ unitNamer: otlptranslator.UnitNamer{UTF8Allowed: !cfg.translationStrategy.ShouldEscape()},
+ labelNamer: labelNamer,
}
if err := cfg.registerer.Register(collector); err != nil {
@@ -143,7 +141,7 @@ func New(opts ...Option) (*Exporter, error) {
}
// Describe implements prometheus.Collector.
-func (c *collector) Describe(ch chan<- *prometheus.Desc) {
+func (*collector) Describe(chan<- *prometheus.Desc) {
// The Opentelemetry SDK doesn't have information on which will exist when the collector
// is registered. By returning nothing we are an "unchecked" collector in Prometheus,
// and assume responsibility for consistency of the metrics produced.
@@ -197,7 +195,11 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
}
if c.resourceAttributesFilter != nil && len(c.resourceKeyVals.keys) == 0 {
- c.createResourceAttributes(metrics.Resource)
+ err := c.createResourceAttributes(metrics.Resource)
+ if err != nil {
+ otel.Handle(err)
+ return
+ }
}
for _, scopeMetrics := range metrics.ScopeMetrics {
@@ -211,7 +213,11 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
kv.keys = append(kv.keys, scopeNameLabel, scopeVersionLabel, scopeSchemaLabel)
kv.vals = append(kv.vals, scopeMetrics.Scope.Name, scopeMetrics.Scope.Version, scopeMetrics.Scope.SchemaURL)
- attrKeys, attrVals := getAttrs(scopeMetrics.Scope.Attributes, c.labelNamer)
+ attrKeys, attrVals, err := getAttrs(scopeMetrics.Scope.Attributes, c.labelNamer)
+ if err != nil {
+ otel.Handle(err)
+ continue
+ }
for i := range attrKeys {
attrKeys[i] = scopeLabelPrefix + attrKeys[i]
}
@@ -227,7 +233,13 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
if typ == nil {
continue
}
- name := c.getName(m)
+ name, err := c.getName(m)
+ if err != nil {
+ // TODO(#7066): Handle this error better. It's not clear this can be
+ // reached, bad metric names should / will be caught at creation time.
+ otel.Handle(err)
+ continue
+ }
drop, help := c.validateMetrics(name, m.Description, typ)
if drop {
@@ -322,7 +334,11 @@ func addExponentialHistogramMetric[N int64 | float64](
labelNamer otlptranslator.LabelNamer,
) {
for _, dp := range histogram.DataPoints {
- keys, values := getAttrs(dp.Attributes, labelNamer)
+ keys, values, err := getAttrs(dp.Attributes, labelNamer)
+ if err != nil {
+ otel.Handle(err)
+ continue
+ }
keys = append(keys, kv.keys...)
values = append(values, kv.vals...)
@@ -382,8 +398,7 @@ func addExponentialHistogramMetric[N int64 | float64](
otel.Handle(err)
continue
}
-
- // TODO(GiedriusS): add exemplars here after https://github.com/prometheus/client_golang/pull/1654#pullrequestreview-2434669425 is done.
+ m = addExemplars(m, dp.Exemplars, labelNamer)
ch <- m
}
}
@@ -397,7 +412,11 @@ func addHistogramMetric[N int64 | float64](
labelNamer otlptranslator.LabelNamer,
) {
for _, dp := range histogram.DataPoints {
- keys, values := getAttrs(dp.Attributes, labelNamer)
+ keys, values, err := getAttrs(dp.Attributes, labelNamer)
+ if err != nil {
+ otel.Handle(err)
+ continue
+ }
keys = append(keys, kv.keys...)
values = append(values, kv.vals...)
@@ -433,7 +452,11 @@ func addSumMetric[N int64 | float64](
}
for _, dp := range sum.DataPoints {
- keys, values := getAttrs(dp.Attributes, labelNamer)
+ keys, values, err := getAttrs(dp.Attributes, labelNamer)
+ if err != nil {
+ otel.Handle(err)
+ continue
+ }
keys = append(keys, kv.keys...)
values = append(values, kv.vals...)
@@ -461,7 +484,11 @@ func addGaugeMetric[N int64 | float64](
labelNamer otlptranslator.LabelNamer,
) {
for _, dp := range gauge.DataPoints {
- keys, values := getAttrs(dp.Attributes, labelNamer)
+ keys, values, err := getAttrs(dp.Attributes, labelNamer)
+ if err != nil {
+ otel.Handle(err)
+ continue
+ }
keys = append(keys, kv.keys...)
values = append(values, kv.vals...)
@@ -477,7 +504,7 @@ func addGaugeMetric[N int64 | float64](
// getAttrs converts the attribute.Set to two lists of matching Prometheus-style
// keys and values.
-func getAttrs(attrs attribute.Set, labelNamer otlptranslator.LabelNamer) ([]string, []string) {
+func getAttrs(attrs attribute.Set, labelNamer otlptranslator.LabelNamer) ([]string, []string, error) {
keys := make([]string, 0, attrs.Len())
values := make([]string, 0, attrs.Len())
itr := attrs.Iter()
@@ -495,7 +522,11 @@ func getAttrs(attrs attribute.Set, labelNamer otlptranslator.LabelNamer) ([]stri
keysMap := make(map[string][]string)
for itr.Next() {
kv := itr.Attribute()
- key := labelNamer.Build(string(kv.Key))
+ key, err := labelNamer.Build(string(kv.Key))
+ if err != nil {
+ // TODO(#7066) Handle this error better.
+ return nil, nil, err
+ }
if _, ok := keysMap[key]; !ok {
keysMap[key] = []string{kv.Value.Emit()}
} else {
@@ -509,17 +540,21 @@ func getAttrs(attrs attribute.Set, labelNamer otlptranslator.LabelNamer) ([]stri
values = append(values, strings.Join(vals, ";"))
}
}
- return keys, values
+ return keys, values, nil
}
func (c *collector) createInfoMetric(name, description string, res *resource.Resource) (prometheus.Metric, error) {
- keys, values := getAttrs(*res.Set(), c.labelNamer)
+ keys, values, err := getAttrs(*res.Set(), c.labelNamer)
+ if err != nil {
+ return nil, err
+ }
desc := prometheus.NewDesc(name, description, keys, nil)
return prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(1), values...)
}
-// getName returns the sanitized name, prefixed with the namespace and suffixed with unit.
-func (c *collector) getName(m metricdata.Metrics) string {
+// getName returns the sanitized name, translated according to the selected
+// TranslationStrategy and namespace option.
+func (c *collector) getName(m metricdata.Metrics) (string, error) {
translatorMetric := otlptranslator.Metric{
Name: m.Name,
Type: c.namingMetricType(m),
@@ -530,7 +565,7 @@ func (c *collector) getName(m metricdata.Metrics) string {
return c.metricNamer.Build(translatorMetric)
}
-func (c *collector) metricType(m metricdata.Metrics) *dto.MetricType {
+func (*collector) metricType(m metricdata.Metrics) *dto.MetricType {
switch v := m.Data.(type) {
case metricdata.ExponentialHistogram[int64], metricdata.ExponentialHistogram[float64]:
return dto.MetricType_HISTOGRAM.Enum()
@@ -581,13 +616,18 @@ func (c *collector) namingMetricType(m metricdata.Metrics) otlptranslator.Metric
return otlptranslator.MetricTypeUnknown
}
-func (c *collector) createResourceAttributes(res *resource.Resource) {
+func (c *collector) createResourceAttributes(res *resource.Resource) error {
c.mu.Lock()
defer c.mu.Unlock()
resourceAttrs, _ := res.Set().Filter(c.resourceAttributesFilter)
- resourceKeys, resourceValues := getAttrs(resourceAttrs, c.labelNamer)
+ resourceKeys, resourceValues, err := getAttrs(resourceAttrs, c.labelNamer)
+ if err != nil {
+ return err
+ }
+
c.resourceKeyVals = keyVals{keys: resourceKeys, vals: resourceValues}
+ return nil
}
func (c *collector) validateMetrics(name, description string, metricType *dto.MetricType) (drop bool, help string) {
@@ -638,10 +678,14 @@ func addExemplars[N int64 | float64](
}
promExemplars := make([]prometheus.Exemplar, len(exemplars))
for i, exemplar := range exemplars {
- labels := attributesToLabels(exemplar.FilteredAttributes, labelNamer)
+ labels, err := attributesToLabels(exemplar.FilteredAttributes, labelNamer)
+ if err != nil {
+ otel.Handle(err)
+ return m
+ }
// Overwrite any existing trace ID or span ID attributes
- labels[otlptranslator.ExemplarTraceIDKey] = hex.EncodeToString(exemplar.TraceID[:])
- labels[otlptranslator.ExemplarSpanIDKey] = hex.EncodeToString(exemplar.SpanID[:])
+ labels[otlptranslator.ExemplarTraceIDKey] = hex.EncodeToString(exemplar.TraceID)
+ labels[otlptranslator.ExemplarSpanIDKey] = hex.EncodeToString(exemplar.SpanID)
promExemplars[i] = prometheus.Exemplar{
Value: float64(exemplar.Value),
Timestamp: exemplar.Time,
@@ -658,10 +702,14 @@ func addExemplars[N int64 | float64](
return metricWithExemplar
}
-func attributesToLabels(attrs []attribute.KeyValue, labelNamer otlptranslator.LabelNamer) prometheus.Labels {
+func attributesToLabels(attrs []attribute.KeyValue, labelNamer otlptranslator.LabelNamer) (prometheus.Labels, error) {
labels := make(map[string]string)
for _, attr := range attrs {
- labels[labelNamer.Build(string(attr.Key))] = attr.Value.Emit()
+ name, err := labelNamer.Build(string(attr.Key))
+ if err != nil {
+ return nil, err
+ }
+ labels[name] = attr.Value.Emit()
}
- return labels
+ return labels, nil
}