| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package stdoutmetric_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"encoding/json" |
| 9 |
"os" |
| 10 |
"time" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/attribute" |
| 13 |
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" |
| 14 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 15 |
"go.opentelemetry.io/otel/sdk/metric" |
| 16 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 17 |
"go.opentelemetry.io/otel/sdk/resource" |
| 18 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
// Sat Jan 01 2000 00:00:00 GMT+0000. |
| 23 |
now = time.Date(2000, time.January, 0o1, 0, 0, 0, 0, time.FixedZone("GMT", 0)) |
| 24 |
|
| 25 |
res = resource.NewSchemaless( |
| 26 |
semconv.ServiceName("stdoutmetric-example"), |
| 27 |
) |
| 28 |
|
| 29 |
mockData = metricdata.ResourceMetrics{ |
| 30 |
Resource: res, |
| 31 |
ScopeMetrics: []metricdata.ScopeMetrics{ |
| 32 |
{ |
| 33 |
Scope: instrumentation.Scope{Name: "example", Version: "0.0.1"}, |
| 34 |
Metrics: []metricdata.Metrics{ |
| 35 |
{ |
| 36 |
Name: "requests", |
| 37 |
Description: "Number of requests received", |
| 38 |
Unit: "1", |
| 39 |
Data: metricdata.Sum[int64]{ |
| 40 |
IsMonotonic: true, |
| 41 |
Temporality: metricdata.DeltaTemporality, |
| 42 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 43 |
{ |
| 44 |
Attributes: attribute.NewSet(attribute.String("server", "central")), |
| 45 |
StartTime: now, |
| 46 |
Time: now.Add(1 * time.Second), |
| 47 |
Value: 5, |
| 48 |
}, |
| 49 |
}, |
| 50 |
}, |
| 51 |
}, |
| 52 |
{ |
| 53 |
Name: "system.cpu.time", |
| 54 |
Description: "Accumulated CPU time spent", |
| 55 |
Unit: "s", |
| 56 |
Data: metricdata.Sum[float64]{ |
| 57 |
IsMonotonic: true, |
| 58 |
Temporality: metricdata.CumulativeTemporality, |
| 59 |
DataPoints: []metricdata.DataPoint[float64]{ |
| 60 |
{ |
| 61 |
Attributes: attribute.NewSet(attribute.String("state", "user")), |
| 62 |
StartTime: now, |
| 63 |
Time: now.Add(1 * time.Second), |
| 64 |
Value: 0.5, |
| 65 |
}, |
| 66 |
}, |
| 67 |
}, |
| 68 |
}, |
| 69 |
{ |
| 70 |
Name: "requests.size", |
| 71 |
Description: "Size of received requests", |
| 72 |
Unit: "kb", |
| 73 |
Data: metricdata.Histogram[int64]{ |
| 74 |
Temporality: metricdata.DeltaTemporality, |
| 75 |
DataPoints: []metricdata.HistogramDataPoint[int64]{ |
| 76 |
{ |
| 77 |
Attributes: attribute.NewSet(attribute.String("server", "central")), |
| 78 |
StartTime: now, |
| 79 |
Time: now.Add(1 * time.Second), |
| 80 |
Count: 10, |
| 81 |
Bounds: []float64{1, 5, 10}, |
| 82 |
BucketCounts: []uint64{1, 3, 6, 0}, |
| 83 |
Sum: 128, |
| 84 |
Min: metricdata.NewExtrema[int64](3), |
| 85 |
Max: metricdata.NewExtrema[int64](30), |
| 86 |
}, |
| 87 |
}, |
| 88 |
}, |
| 89 |
}, |
| 90 |
{ |
| 91 |
Name: "latency", |
| 92 |
Description: "Time spend processing received requests", |
| 93 |
Unit: "ms", |
| 94 |
Data: metricdata.Histogram[float64]{ |
| 95 |
Temporality: metricdata.DeltaTemporality, |
| 96 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 97 |
{ |
| 98 |
Attributes: attribute.NewSet(attribute.String("server", "central")), |
| 99 |
StartTime: now, |
| 100 |
Time: now.Add(1 * time.Second), |
| 101 |
Count: 10, |
| 102 |
Bounds: []float64{1, 5, 10}, |
| 103 |
BucketCounts: []uint64{1, 3, 6, 0}, |
| 104 |
Sum: 57, |
| 105 |
}, |
| 106 |
}, |
| 107 |
}, |
| 108 |
}, |
| 109 |
{ |
| 110 |
Name: "system.memory.usage", |
| 111 |
Description: "Memory usage", |
| 112 |
Unit: "By", |
| 113 |
Data: metricdata.Gauge[int64]{ |
| 114 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 115 |
{ |
| 116 |
Attributes: attribute.NewSet(attribute.String("state", "used")), |
| 117 |
Time: now.Add(1 * time.Second), |
| 118 |
Value: 100, |
| 119 |
}, |
| 120 |
}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
{ |
| 124 |
Name: "temperature", |
| 125 |
Description: "CPU global temperature", |
| 126 |
Unit: "cel(1 K)", |
| 127 |
Data: metricdata.Gauge[float64]{ |
| 128 |
DataPoints: []metricdata.DataPoint[float64]{ |
| 129 |
{ |
| 130 |
Attributes: attribute.NewSet(attribute.String("server", "central")), |
| 131 |
Time: now.Add(1 * time.Second), |
| 132 |
Value: 32.4, |
| 133 |
}, |
| 134 |
}, |
| 135 |
}, |
| 136 |
}, |
| 137 |
}, |
| 138 |
}, |
| 139 |
}, |
| 140 |
} |
| 141 |
) |
| 142 |
|
| 143 |
func Example() { |
| 144 |
// Print with a JSON encoder that indents with two spaces. |
| 145 |
enc := json.NewEncoder(os.Stdout) |
| 146 |
enc.SetIndent("", " ") |
| 147 |
|
| 148 |
exp, err := stdoutmetric.New( |
| 149 |
stdoutmetric.WithEncoder(enc), |
| 150 |
stdoutmetric.WithoutTimestamps(), |
| 151 |
) |
| 152 |
if err != nil { |
| 153 |
panic(err) |
| 154 |
} |
| 155 |
|
| 156 |
// Register the exporter with an SDK via a periodic reader. |
| 157 |
sdk := metric.NewMeterProvider( |
| 158 |
metric.WithResource(res), |
| 159 |
metric.WithReader(metric.NewPeriodicReader(exp)), |
| 160 |
) |
| 161 |
|
| 162 |
ctx := context.Background() |
| 163 |
// This is where the sdk would be used to create a Meter and from that |
| 164 |
// instruments that would make measurements of your code. To simulate that |
| 165 |
// behavior, call export directly with mocked data. |
| 166 |
_ = exp.Export(ctx, &mockData) |
| 167 |
|
| 168 |
// Ensure the periodic reader is cleaned up by shutting down the sdk. |
| 169 |
_ = sdk.Shutdown(ctx) |
| 170 |
|
| 171 |
// Output: |
| 172 |
// { |
| 173 |
// "Resource": [ |
| 174 |
// { |
| 175 |
// "Key": "service.name", |
| 176 |
// "Value": { |
| 177 |
// "Type": "STRING", |
| 178 |
// "Value": "stdoutmetric-example" |
| 179 |
// } |
| 180 |
// } |
| 181 |
// ], |
| 182 |
// "ScopeMetrics": [ |
| 183 |
// { |
| 184 |
// "Scope": { |
| 185 |
// "Name": "example", |
| 186 |
// "Version": "0.0.1", |
| 187 |
// "SchemaURL": "", |
| 188 |
// "Attributes": null |
| 189 |
// }, |
| 190 |
// "Metrics": [ |
| 191 |
// { |
| 192 |
// "Name": "requests", |
| 193 |
// "Description": "Number of requests received", |
| 194 |
// "Unit": "1", |
| 195 |
// "Data": { |
| 196 |
// "DataPoints": [ |
| 197 |
// { |
| 198 |
// "Attributes": [ |
| 199 |
// { |
| 200 |
// "Key": "server", |
| 201 |
// "Value": { |
| 202 |
// "Type": "STRING", |
| 203 |
// "Value": "central" |
| 204 |
// } |
| 205 |
// } |
| 206 |
// ], |
| 207 |
// "StartTime": "0001-01-01T00:00:00Z", |
| 208 |
// "Time": "0001-01-01T00:00:00Z", |
| 209 |
// "Value": 5 |
| 210 |
// } |
| 211 |
// ], |
| 212 |
// "Temporality": "DeltaTemporality", |
| 213 |
// "IsMonotonic": true |
| 214 |
// } |
| 215 |
// }, |
| 216 |
// { |
| 217 |
// "Name": "system.cpu.time", |
| 218 |
// "Description": "Accumulated CPU time spent", |
| 219 |
// "Unit": "s", |
| 220 |
// "Data": { |
| 221 |
// "DataPoints": [ |
| 222 |
// { |
| 223 |
// "Attributes": [ |
| 224 |
// { |
| 225 |
// "Key": "state", |
| 226 |
// "Value": { |
| 227 |
// "Type": "STRING", |
| 228 |
// "Value": "user" |
| 229 |
// } |
| 230 |
// } |
| 231 |
// ], |
| 232 |
// "StartTime": "0001-01-01T00:00:00Z", |
| 233 |
// "Time": "0001-01-01T00:00:00Z", |
| 234 |
// "Value": 0.5 |
| 235 |
// } |
| 236 |
// ], |
| 237 |
// "Temporality": "CumulativeTemporality", |
| 238 |
// "IsMonotonic": true |
| 239 |
// } |
| 240 |
// }, |
| 241 |
// { |
| 242 |
// "Name": "requests.size", |
| 243 |
// "Description": "Size of received requests", |
| 244 |
// "Unit": "kb", |
| 245 |
// "Data": { |
| 246 |
// "DataPoints": [ |
| 247 |
// { |
| 248 |
// "Attributes": [ |
| 249 |
// { |
| 250 |
// "Key": "server", |
| 251 |
// "Value": { |
| 252 |
// "Type": "STRING", |
| 253 |
// "Value": "central" |
| 254 |
// } |
| 255 |
// } |
| 256 |
// ], |
| 257 |
// "StartTime": "0001-01-01T00:00:00Z", |
| 258 |
// "Time": "0001-01-01T00:00:00Z", |
| 259 |
// "Count": 10, |
| 260 |
// "Bounds": [ |
| 261 |
// 1, |
| 262 |
// 5, |
| 263 |
// 10 |
| 264 |
// ], |
| 265 |
// "BucketCounts": [ |
| 266 |
// 1, |
| 267 |
// 3, |
| 268 |
// 6, |
| 269 |
// 0 |
| 270 |
// ], |
| 271 |
// "Min": 3, |
| 272 |
// "Max": 30, |
| 273 |
// "Sum": 128 |
| 274 |
// } |
| 275 |
// ], |
| 276 |
// "Temporality": "DeltaTemporality" |
| 277 |
// } |
| 278 |
// }, |
| 279 |
// { |
| 280 |
// "Name": "latency", |
| 281 |
// "Description": "Time spend processing received requests", |
| 282 |
// "Unit": "ms", |
| 283 |
// "Data": { |
| 284 |
// "DataPoints": [ |
| 285 |
// { |
| 286 |
// "Attributes": [ |
| 287 |
// { |
| 288 |
// "Key": "server", |
| 289 |
// "Value": { |
| 290 |
// "Type": "STRING", |
| 291 |
// "Value": "central" |
| 292 |
// } |
| 293 |
// } |
| 294 |
// ], |
| 295 |
// "StartTime": "0001-01-01T00:00:00Z", |
| 296 |
// "Time": "0001-01-01T00:00:00Z", |
| 297 |
// "Count": 10, |
| 298 |
// "Bounds": [ |
| 299 |
// 1, |
| 300 |
// 5, |
| 301 |
// 10 |
| 302 |
// ], |
| 303 |
// "BucketCounts": [ |
| 304 |
// 1, |
| 305 |
// 3, |
| 306 |
// 6, |
| 307 |
// 0 |
| 308 |
// ], |
| 309 |
// "Min": null, |
| 310 |
// "Max": null, |
| 311 |
// "Sum": 57 |
| 312 |
// } |
| 313 |
// ], |
| 314 |
// "Temporality": "DeltaTemporality" |
| 315 |
// } |
| 316 |
// }, |
| 317 |
// { |
| 318 |
// "Name": "system.memory.usage", |
| 319 |
// "Description": "Memory usage", |
| 320 |
// "Unit": "By", |
| 321 |
// "Data": { |
| 322 |
// "DataPoints": [ |
| 323 |
// { |
| 324 |
// "Attributes": [ |
| 325 |
// { |
| 326 |
// "Key": "state", |
| 327 |
// "Value": { |
| 328 |
// "Type": "STRING", |
| 329 |
// "Value": "used" |
| 330 |
// } |
| 331 |
// } |
| 332 |
// ], |
| 333 |
// "StartTime": "0001-01-01T00:00:00Z", |
| 334 |
// "Time": "0001-01-01T00:00:00Z", |
| 335 |
// "Value": 100 |
| 336 |
// } |
| 337 |
// ] |
| 338 |
// } |
| 339 |
// }, |
| 340 |
// { |
| 341 |
// "Name": "temperature", |
| 342 |
// "Description": "CPU global temperature", |
| 343 |
// "Unit": "cel(1 K)", |
| 344 |
// "Data": { |
| 345 |
// "DataPoints": [ |
| 346 |
// { |
| 347 |
// "Attributes": [ |
| 348 |
// { |
| 349 |
// "Key": "server", |
| 350 |
// "Value": { |
| 351 |
// "Type": "STRING", |
| 352 |
// "Value": "central" |
| 353 |
// } |
| 354 |
// } |
| 355 |
// ], |
| 356 |
// "StartTime": "0001-01-01T00:00:00Z", |
| 357 |
// "Time": "0001-01-01T00:00:00Z", |
| 358 |
// "Value": 32.4 |
| 359 |
// } |
| 360 |
// ] |
| 361 |
// } |
| 362 |
// } |
| 363 |
// ] |
| 364 |
// } |
| 365 |
// ] |
| 366 |
// } |
| 367 |
// { |
| 368 |
// "Resource": [ |
| 369 |
// { |
| 370 |
// "Key": "service.name", |
| 371 |
// "Value": { |
| 372 |
// "Type": "STRING", |
| 373 |
// "Value": "stdoutmetric-example" |
| 374 |
// } |
| 375 |
// } |
| 376 |
// ], |
| 377 |
// "ScopeMetrics": null |
| 378 |
// } |
| 379 |
} |