| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package pmetric_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
|
| 9 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 10 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 11 |
) |
| 12 |
|
| 13 |
func ExampleNewMetrics() { |
| 14 |
metrics := pmetric.NewMetrics() |
| 15 |
|
| 16 |
resourceMetrics := metrics.ResourceMetrics().AppendEmpty() |
| 17 |
|
| 18 |
resourceMetrics.Resource().Attributes().PutStr("service.name", "my-service") |
| 19 |
resourceMetrics.Resource().Attributes().PutStr("host.name", "server-01") |
| 20 |
|
| 21 |
scopeMetrics := resourceMetrics.ScopeMetrics().AppendEmpty() |
| 22 |
scopeMetrics.Scope().SetName("my-meter") |
| 23 |
scopeMetrics.Scope().SetVersion("1.0.0") |
| 24 |
|
| 25 |
metric := scopeMetrics.Metrics().AppendEmpty() |
| 26 |
metric.SetName("requests_total") |
| 27 |
metric.SetDescription("Total number of requests") |
| 28 |
metric.SetUnit("1") |
| 29 |
|
| 30 |
sum := metric.SetEmptySum() |
| 31 |
sum.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative) |
| 32 |
sum.SetIsMonotonic(true) |
| 33 |
|
| 34 |
dataPoint := sum.DataPoints().AppendEmpty() |
| 35 |
dataPoint.SetStartTimestamp(pcommon.Timestamp(1640991600000000000)) // 2022-01-01 00:00:00 UTC |
| 36 |
dataPoint.SetTimestamp(pcommon.Timestamp(1640995200000000000)) // 2022-01-01 01:00:00 UTC |
| 37 |
dataPoint.SetIntValue(1234) |
| 38 |
dataPoint.Attributes().PutStr("endpoint", "/api/v1/users") |
| 39 |
dataPoint.Attributes().PutStr("method", "GET") |
| 40 |
|
| 41 |
fmt.Printf("Resource metrics count: %d\n", metrics.ResourceMetrics().Len()) |
| 42 |
fmt.Printf("Metrics count: %d\n", scopeMetrics.Metrics().Len()) |
| 43 |
fmt.Printf("Metric name: %s\n", metric.Name()) |
| 44 |
fmt.Printf("Data points count: %d\n", sum.DataPoints().Len()) |
| 45 |
// Output: |
| 46 |
// Resource metrics count: 1 |
| 47 |
// Metrics count: 1 |
| 48 |
// Metric name: requests_total |
| 49 |
// Data points count: 1 |
| 50 |
} |
| 51 |
|
| 52 |
func ExampleMetric_SetEmptyGauge() { |
| 53 |
metrics := pmetric.NewMetrics() |
| 54 |
resourceMetrics := metrics.ResourceMetrics().AppendEmpty() |
| 55 |
scopeMetrics := resourceMetrics.ScopeMetrics().AppendEmpty() |
| 56 |
|
| 57 |
metric := scopeMetrics.Metrics().AppendEmpty() |
| 58 |
metric.SetName("cpu_usage_percent") |
| 59 |
metric.SetDescription("Current CPU usage percentage") |
| 60 |
metric.SetUnit("%") |
| 61 |
|
| 62 |
gauge := metric.SetEmptyGauge() |
| 63 |
|
| 64 |
for i := range 4 { |
| 65 |
dataPoint := gauge.DataPoints().AppendEmpty() |
| 66 |
dataPoint.SetTimestamp(pcommon.Timestamp(1640995200000000000)) |
| 67 |
dataPoint.SetDoubleValue(45.5 + float64(i)*2.1) |
| 68 |
dataPoint.Attributes().PutInt("cpu", int64(i)) |
| 69 |
} |
| 70 |
|
| 71 |
fmt.Printf("Metric type: %s\n", metric.Type()) |
| 72 |
fmt.Printf("Data points count: %d\n", gauge.DataPoints().Len()) |
| 73 |
fmt.Printf("First CPU usage: %.1f%%\n", gauge.DataPoints().At(0).DoubleValue()) |
| 74 |
// Output: |
| 75 |
// Metric type: Gauge |
| 76 |
// Data points count: 4 |
| 77 |
// First CPU usage: 45.5% |
| 78 |
} |
| 79 |
|
| 80 |
func ExampleMetric_SetEmptyHistogram() { |
| 81 |
metrics := pmetric.NewMetrics() |
| 82 |
resourceMetrics := metrics.ResourceMetrics().AppendEmpty() |
| 83 |
scopeMetrics := resourceMetrics.ScopeMetrics().AppendEmpty() |
| 84 |
|
| 85 |
metric := scopeMetrics.Metrics().AppendEmpty() |
| 86 |
metric.SetName("request_duration_seconds") |
| 87 |
metric.SetDescription("Request duration in seconds") |
| 88 |
metric.SetUnit("s") |
| 89 |
|
| 90 |
histogram := metric.SetEmptyHistogram() |
| 91 |
histogram.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative) |
| 92 |
|
| 93 |
dataPoint := histogram.DataPoints().AppendEmpty() |
| 94 |
dataPoint.SetStartTimestamp(pcommon.Timestamp(1640995140000000000)) // 2022-01-01 00:59:00 UTC |
| 95 |
dataPoint.SetTimestamp(pcommon.Timestamp(1640995200000000000)) // 2022-01-01 01:00:00 UTC |
| 96 |
dataPoint.SetCount(100) |
| 97 |
dataPoint.SetSum(23.5) |
| 98 |
dataPoint.SetMin(0.001) |
| 99 |
dataPoint.SetMax(5.0) |
| 100 |
|
| 101 |
dataPoint.ExplicitBounds().FromRaw([]float64{0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0}) |
| 102 |
dataPoint.BucketCounts().FromRaw([]uint64{5, 10, 20, 25, 20, 15, 3, 2, 0, 0}) |
| 103 |
|
| 104 |
dataPoint.Attributes().PutStr("endpoint", "/api/health") |
| 105 |
dataPoint.SetFlags(pmetric.DefaultDataPointFlags) |
| 106 |
|
| 107 |
fmt.Printf("Metric type: %s\n", metric.Type()) |
| 108 |
fmt.Printf("Total count: %d\n", dataPoint.Count()) |
| 109 |
fmt.Printf("Total sum: %.1f\n", dataPoint.Sum()) |
| 110 |
fmt.Printf("Min value: %.3f\n", dataPoint.Min()) |
| 111 |
fmt.Printf("Max value: %.1f\n", dataPoint.Max()) |
| 112 |
fmt.Printf("Bucket count: %d\n", dataPoint.BucketCounts().Len()) |
| 113 |
// Output: |
| 114 |
// Metric type: Histogram |
| 115 |
// Total count: 100 |
| 116 |
// Total sum: 23.5 |
| 117 |
// Min value: 0.001 |
| 118 |
// Max value: 5.0 |
| 119 |
// Bucket count: 10 |
| 120 |
} |
| 121 |
|
| 122 |
func ExampleMetric_SetEmptyExponentialHistogram() { |
| 123 |
metrics := pmetric.NewMetrics() |
| 124 |
resourceMetrics := metrics.ResourceMetrics().AppendEmpty() |
| 125 |
scopeMetrics := resourceMetrics.ScopeMetrics().AppendEmpty() |
| 126 |
|
| 127 |
metric := scopeMetrics.Metrics().AppendEmpty() |
| 128 |
metric.SetName("response_size_bytes") |
| 129 |
metric.SetDescription("Response size in bytes") |
| 130 |
metric.SetUnit("By") |
| 131 |
|
| 132 |
expHist := metric.SetEmptyExponentialHistogram() |
| 133 |
expHist.SetAggregationTemporality(pmetric.AggregationTemporalityDelta) |
| 134 |
|
| 135 |
dataPoint := expHist.DataPoints().AppendEmpty() |
| 136 |
dataPoint.SetStartTimestamp(pcommon.Timestamp(1640995140000000000)) |
| 137 |
dataPoint.SetTimestamp(pcommon.Timestamp(1640995200000000000)) |
| 138 |
dataPoint.SetCount(50) |
| 139 |
dataPoint.SetSum(1024.5) |
| 140 |
dataPoint.SetScale(4) |
| 141 |
dataPoint.SetZeroCount(5) |
| 142 |
dataPoint.SetZeroThreshold(0.001) |
| 143 |
|
| 144 |
// Set positive buckets |
| 145 |
positive := dataPoint.Positive() |
| 146 |
positive.SetOffset(2) |
| 147 |
positive.BucketCounts().FromRaw([]uint64{2, 3, 7, 8, 5}) |
| 148 |
|
| 149 |
// Set negative buckets |
| 150 |
negative := dataPoint.Negative() |
| 151 |
negative.SetOffset(1) |
| 152 |
negative.BucketCounts().FromRaw([]uint64{1, 2, 3}) |
| 153 |
|
| 154 |
dataPoint.Attributes().PutStr("protocol", "http") |
| 155 |
|
| 156 |
fmt.Printf("Metric type: %s\n", metric.Type()) |
| 157 |
fmt.Printf("Scale: %d\n", dataPoint.Scale()) |
| 158 |
fmt.Printf("Zero count: %d\n", dataPoint.ZeroCount()) |
| 159 |
fmt.Printf("Positive buckets: %d\n", positive.BucketCounts().Len()) |
| 160 |
fmt.Printf("Negative buckets: %d\n", negative.BucketCounts().Len()) |
| 161 |
// Output: |
| 162 |
// Metric type: ExponentialHistogram |
| 163 |
// Scale: 4 |
| 164 |
// Zero count: 5 |
| 165 |
// Positive buckets: 5 |
| 166 |
// Negative buckets: 3 |
| 167 |
} |
| 168 |
|
| 169 |
func ExampleMetric_SetEmptySummary() { |
| 170 |
metrics := pmetric.NewMetrics() |
| 171 |
resourceMetrics := metrics.ResourceMetrics().AppendEmpty() |
| 172 |
scopeMetrics := resourceMetrics.ScopeMetrics().AppendEmpty() |
| 173 |
|
| 174 |
metric := scopeMetrics.Metrics().AppendEmpty() |
| 175 |
metric.SetName("latency_seconds") |
| 176 |
metric.SetDescription("Request latency in seconds") |
| 177 |
metric.SetUnit("s") |
| 178 |
|
| 179 |
summary := metric.SetEmptySummary() |
| 180 |
|
| 181 |
dataPoint := summary.DataPoints().AppendEmpty() |
| 182 |
dataPoint.SetStartTimestamp(pcommon.Timestamp(1640995140000000000)) |
| 183 |
dataPoint.SetTimestamp(pcommon.Timestamp(1640995200000000000)) |
| 184 |
dataPoint.SetCount(1000) |
| 185 |
dataPoint.SetSum(125.5) |
| 186 |
|
| 187 |
// Add quantile values |
| 188 |
q50 := dataPoint.QuantileValues().AppendEmpty() |
| 189 |
q50.SetQuantile(0.5) |
| 190 |
q50.SetValue(0.1) |
| 191 |
|
| 192 |
q95 := dataPoint.QuantileValues().AppendEmpty() |
| 193 |
q95.SetQuantile(0.95) |
| 194 |
q95.SetValue(0.5) |
| 195 |
|
| 196 |
q99 := dataPoint.QuantileValues().AppendEmpty() |
| 197 |
q99.SetQuantile(0.99) |
| 198 |
q99.SetValue(1.0) |
| 199 |
|
| 200 |
dataPoint.Attributes().PutStr("service", "api") |
| 201 |
|
| 202 |
fmt.Printf("Metric type: %s\n", metric.Type()) |
| 203 |
fmt.Printf("Count: %d\n", dataPoint.Count()) |
| 204 |
fmt.Printf("Sum: %.1f\n", dataPoint.Sum()) |
| 205 |
fmt.Printf("Quantiles: %d\n", dataPoint.QuantileValues().Len()) |
| 206 |
fmt.Printf("P95: %.1f\n", q95.Value()) |
| 207 |
// Output: |
| 208 |
// Metric type: Summary |
| 209 |
// Count: 1000 |
| 210 |
// Sum: 125.5 |
| 211 |
// Quantiles: 3 |
| 212 |
// P95: 0.5 |
| 213 |
} |
| 214 |
|
| 215 |
func ExampleNumberDataPoint_Exemplars() { |
| 216 |
metrics := pmetric.NewMetrics() |
| 217 |
resourceMetrics := metrics.ResourceMetrics().AppendEmpty() |
| 218 |
scopeMetrics := resourceMetrics.ScopeMetrics().AppendEmpty() |
| 219 |
|
| 220 |
metric := scopeMetrics.Metrics().AppendEmpty() |
| 221 |
metric.SetName("http_requests_total") |
| 222 |
|
| 223 |
sum := metric.SetEmptySum() |
| 224 |
dataPoint := sum.DataPoints().AppendEmpty() |
| 225 |
dataPoint.SetIntValue(5000) |
| 226 |
|
| 227 |
exemplar := dataPoint.Exemplars().AppendEmpty() |
| 228 |
exemplar.SetTimestamp(pcommon.Timestamp(1640995200000000000)) |
| 229 |
exemplar.SetIntValue(1) |
| 230 |
exemplar.FilteredAttributes().PutStr("trace_id", "abc123") |
| 231 |
exemplar.FilteredAttributes().PutStr("span_id", "def456") |
| 232 |
|
| 233 |
fmt.Printf("Data point value: %d\n", dataPoint.IntValue()) |
| 234 |
fmt.Printf("Exemplars count: %d\n", dataPoint.Exemplars().Len()) |
| 235 |
fmt.Printf("Exemplar value: %d\n", exemplar.IntValue()) |
| 236 |
// Output: |
| 237 |
// Data point value: 5000 |
| 238 |
// Exemplars count: 1 |
| 239 |
// Exemplar value: 1 |
| 240 |
} |
| 241 |
|
| 242 |
func ExampleDataPointFlags() { |
| 243 |
metrics := pmetric.NewMetrics() |
| 244 |
resourceMetrics := metrics.ResourceMetrics().AppendEmpty() |
| 245 |
scopeMetrics := resourceMetrics.ScopeMetrics().AppendEmpty() |
| 246 |
|
| 247 |
metric := scopeMetrics.Metrics().AppendEmpty() |
| 248 |
metric.SetName("test_metric") |
| 249 |
|
| 250 |
gauge := metric.SetEmptyGauge() |
| 251 |
dataPoint := gauge.DataPoints().AppendEmpty() |
| 252 |
|
| 253 |
// Test default flags |
| 254 |
defaultFlags := pmetric.DefaultDataPointFlags |
| 255 |
dataPoint.SetFlags(defaultFlags) |
| 256 |
fmt.Printf("Default flags NoRecordedValue: %t\n", dataPoint.Flags().NoRecordedValue()) |
| 257 |
|
| 258 |
// Test with NoRecordedValue flag |
| 259 |
flagsWithNoValue := defaultFlags.WithNoRecordedValue(true) |
| 260 |
dataPoint.SetFlags(flagsWithNoValue) |
| 261 |
fmt.Printf("With NoRecordedValue flag: %t\n", dataPoint.Flags().NoRecordedValue()) |
| 262 |
|
| 263 |
// Test removing NoRecordedValue flag |
| 264 |
flagsWithoutNoValue := flagsWithNoValue.WithNoRecordedValue(false) |
| 265 |
dataPoint.SetFlags(flagsWithoutNoValue) |
| 266 |
fmt.Printf("Without NoRecordedValue flag: %t\n", dataPoint.Flags().NoRecordedValue()) |
| 267 |
|
| 268 |
// Output: |
| 269 |
// Default flags NoRecordedValue: false |
| 270 |
// With NoRecordedValue flag: true |
| 271 |
// Without NoRecordedValue flag: false |
| 272 |
} |