2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-10-22 13:19:11 -07: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"
|
|
|
|
|
2021-11-19 15:34:11 +00:00
|
|
|
"github.com/go-logr/stdr"
|
|
|
|
|
2020-11-16 18:30:54 +01:00
|
|
|
"go.opentelemetry.io/otel"
|
2021-02-18 12:59:37 -05:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-11-13 16:34:24 +01:00
|
|
|
"go.opentelemetry.io/otel/baggage"
|
2019-11-01 11:40:29 -07:00
|
|
|
"go.opentelemetry.io/otel/example/namedtracer/foo"
|
2021-06-16 11:32:42 -04:00
|
|
|
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
|
2019-11-01 11:40:29 -07:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2020-11-06 23:13:31 +01:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2019-10-22 13:19:11 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-02-18 12:59:37 -05:00
|
|
|
fooKey = attribute.Key("ex.com/foo")
|
|
|
|
barKey = attribute.Key("ex.com/bar")
|
|
|
|
anotherKey = attribute.Key("ex.com/another")
|
2019-10-22 13:19:11 -07:00
|
|
|
)
|
|
|
|
|
2020-09-23 15:16:13 -07:00
|
|
|
var tp *sdktrace.TracerProvider
|
2019-10-22 13:19:11 -07:00
|
|
|
|
|
|
|
// initTracer creates and registers trace provider instance.
|
2020-10-26 12:20:49 -04:00
|
|
|
func initTracer() {
|
2019-10-22 13:19:11 -07:00
|
|
|
var err error
|
2021-06-16 11:32:42 -04:00
|
|
|
exp, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
|
2019-10-22 13:19:11 -07:00
|
|
|
if err != nil {
|
2021-06-16 11:32:42 -04:00
|
|
|
log.Panicf("failed to initialize stdouttrace exporter %v\n", err)
|
2020-10-26 12:20:49 -04:00
|
|
|
return
|
2019-10-22 13:19:11 -07:00
|
|
|
}
|
2020-09-24 15:25:20 -07:00
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(exp)
|
2020-09-23 15:16:13 -07:00
|
|
|
tp = sdktrace.NewTracerProvider(
|
2021-03-18 16:34:47 +00:00
|
|
|
sdktrace.WithSampler(sdktrace.AlwaysSample()),
|
2020-09-24 15:25:20 -07:00
|
|
|
sdktrace.WithSpanProcessor(bsp),
|
2020-09-09 10:19:03 -07:00
|
|
|
)
|
2020-11-16 18:30:54 +01:00
|
|
|
otel.SetTracerProvider(tp)
|
2019-10-22 13:19:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-11-19 15:34:11 +00:00
|
|
|
// Set logging level to info to see SDK status messages
|
|
|
|
stdr.SetVerbosity(1)
|
|
|
|
|
2019-10-22 13:19:11 -07:00
|
|
|
// initialize trace provider.
|
2020-10-26 12:20:49 -04:00
|
|
|
initTracer()
|
2019-10-22 13:19:11 -07:00
|
|
|
|
|
|
|
// Create a named tracer with package path as its name.
|
2019-11-25 18:46:07 +01:00
|
|
|
tracer := tp.Tracer("example/namedtracer/main")
|
2019-10-22 13:19:11 -07:00
|
|
|
ctx := context.Background()
|
2020-10-26 12:20:49 -04:00
|
|
|
defer func() { _ = tp.Shutdown(ctx) }()
|
2021-06-08 15:06:37 +00: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 13:19:11 -07:00
|
|
|
|
2020-11-06 23:13:31 +01:00
|
|
|
var span trace.Span
|
2020-08-08 12:10:36 -07:00
|
|
|
ctx, span = tracer.Start(ctx, "operation")
|
|
|
|
defer span.End()
|
2021-02-18 12:59:37 -05:00
|
|
|
span.AddEvent("Nice operation!", trace.WithAttributes(attribute.Int("bogons", 100)))
|
2020-08-08 12:10:36 -07:00
|
|
|
span.SetAttributes(anotherKey.String("yes"))
|
|
|
|
if err := foo.SubOperation(ctx); err != nil {
|
2019-10-22 13:19:11 -07:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|