Debian Codegraph @

storage/remote/otlptranslator/prometheusremotewrite/otlp_to_openmetrics_metadata.go

raw
1 // Copyright 2024 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 // Provenance-includes-location: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/95e8f8fdc2a9dc87230406c9a3cf02be4fd68bea/pkg/translator/prometheusremotewrite/otlp_to_openmetrics_metadata.go
14 // Provenance-includes-license: Apache-2.0
15 // Provenance-includes-copyright: Copyright The OpenTelemetry Authors.
16
17 package prometheusremotewrite
18
19 import (
20 "go.opentelemetry.io/collector/pdata/pmetric"
21
22 "github.com/prometheus/prometheus/prompb"
23 prometheustranslator "github.com/prometheus/prometheus/storage/remote/otlptranslator/prometheus"
24 )
25
26 func otelMetricTypeToPromMetricType(otelMetric pmetric.Metric) prompb.MetricMetadata_MetricType {
27 switch otelMetric.Type() {
28 case pmetric.MetricTypeGauge:
29 return prompb.MetricMetadata_GAUGE
30 case pmetric.MetricTypeSum:
31 metricType := prompb.MetricMetadata_GAUGE
32 if otelMetric.Sum().IsMonotonic() {
33 metricType = prompb.MetricMetadata_COUNTER
34 }
35 return metricType
36 case pmetric.MetricTypeHistogram:
37 return prompb.MetricMetadata_HISTOGRAM
38 case pmetric.MetricTypeSummary:
39 return prompb.MetricMetadata_SUMMARY
40 case pmetric.MetricTypeExponentialHistogram:
41 return prompb.MetricMetadata_HISTOGRAM
42 }
43 return prompb.MetricMetadata_UNKNOWN
44 }
45
46 func OtelMetricsToMetadata(md pmetric.Metrics, addMetricSuffixes bool) []*prompb.MetricMetadata {
47 resourceMetricsSlice := md.ResourceMetrics()
48
49 metadataLength := 0
50 for i := 0; i < resourceMetricsSlice.Len(); i++ {
51 scopeMetricsSlice := resourceMetricsSlice.At(i).ScopeMetrics()
52 for j := 0; j < scopeMetricsSlice.Len(); j++ {
53 metadataLength += scopeMetricsSlice.At(j).Metrics().Len()
54 }
55 }
56
57 var metadata = make([]*prompb.MetricMetadata, 0, metadataLength)
58 for i := 0; i < resourceMetricsSlice.Len(); i++ {
59 resourceMetrics := resourceMetricsSlice.At(i)
60 scopeMetricsSlice := resourceMetrics.ScopeMetrics()
61
62 for j := 0; j < scopeMetricsSlice.Len(); j++ {
63 scopeMetrics := scopeMetricsSlice.At(j)
64 for k := 0; k < scopeMetrics.Metrics().Len(); k++ {
65 metric := scopeMetrics.Metrics().At(k)
66 entry := prompb.MetricMetadata{
67 Type: otelMetricTypeToPromMetricType(metric),
68 MetricFamilyName: prometheustranslator.BuildCompliantName(metric, "", addMetricSuffixes),
69 Help: metric.Description(),
70 }
71 metadata = append(metadata, &entry)
72 }
73 }
74 }
75
76 return metadata
77 }