2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-10-22 22:19:11 +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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
|
2020-10-05 17:25:09 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/example/namedtracer/foo"
|
2020-07-22 21:34:44 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/stdout"
|
2020-10-17 19:03:48 +02:00
|
|
|
"go.opentelemetry.io/otel/global"
|
2020-08-18 05:25:03 +02:00
|
|
|
"go.opentelemetry.io/otel/label"
|
2019-11-01 20:40:29 +02:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2020-11-07 00:13:31 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2019-10-22 22:19:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-08-18 05:25:03 +02:00
|
|
|
fooKey = label.Key("ex.com/foo")
|
|
|
|
barKey = label.Key("ex.com/bar")
|
|
|
|
anotherKey = label.Key("ex.com/another")
|
2019-10-22 22:19:11 +02:00
|
|
|
)
|
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
var tp *sdktrace.TracerProvider
|
2019-10-22 22:19:11 +02:00
|
|
|
|
|
|
|
// initTracer creates and registers trace provider instance.
|
2020-10-26 18:20:49 +02:00
|
|
|
func initTracer() {
|
2019-10-22 22:19:11 +02:00
|
|
|
var err error
|
2020-07-22 21:34:44 +02:00
|
|
|
exp, err := stdout.NewExporter(stdout.WithPrettyPrint())
|
2019-10-22 22:19:11 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Panicf("failed to initialize stdout exporter %v\n", err)
|
2020-10-26 18:20:49 +02:00
|
|
|
return
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
2020-09-25 00:25:20 +02:00
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(exp)
|
2020-09-24 00:16:13 +02:00
|
|
|
tp = sdktrace.NewTracerProvider(
|
2020-09-09 19:19:03 +02:00
|
|
|
sdktrace.WithConfig(
|
|
|
|
sdktrace.Config{
|
|
|
|
DefaultSampler: sdktrace.AlwaysSample(),
|
|
|
|
},
|
|
|
|
),
|
2020-09-25 00:25:20 +02:00
|
|
|
sdktrace.WithSpanProcessor(bsp),
|
2020-09-09 19:19:03 +02:00
|
|
|
)
|
2020-08-31 19:02:04 +02:00
|
|
|
global.SetTracerProvider(tp)
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// initialize trace provider.
|
2020-10-26 18:20:49 +02:00
|
|
|
initTracer()
|
2019-10-22 22:19:11 +02:00
|
|
|
|
|
|
|
// Create a named tracer with package path as its name.
|
2019-11-25 19:46:07 +02:00
|
|
|
tracer := tp.Tracer("example/namedtracer/main")
|
2019-10-22 22:19:11 +02:00
|
|
|
ctx := context.Background()
|
2020-10-26 18:20:49 +02:00
|
|
|
defer func() { _ = tp.Shutdown(ctx) }()
|
2020-10-05 17:25:09 +02:00
|
|
|
ctx = otel.ContextWithBaggageValues(ctx, fooKey.String("foo1"), barKey.String("bar1"))
|
2019-10-22 22:19:11 +02:00
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
var span trace.Span
|
2020-08-08 21:10:36 +02:00
|
|
|
ctx, span = tracer.Start(ctx, "operation")
|
|
|
|
defer span.End()
|
2020-11-07 00:13:31 +02:00
|
|
|
span.AddEvent("Nice operation!", trace.WithAttributes(label.Int("bogons", 100)))
|
2020-08-08 21:10:36 +02:00
|
|
|
span.SetAttributes(anotherKey.String("yes"))
|
|
|
|
if err := foo.SubOperation(ctx); err != nil {
|
2019-10-22 22:19:11 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|