| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package normal |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
|
| 12 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 13 |
) |
| 14 |
|
| 15 |
func TestMarshalMetrics(t *testing.T) { |
| 16 |
tests := []struct { |
| 17 |
name string |
| 18 |
input pmetric.Metrics |
| 19 |
expected string |
| 20 |
}{ |
| 21 |
{ |
| 22 |
name: "empty metrics", |
| 23 |
input: pmetric.NewMetrics(), |
| 24 |
expected: "", |
| 25 |
}, |
| 26 |
{ |
| 27 |
name: "sum data point", |
| 28 |
input: func() pmetric.Metrics { |
| 29 |
metrics := pmetric.NewMetrics() |
| 30 |
metric := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty() |
| 31 |
metric.SetName("system.cpu.time") |
| 32 |
dataPoint := metric.SetEmptySum().DataPoints().AppendEmpty() |
| 33 |
dataPoint.SetDoubleValue(123.456) |
| 34 |
dataPoint.Attributes().PutStr("state", "user") |
| 35 |
dataPoint.Attributes().PutStr("cpu", "0") |
| 36 |
return metrics |
| 37 |
}(), |
| 38 |
expected: `ResourceMetrics #0 |
| 39 |
ScopeMetrics #0 |
| 40 |
system.cpu.time{state=user,cpu=0} 123.456 |
| 41 |
`, |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "data point with resource and scope attributes", |
| 45 |
input: func() pmetric.Metrics { |
| 46 |
metrics := pmetric.NewMetrics() |
| 47 |
resourceMetrics := metrics.ResourceMetrics().AppendEmpty() |
| 48 |
resourceMetrics.SetSchemaUrl("https://opentelemetry.io/resource-schema-url") |
| 49 |
resourceMetrics.Resource().Attributes().PutStr("resourceKey1", "resourceValue1") |
| 50 |
resourceMetrics.Resource().Attributes().PutBool("resourceKey2", false) |
| 51 |
scopeMetrics := resourceMetrics.ScopeMetrics().AppendEmpty() |
| 52 |
scopeMetrics.SetSchemaUrl("http://opentelemetry.io/scope-schema-url") |
| 53 |
scopeMetrics.Scope().SetName("scope-name") |
| 54 |
scopeMetrics.Scope().SetVersion("1.2.3") |
| 55 |
scopeMetrics.Scope().Attributes().PutStr("scopeKey1", "scopeValue1") |
| 56 |
scopeMetrics.Scope().Attributes().PutBool("scopeKey2", true) |
| 57 |
metric := scopeMetrics.Metrics().AppendEmpty() |
| 58 |
metric.SetName("system.cpu.time") |
| 59 |
dataPoint := metric.SetEmptySum().DataPoints().AppendEmpty() |
| 60 |
dataPoint.SetDoubleValue(123.456) |
| 61 |
dataPoint.Attributes().PutStr("state", "user") |
| 62 |
dataPoint.Attributes().PutStr("cpu", "0") |
| 63 |
return metrics |
| 64 |
}(), |
| 65 |
expected: `ResourceMetrics #0 [https://opentelemetry.io/resource-schema-url] resourceKey1=resourceValue1 resourceKey2=false |
| 66 |
ScopeMetrics #0 [email protected] [http://opentelemetry.io/scope-schema-url] scopeKey1=scopeValue1 scopeKey2=true |
| 67 |
system.cpu.time{state=user,cpu=0} 123.456 |
| 68 |
`, |
| 69 |
}, |
| 70 |
{ |
| 71 |
name: "gauge data point", |
| 72 |
input: func() pmetric.Metrics { |
| 73 |
metrics := pmetric.NewMetrics() |
| 74 |
metric := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty() |
| 75 |
metric.SetName("system.cpu.utilization") |
| 76 |
dataPoint := metric.SetEmptyGauge().DataPoints().AppendEmpty() |
| 77 |
dataPoint.SetDoubleValue(78.901234567) |
| 78 |
dataPoint.Attributes().PutStr("state", "free") |
| 79 |
dataPoint.Attributes().PutStr("cpu", "8") |
| 80 |
return metrics |
| 81 |
}(), |
| 82 |
expected: `ResourceMetrics #0 |
| 83 |
ScopeMetrics #0 |
| 84 |
system.cpu.utilization{state=free,cpu=8} 78.901234567 |
| 85 |
`, |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "histogram", |
| 89 |
input: func() pmetric.Metrics { |
| 90 |
metrics := pmetric.NewMetrics() |
| 91 |
metric := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty() |
| 92 |
metric.SetName("http.server.request.duration") |
| 93 |
dataPoint := metric.SetEmptyHistogram().DataPoints().AppendEmpty() |
| 94 |
dataPoint.Attributes().PutInt("http.response.status_code", 200) |
| 95 |
dataPoint.Attributes().PutStr("http.request.method", "GET") |
| 96 |
dataPoint.ExplicitBounds().FromRaw([]float64{0.125, 0.5, 1, 3}) |
| 97 |
dataPoint.BucketCounts().FromRaw([]uint64{1324, 13, 0, 2, 1}) |
| 98 |
dataPoint.SetCount(1340) |
| 99 |
dataPoint.SetSum(99.573) |
| 100 |
dataPoint.SetMin(0.017) |
| 101 |
dataPoint.SetMax(8.13) |
| 102 |
return metrics |
| 103 |
}(), |
| 104 |
expected: `ResourceMetrics #0 |
| 105 |
ScopeMetrics #0 |
| 106 |
http.server.request.duration{http.response.status_code=200,http.request.method=GET} count=1340 sum=99.573 min=0.017 max=8.13 le0.125=1324 le0.5=13 le1=0 le3=2 1 |
| 107 |
`, |
| 108 |
}, |
| 109 |
{ |
| 110 |
name: "exponential histogram", |
| 111 |
input: func() pmetric.Metrics { |
| 112 |
metrics := pmetric.NewMetrics() |
| 113 |
metric := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty() |
| 114 |
metric.SetName("http.server.request.duration") |
| 115 |
dataPoint := metric.SetEmptyExponentialHistogram().DataPoints().AppendEmpty() |
| 116 |
dataPoint.Attributes().PutInt("http.response.status_code", 200) |
| 117 |
dataPoint.Attributes().PutStr("http.request.method", "GET") |
| 118 |
dataPoint.SetCount(1340) |
| 119 |
dataPoint.SetSum(99.573) |
| 120 |
dataPoint.SetMin(0.017) |
| 121 |
dataPoint.SetMax(8.13) |
| 122 |
return metrics |
| 123 |
}(), |
| 124 |
expected: `ResourceMetrics #0 |
| 125 |
ScopeMetrics #0 |
| 126 |
http.server.request.duration{http.response.status_code=200,http.request.method=GET} count=1340 sum=99.573 min=0.017 max=8.13 |
| 127 |
`, |
| 128 |
}, |
| 129 |
{ |
| 130 |
name: "summary", |
| 131 |
input: func() pmetric.Metrics { |
| 132 |
metrics := pmetric.NewMetrics() |
| 133 |
metric := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty() |
| 134 |
metric.SetName("summary") |
| 135 |
dataPoint := metric.SetEmptySummary().DataPoints().AppendEmpty() |
| 136 |
dataPoint.Attributes().PutInt("http.response.status_code", 200) |
| 137 |
dataPoint.Attributes().PutStr("http.request.method", "GET") |
| 138 |
dataPoint.SetCount(1340) |
| 139 |
dataPoint.SetSum(99.573) |
| 140 |
quantile := dataPoint.QuantileValues().AppendEmpty() |
| 141 |
quantile.SetQuantile(0.01) |
| 142 |
quantile.SetValue(15) |
| 143 |
return metrics |
| 144 |
}(), |
| 145 |
expected: `ResourceMetrics #0 |
| 146 |
ScopeMetrics #0 |
| 147 |
summary{http.response.status_code=200,http.request.method=GET} count=1340 sum=99.573000 q0.01=15 |
| 148 |
`, |
| 149 |
}, |
| 150 |
} |
| 151 |
for _, tt := range tests { |
| 152 |
t.Run(tt.name, func(t *testing.T) { |
| 153 |
output, err := NewNormalMetricsMarshaler().MarshalMetrics(tt.input) |
| 154 |
require.NoError(t, err) |
| 155 |
assert.Equal(t, tt.expected, string(output)) |
| 156 |
}) |
| 157 |
} |
| 158 |
} |