Skip to content

Commit

Permalink
Small example of how golang metric sdk histograms behave with async i…
Browse files Browse the repository at this point in the history
…nstruments
  • Loading branch information
aabmass committed Nov 30, 2023
1 parent 1625b35 commit 0674383
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 0 deletions.
18 changes: 18 additions & 0 deletions gometrictest/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module github.com/aabmass/gometrictest

go 1.22

require (
go.opentelemetry.io/otel v1.21.0
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.44.0
go.opentelemetry.io/otel/sdk v1.21.0
go.opentelemetry.io/otel/sdk/metric v1.21.0
)

require (
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/sys v0.14.0 // indirect
)
29 changes: 29 additions & 0 deletions gometrictest/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc=
go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.44.0 h1:dEZWPjVN22urgYCza3PXRUGEyCB++y1sAqm6guWFesk=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.44.0/go.mod h1:sTt30Evb7hJB/gEk27qLb1+l9n4Tb8HvHkR0Wx3S6CU=
go.opentelemetry.io/otel/metric v1.21.0 h1:tlYWfeo+Bocx5kLEloTjbcDwBuELRrIFxwdQ36PlJu4=
go.opentelemetry.io/otel/metric v1.21.0/go.mod h1:o1p3CA8nNHW8j5yuQLdc1eeqEaPfzug24uvsyIEJRWM=
go.opentelemetry.io/otel/sdk v1.21.0 h1:FTt8qirL1EysG6sTQRZ5TokkU8d0ugCj8htOgThZXQ8=
go.opentelemetry.io/otel/sdk v1.21.0/go.mod h1:Nna6Yv7PWTdgJHVRD9hIYywQBRx7pbox6nwBnZIxl/E=
go.opentelemetry.io/otel/sdk/metric v1.21.0 h1:smhI5oD714d6jHE6Tie36fPx4WDFIg+Y6RfAY4ICcR0=
go.opentelemetry.io/otel/sdk/metric v1.21.0/go.mod h1:FJ8RAsoPGv/wYMgBdUJXOm+6pzFY3YdljnXtv1SBE8Q=
go.opentelemetry.io/otel/trace v1.21.0 h1:WD9i5gzvoUPuXIXH24ZNBudiarZDKuekPqi/E8fpfLc=
go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+BaslueVtS/qQ=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
90 changes: 90 additions & 0 deletions gometrictest/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"context"
"encoding/json"
"log"
"os"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"

semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
)

func main() {
// Create resource.
res, err := resource.Merge(resource.Default(),
resource.NewWithAttributes(semconv.SchemaURL,
semconv.ServiceName("my-service"),
semconv.ServiceVersion("0.1.0"),
))
if err != nil {
log.Fatalln(err)
}

encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
exporter, err := stdoutmetric.New(
stdoutmetric.WithEncoder(encoder),
)
if err != nil {
log.Fatalln(err)
}
reader := sdkmetric.NewPeriodicReader(exporter)

// Create a meter provider.
// You can pass this instance directly to your instrumented code if it
// accepts a MeterProvider instance.
meterProvider := sdkmetric.NewMeterProvider(
sdkmetric.WithResource(res),
sdkmetric.WithReader(reader),
sdkmetric.WithView(sdkmetric.NewView(
sdkmetric.Instrument{Name: "*"},
sdkmetric.Stream{Aggregation: sdkmetric.AggregationExplicitBucketHistogram{
Boundaries: []float64{0, 10, 100, 1000},
}},
)),
)

// Handle shutdown properly so that nothing leaks.
defer func() {
err := meterProvider.Shutdown(context.Background())
if err != nil {
log.Fatalln(err)
}
}()

// Register as global meter provider so that it can be used via otel.Meter
// and accessed using otel.GetMeterProvider.
// Most instrumentation libraries use the global meter provider as default.
// If the global meter provider is not set then a no-op implementation
// is used, which fails to generate data.
otel.SetMeterProvider(meterProvider)

_, err = otel.Meter("foo").Int64ObservableCounter(
"observableCounter",
metric.WithInt64Callback(callback),
)
if err != nil {
log.Fatalln(err)
}

_, err = otel.Meter("foo").Int64ObservableGauge(
"obseravableGauge",
metric.WithInt64Callback(callback),
)
if err != nil {
log.Fatalln(err)
}
}

func callback(ctx context.Context, io metric.Int64Observer) error {
io.Observe(12)
io.Observe(112)
io.Observe(1112)
return nil
}
114 changes: 114 additions & 0 deletions gometrictest/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"Resource": [
{
"Key": "service.name",
"Value": {
"Type": "STRING",
"Value": "my-service"
}
},
{
"Key": "service.version",
"Value": {
"Type": "STRING",
"Value": "0.1.0"
}
},
{
"Key": "telemetry.sdk.language",
"Value": {
"Type": "STRING",
"Value": "go"
}
},
{
"Key": "telemetry.sdk.name",
"Value": {
"Type": "STRING",
"Value": "opentelemetry"
}
},
{
"Key": "telemetry.sdk.version",
"Value": {
"Type": "STRING",
"Value": "1.21.0"
}
}
],
"ScopeMetrics": [
{
"Scope": {
"Name": "foo",
"Version": "",
"SchemaURL": ""
},
"Metrics": [
{
"Name": "observableCounter",
"Description": "",
"Unit": "",
"Data": {
"DataPoints": [
{
"Attributes": [],
"StartTime": "2023-11-30T18:07:30.518135377Z",
"Time": "2023-11-30T18:07:30.518191647Z",
"Count": 3,
"Bounds": [
0,
10,
100,
1000
],
"BucketCounts": [
0,
0,
1,
1,
1
],
"Min": {},
"Max": {},
"Sum": 1236
}
],
"Temporality": "CumulativeTemporality"
}
},
{
"Name": "obseravableGauge",
"Description": "",
"Unit": "",
"Data": {
"DataPoints": [
{
"Attributes": [],
"StartTime": "2023-11-30T18:07:30.518142767Z",
"Time": "2023-11-30T18:07:30.518192677Z",
"Count": 3,
"Bounds": [
0,
10,
100,
1000
],
"BucketCounts": [
0,
0,
1,
1,
1
],
"Min": {},
"Max": {},
"Sum": 0
}
],
"Temporality": "CumulativeTemporality"
}
}
]
}
]
}

0 comments on commit 0674383

Please sign in to comment.