1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-10 09:50:58 +02:00

Add OTLP metrics

This commit is contained in:
wtong98 2020-05-26 15:24:16 -05:00
parent 69ddc81a81
commit a4cf6e1d69
2 changed files with 41 additions and 12 deletions

View File

@ -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=

View File

@ -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()
}