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"
|
2022-05-19 22:15:07 +02:00
|
|
|
"fmt"
|
2019-10-22 22:19:11 +02:00
|
|
|
"log"
|
|
|
|
|
2021-11-19 17:34:11 +02:00
|
|
|
"github.com/go-logr/stdr"
|
|
|
|
|
2020-11-16 19:30:54 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-11-13 17:34:24 +02:00
|
|
|
"go.opentelemetry.io/otel/baggage"
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/example/namedtracer/foo"
|
2021-06-16 17:32:42 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
|
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 (
|
2021-02-18 19:59:37 +02:00
|
|
|
fooKey = attribute.Key("ex.com/foo")
|
|
|
|
barKey = attribute.Key("ex.com/bar")
|
|
|
|
anotherKey = attribute.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.
|
2022-05-19 22:15:07 +02:00
|
|
|
func initTracer() error {
|
2021-06-16 17:32:42 +02:00
|
|
|
exp, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
|
2019-10-22 22:19:11 +02:00
|
|
|
if err != nil {
|
2022-05-19 22:15:07 +02:00
|
|
|
return fmt.Errorf("failed to initialize stdouttrace exporter: %w", err)
|
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(
|
2021-03-18 18:34:47 +02:00
|
|
|
sdktrace.WithSampler(sdktrace.AlwaysSample()),
|
2020-09-25 00:25:20 +02:00
|
|
|
sdktrace.WithSpanProcessor(bsp),
|
2020-09-09 19:19:03 +02:00
|
|
|
)
|
2020-11-16 19:30:54 +02:00
|
|
|
otel.SetTracerProvider(tp)
|
2022-05-19 22:15:07 +02:00
|
|
|
return nil
|
2019-10-22 22:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-11-19 17:34:11 +02:00
|
|
|
// Set logging level to info to see SDK status messages
|
2022-01-11 02:58:01 +02:00
|
|
|
stdr.SetVerbosity(5)
|
2021-11-19 17:34:11 +02:00
|
|
|
|
2019-10-22 22:19:11 +02:00
|
|
|
// initialize trace provider.
|
2022-05-19 22:15:07 +02:00
|
|
|
if err := initTracer(); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
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) }()
|
2021-06-08 17:06:37 +02:00
|
|
|
|
|
|
|
m0, _ := baggage.NewMember(string(fooKey), "foo1")
|
|
|
|
m1, _ := baggage.NewMember(string(barKey), "bar1")
|
|
|
|
b, _ := baggage.New(m0, m1)
|
|
|
|
ctx = baggage.ContextWithBaggage(ctx, b)
|
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()
|
2021-02-18 19:59:37 +02:00
|
|
|
span.AddEvent("Nice operation!", trace.WithAttributes(attribute.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)
|
|
|
|
}
|
|
|
|
}
|