Debian Codegraph @

exporter/debugexporter/internal/normal/metrics.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package normal // import "go.opentelemetry.io/collector/exporter/debugexporter/internal/normal"
5
6 import (
7 "bytes"
8 "fmt"
9 "strconv"
10 "strings"
11
12 "go.opentelemetry.io/collector/pdata/pmetric"
13 )
14
15 type normalMetricsMarshaler struct{}
16
17 // Ensure normalMetricsMarshaller implements interface pmetric.Marshaler
18 var _ pmetric.Marshaler = normalMetricsMarshaler{}
19
20 // NewNormalMetricsMarshaler returns a pmetric.Marshaler for normal verbosity. It writes one line of text per log record
21 func NewNormalMetricsMarshaler() pmetric.Marshaler {
22 return normalMetricsMarshaler{}
23 }
24
25 func (normalMetricsMarshaler) MarshalMetrics(md pmetric.Metrics) ([]byte, error) {
26 var buffer bytes.Buffer
27 for i := 0; i < md.ResourceMetrics().Len(); i++ {
28 resourceMetrics := md.ResourceMetrics().At(i)
29
30 buffer.WriteString(fmt.Sprintf("ResourceMetrics #%d%s%s\n", i, writeResourceDetails(resourceMetrics.SchemaUrl()), writeAttributesString(resourceMetrics.Resource().Attributes())))
31
32 for j := 0; j < resourceMetrics.ScopeMetrics().Len(); j++ {
33 scopeMetrics := resourceMetrics.ScopeMetrics().At(j)
34
35 buffer.WriteString(fmt.Sprintf("ScopeMetrics #%d%s%s\n", i, writeScopeDetails(scopeMetrics.Scope().Name(), scopeMetrics.Scope().Version(), scopeMetrics.SchemaUrl()), writeAttributesString(scopeMetrics.Scope().Attributes())))
36
37 for k := 0; k < scopeMetrics.Metrics().Len(); k++ {
38 metric := scopeMetrics.Metrics().At(k)
39
40 var dataPointLines []string
41 switch metric.Type() {
42 case pmetric.MetricTypeGauge:
43 dataPointLines = writeNumberDataPoints(metric, metric.Gauge().DataPoints())
44 case pmetric.MetricTypeSum:
45 dataPointLines = writeNumberDataPoints(metric, metric.Sum().DataPoints())
46 case pmetric.MetricTypeHistogram:
47 dataPointLines = writeHistogramDataPoints(metric)
48 case pmetric.MetricTypeExponentialHistogram:
49 dataPointLines = writeExponentialHistogramDataPoints(metric)
50 case pmetric.MetricTypeSummary:
51 dataPointLines = writeSummaryDataPoints(metric)
52 }
53 for _, line := range dataPointLines {
54 buffer.WriteString(line)
55 }
56 }
57 }
58 }
59 return buffer.Bytes(), nil
60 }
61
62 func writeNumberDataPoints(metric pmetric.Metric, dataPoints pmetric.NumberDataPointSlice) (lines []string) {
63 for i := 0; i < dataPoints.Len(); i++ {
64 dataPoint := dataPoints.At(i)
65 dataPointAttributes := writeAttributes(dataPoint.Attributes())
66
67 var value string
68 switch dataPoint.ValueType() {
69 case pmetric.NumberDataPointValueTypeInt:
70 value = strconv.FormatInt(dataPoint.IntValue(), 10)
71 case pmetric.NumberDataPointValueTypeDouble:
72 value = fmt.Sprintf("%v", dataPoint.DoubleValue())
73 }
74
75 dataPointLine := fmt.Sprintf("%s{%s} %s\n", metric.Name(), strings.Join(dataPointAttributes, ","), value)
76 lines = append(lines, dataPointLine)
77 }
78 return lines
79 }
80
81 func writeHistogramDataPoints(metric pmetric.Metric) (lines []string) {
82 for i := 0; i < metric.Histogram().DataPoints().Len(); i++ {
83 dataPoint := metric.Histogram().DataPoints().At(i)
84 dataPointAttributes := writeAttributes(dataPoint.Attributes())
85
86 var value strings.Builder
87 fmt.Fprintf(&value, "count=%d", dataPoint.Count())
88 if dataPoint.HasSum() {
89 fmt.Fprintf(&value, " sum=%v", dataPoint.Sum())
90 }
91 if dataPoint.HasMin() {
92 fmt.Fprintf(&value, " min=%v", dataPoint.Min())
93 }
94 if dataPoint.HasMax() {
95 fmt.Fprintf(&value, " max=%v", dataPoint.Max())
96 }
97
98 for bucketIndex := 0; bucketIndex < dataPoint.BucketCounts().Len(); bucketIndex++ {
99 bucketBound := ""
100 if bucketIndex < dataPoint.ExplicitBounds().Len() {
101 bucketBound = fmt.Sprintf("le%v=", dataPoint.ExplicitBounds().At(bucketIndex))
102 }
103 bucketCount := dataPoint.BucketCounts().At(bucketIndex)
104 fmt.Fprintf(&value, " %s%d", bucketBound, bucketCount)
105 }
106
107 dataPointLine := fmt.Sprintf("%s{%s} %s\n", metric.Name(), strings.Join(dataPointAttributes, ","), value.String())
108 lines = append(lines, dataPointLine)
109 }
110 return lines
111 }
112
113 func writeExponentialHistogramDataPoints(metric pmetric.Metric) (lines []string) {
114 for i := 0; i < metric.ExponentialHistogram().DataPoints().Len(); i++ {
115 dataPoint := metric.ExponentialHistogram().DataPoints().At(i)
116 dataPointAttributes := writeAttributes(dataPoint.Attributes())
117
118 var value string
119 value = fmt.Sprintf("count=%d", dataPoint.Count())
120 if dataPoint.HasSum() {
121 value += fmt.Sprintf(" sum=%v", dataPoint.Sum())
122 }
123 if dataPoint.HasMin() {
124 value += fmt.Sprintf(" min=%v", dataPoint.Min())
125 }
126 if dataPoint.HasMax() {
127 value += fmt.Sprintf(" max=%v", dataPoint.Max())
128 }
129
130 // TODO display buckets
131
132 dataPointLine := fmt.Sprintf("%s{%s} %s\n", metric.Name(), strings.Join(dataPointAttributes, ","), value)
133 lines = append(lines, dataPointLine)
134 }
135 return lines
136 }
137
138 func writeSummaryDataPoints(metric pmetric.Metric) (lines []string) {
139 for i := 0; i < metric.Summary().DataPoints().Len(); i++ {
140 dataPoint := metric.Summary().DataPoints().At(i)
141 dataPointAttributes := writeAttributes(dataPoint.Attributes())
142
143 var value strings.Builder
144 fmt.Fprintf(&value, "count=%d", dataPoint.Count())
145 fmt.Fprintf(&value, " sum=%f", dataPoint.Sum())
146
147 for quantileIndex := 0; quantileIndex < dataPoint.QuantileValues().Len(); quantileIndex++ {
148 quantile := dataPoint.QuantileValues().At(quantileIndex)
149 fmt.Fprintf(&value, " q%v=%v", quantile.Quantile(), quantile.Value())
150 }
151
152 dataPointLine := fmt.Sprintf("%s{%s} %s\n", metric.Name(), strings.Join(dataPointAttributes, ","), value.String())
153 lines = append(lines, dataPointLine)
154 }
155 return lines
156 }