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