Created
February 9, 2023 17:11
-
-
Save joaopgrassi/ea58ca27dc9f7f5baa804f556d9cb2d2 to your computer and use it in GitHub Desktop.
OTel Go exporting metrics via OTLP HTTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"math/rand" | |
"os" | |
"time" | |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" | |
"go.opentelemetry.io/otel/metric/global" | |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" | |
"go.opentelemetry.io/otel/sdk/metric/metricdata" | |
) | |
// Mapper function to instrument > temporality | |
// See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk_exporters/otlp.md#additional-configuration | |
func DeltaTemporality(ik sdkmetric.InstrumentKind) metricdata.Temporality { | |
switch ik { | |
case sdkmetric.InstrumentKindCounter, sdkmetric.InstrumentKindObservableCounter, sdkmetric.InstrumentKindHistogram: | |
return metricdata.DeltaTemporality | |
case sdkmetric.InstrumentKindUpDownCounter, sdkmetric.InstrumentKindObservableUpDownCounter: | |
return metricdata.CumulativeTemporality | |
default: | |
return metricdata.CumulativeTemporality | |
} | |
} | |
func main() { | |
headers := make(map[string]string) | |
headers["Authorization"] = "Api-Token token" | |
eopts := []otlpmetrichttp.Option{ | |
otlpmetrichttp.WithEndpoint("dynatrace url"), | |
otlpmetrichttp.WithURLPath("api/v2/otlp/v1/metrics"), | |
otlpmetrichttp.WithHeaders(headers), | |
// Configure the exporter with the correct temporality | |
otlpmetrichttp.WithTemporalitySelector(DeltaTemporality), | |
} | |
exporter, err := otlpmetrichttp.New(context.Background(), eopts...) | |
if err != nil { | |
panic(err) | |
} | |
meterProvider := sdkmetric.NewMeterProvider(sdkmetric.WithReader( | |
sdkmetric.NewPeriodicReader(exporter, sdkmetric.WithInterval(2*time.Second)), | |
)) | |
global.SetMeterProvider(meterProvider) | |
meter := global.Meter("my.app") | |
counter, err := meter.Int64Counter("my.counter") | |
for { | |
counter.Add(context.Background(), int64(rand.Intn(20))) | |
time.Sleep(time.Second * 5) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment