2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-06-14 22:09:41 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-06-14 20:37:05 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-04 22:23:23 +02:00
|
|
|
"log"
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2020-01-28 20:13:46 +02:00
|
|
|
"go.opentelemetry.io/otel/api/correlation"
|
2019-11-26 06:41:24 +02:00
|
|
|
"go.opentelemetry.io/otel/api/global"
|
2020-05-14 01:06:03 +02:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
|
|
|
"go.opentelemetry.io/otel/api/trace"
|
2020-03-02 23:54:57 +02:00
|
|
|
metricstdout "go.opentelemetry.io/otel/exporters/metric/stdout"
|
|
|
|
tracestdout "go.opentelemetry.io/otel/exporters/trace/stdout"
|
2019-11-15 23:01:20 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/controller/push"
|
2019-11-04 22:23:23 +02:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-05-14 01:21:23 +02:00
|
|
|
fooKey = kv.Key("ex.com/foo")
|
|
|
|
barKey = kv.Key("ex.com/bar")
|
|
|
|
lemonsKey = kv.Key("ex.com/lemons")
|
|
|
|
anotherKey = kv.Key("ex.com/another")
|
2019-10-23 08:29:24 +02:00
|
|
|
)
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-11-04 22:23:23 +02:00
|
|
|
// initTracer creates and registers trace provider instance.
|
|
|
|
func initTracer() {
|
|
|
|
var err error
|
2019-11-15 23:01:20 +02:00
|
|
|
exp, err := tracestdout.NewExporter(tracestdout.Options{PrettyPrint: false})
|
2019-11-04 22:23:23 +02:00
|
|
|
if err != nil {
|
2019-11-15 23:01:20 +02:00
|
|
|
log.Panicf("failed to initialize trace stdout exporter %v", err)
|
2019-11-04 22:23:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
tp, err := sdktrace.NewProvider(sdktrace.WithSyncer(exp),
|
2020-03-17 01:48:35 +02:00
|
|
|
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
|
2020-05-14 01:06:03 +02:00
|
|
|
sdktrace.WithResourceAttributes(kv.String("rk1", "rv11"), kv.Int64("rk2", 5)))
|
2019-11-04 22:23:23 +02:00
|
|
|
if err != nil {
|
2019-11-15 23:01:20 +02:00
|
|
|
log.Panicf("failed to initialize trace provider %v", err)
|
2019-11-04 22:23:23 +02:00
|
|
|
}
|
|
|
|
global.SetTraceProvider(tp)
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
func initMeter() *push.Controller {
|
2020-01-03 19:48:45 +02:00
|
|
|
pusher, err := metricstdout.InstallNewPipeline(metricstdout.Config{
|
2019-11-15 23:01:20 +02:00
|
|
|
Quantiles: []float64{0.5, 0.9, 0.99},
|
|
|
|
PrettyPrint: false,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("failed to initialize metric stdout exporter %v", err)
|
|
|
|
}
|
|
|
|
return pusher
|
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
func main() {
|
2019-11-15 23:01:20 +02:00
|
|
|
defer initMeter().Stop()
|
2019-11-04 22:23:23 +02:00
|
|
|
initTracer()
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-03-11 17:23:32 +02:00
|
|
|
tracer := global.Tracer("ex.com/basic")
|
|
|
|
meter := global.Meter("ex.com/basic")
|
2019-11-04 22:23:23 +02:00
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
commonLabels := []kv.KeyValue{lemonsKey.Int(10), kv.String("A", "1"), kv.String("B", "2"), kv.String("C", "3")}
|
2020-03-11 01:00:37 +02:00
|
|
|
|
2020-05-20 06:33:10 +02:00
|
|
|
oneMetricCB := func(_ context.Context, result metric.Float64ObserverResult) {
|
2020-03-27 23:06:48 +02:00
|
|
|
result.Observe(1, commonLabels...)
|
2020-03-11 01:00:37 +02:00
|
|
|
}
|
2020-05-18 20:03:43 +02:00
|
|
|
_ = metric.Must(meter).RegisterFloat64ValueObserver("ex.com.one", oneMetricCB,
|
|
|
|
metric.WithDescription("A ValueObserver set to 1.0"),
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorderTwo := metric.Must(meter).NewFloat64ValueRecorder("ex.com.two")
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-01-28 20:13:46 +02:00
|
|
|
ctx = correlation.NewContext(ctx,
|
2019-10-30 22:21:13 +02:00
|
|
|
fooKey.String("foo1"),
|
|
|
|
barKey.String("bar1"),
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder := valuerecorderTwo.Bind(commonLabels...)
|
|
|
|
defer valuerecorder.Unbind()
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
err := tracer.WithSpan(ctx, "operation", func(ctx context.Context) error {
|
|
|
|
|
2020-05-14 01:21:23 +02:00
|
|
|
trace.SpanFromContext(ctx).AddEvent(ctx, "Nice operation!", kv.Key("bogons").Int(100))
|
2019-06-24 19:35:16 +02:00
|
|
|
|
2019-12-11 18:51:32 +02:00
|
|
|
trace.SpanFromContext(ctx).SetAttributes(anotherKey.String("yes"))
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-10-09 00:45:49 +02:00
|
|
|
meter.RecordBatch(
|
2019-10-10 23:30:22 +02:00
|
|
|
// Note: call-site variables added as context Entries:
|
2020-01-28 20:13:46 +02:00
|
|
|
correlation.NewContext(ctx, anotherKey.String("xyz")),
|
2019-10-09 00:45:49 +02:00
|
|
|
commonLabels,
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorderTwo.Measurement(2.0),
|
2019-10-09 00:45:49 +02:00
|
|
|
)
|
|
|
|
|
2019-06-14 20:37:05 +02:00
|
|
|
return tracer.WithSpan(
|
|
|
|
ctx,
|
|
|
|
"Sub operation...",
|
|
|
|
func(ctx context.Context) error {
|
2019-12-11 18:51:32 +02:00
|
|
|
trace.SpanFromContext(ctx).SetAttributes(lemonsKey.String("five"))
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-12-11 18:51:32 +02:00
|
|
|
trace.SpanFromContext(ctx).AddEvent(ctx, "Sub span event")
|
2019-06-24 19:35:16 +02:00
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder.Record(ctx, 1.3)
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|