From a4cf6e1d69ace51097420bc20d07d843a3fe0bfb Mon Sep 17 00:00:00 2001 From: wtong98 Date: Tue, 26 May 2020 15:24:16 -0500 Subject: [PATCH] Add OTLP metrics --- example/otlp/go.sum | 1 + example/otlp/main.go | 52 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/example/otlp/go.sum b/example/otlp/go.sum index 6218dbdb3..06b30f919 100644 --- a/example/otlp/go.sum +++ b/example/otlp/go.sum @@ -21,6 +21,7 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/DataDog/datadog-go v2.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= +github.com/DataDog/sketches-go v0.0.0-20190923095040-43f19ad77ff7 h1:qELHH0AWCvf98Yf+CNIJx9vOZOfHFDDzgDRYsnNk/vs= github.com/DataDog/sketches-go v0.0.0-20190923095040-43f19ad77ff7/go.mod h1:Q5DbzQ+3AkgGwymQO7aZFNP7ns2lZKGtvRBzRXfdi60= github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA= diff --git a/example/otlp/main.go b/example/otlp/main.go index acd721aa8..7e36f13fa 100644 --- a/example/otlp/main.go +++ b/example/otlp/main.go @@ -8,43 +8,71 @@ import ( "log" "time" - // "github.com/open-telemetry/opentelemetry-collector/translator/conventions" - "go.opentelemetry.io/otel/api/global" - // "go.opentelemetry.io/otel/api/kv" - // "go.opentelemetry.io/otel/api/metric" + "go.opentelemetry.io/otel/api/kv" + "go.opentelemetry.io/otel/api/metric" "go.opentelemetry.io/otel/exporters/otlp" + "go.opentelemetry.io/otel/sdk/metric/controller/push" + "go.opentelemetry.io/otel/sdk/metric/selector/simple" sdktrace "go.opentelemetry.io/otel/sdk/trace" - // tracestdout "go.opentelemetry.io/otel/exporters/trace/stdout" ) -func initExporter() { +// TODO: basic documentation +func initProvider() { exp, err := otlp.NewExporter( otlp.WithInsecure(), otlp.WithAddress("localhost:55680"), ) - // exp, err := tracestdout.NewExporter(tracestdout.Options{PrettyPrint: true}) handleErr(err, "Failed to create exporter: $v") - // defer handleErr(exp.Stop(), "Failed to stop exporter: %v") - - provider, err := sdktrace.NewProvider( + traceProvider, err := sdktrace.NewProvider( sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}), sdktrace.WithSyncer(exp), ) handleErr(err, "Failed to create trace provider: %v") - global.SetTraceProvider(provider) + pusher := push.New( + simple.NewWithExactDistribution(), + exp, + push.WithStateful(true), + push.WithPeriod(time.Duration(5) * time.Second), + ) + + global.SetTraceProvider(traceProvider) + global.SetMeterProvider(pusher.Provider()) + pusher.Start() } + func main() { - initExporter() + initProvider() + tracer := global.Tracer("mage-sense") + meter := global.Meter("mage-read") + + commonLabels := []kv.KeyValue{ + kv.String("work-room", "East Scriptorium"), + kv.String("occupancy", "69,105"), + kv.String("priority", "Ultra"), + } + + oneMetricCB := func(_ context.Context, result metric.Float64ObserverResult) { + result.Observe(1, commonLabels...) + } + _ = metric.Must(meter).NewFloat64ValueObserver("scrying.glass.one", oneMetricCB, + metric.WithDescription("A ValueObserver set to 1.0"), + ) + + valuerecorder := metric.Must(meter). + NewFloat64ValueRecorder("scrying.glass.two"). + Bind(commonLabels...) + defer valuerecorder.Unbind() ctx, span := tracer.Start(context.Background(), "Archmage-Overlord") for i := 0; i < 10; i++ { _, innerSpan := tracer.Start(ctx, fmt.Sprintf("Minion-%d", i)) log.Println("Minions hard at work, scribing...") + valuerecorder.Record(ctx, float64(i) * 1.5) <-time.After(time.Second) innerSpan.End() }