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-07-22 21:34:44 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/stdout"
|
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
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
func main() {
|
|
|
|
pusher, err := stdout.InstallNewPipeline([]stdout.Option{
|
|
|
|
stdout.WithQuantiles([]float64{0.5, 0.9, 0.99}),
|
|
|
|
stdout.WithPrettyPrint(),
|
|
|
|
}, nil)
|
2019-11-15 23:01:20 +02:00
|
|
|
if err != nil {
|
2020-07-22 21:34:44 +02:00
|
|
|
log.Fatalf("failed to initialize stdout export pipeline: %v", err)
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
2020-07-22 21:34:44 +02:00
|
|
|
defer pusher.Stop()
|
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-22 00:42:14 +02:00
|
|
|
_ = metric.Must(meter).NewFloat64ValueObserver("ex.com.one", oneMetricCB,
|
2020-05-18 20:03:43 +02:00
|
|
|
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
|
|
|
|
2020-07-22 21:34:44 +02:00
|
|
|
err = tracer.WithSpan(ctx, "operation", func(ctx context.Context) error {
|
2019-06-14 20:37:05 +02:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|