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"
|
|
|
|
|
2020-01-28 19:13:46 +01:00
|
|
|
"go.opentelemetry.io/otel/api/correlation"
|
2019-11-26 12:41:24 +08:00
|
|
|
"go.opentelemetry.io/otel/api/global"
|
2019-11-01 11:40:29 -07:00
|
|
|
"go.opentelemetry.io/otel/api/trace"
|
|
|
|
"go.opentelemetry.io/otel/example/namedtracer/foo"
|
2020-07-22 12:34:44 -07:00
|
|
|
"go.opentelemetry.io/otel/exporters/stdout"
|
2020-08-17 20:25:03 -07:00
|
|
|
"go.opentelemetry.io/otel/label"
|
2019-11-01 11:40:29 -07:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2019-10-22 13:19:11 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-08-17 20:25:03 -07:00
|
|
|
fooKey = label.Key("ex.com/foo")
|
|
|
|
barKey = label.Key("ex.com/bar")
|
|
|
|
anotherKey = label.Key("ex.com/another")
|
2019-10-22 13:19:11 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var tp *sdktrace.Provider
|
|
|
|
|
|
|
|
// initTracer creates and registers trace provider instance.
|
|
|
|
func initTracer() {
|
|
|
|
var err error
|
2020-07-22 12:34:44 -07:00
|
|
|
exp, err := stdout.NewExporter(stdout.WithPrettyPrint())
|
2019-10-22 13:19:11 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Panicf("failed to initialize stdout exporter %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2020-08-08 00:16:43 +08:00
|
|
|
tp, err = sdktrace.NewProvider(sdktrace.WithBatcher(exp),
|
2019-10-22 13:19:11 -07:00
|
|
|
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}))
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("failed to initialize trace provider %v\n", err)
|
|
|
|
}
|
2019-10-25 11:38:52 -07:00
|
|
|
global.SetTraceProvider(tp)
|
2019-10-22 13:19:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// initialize trace provider.
|
|
|
|
initTracer()
|
|
|
|
|
|
|
|
// 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-01-28 19:13:46 +01:00
|
|
|
ctx = correlation.NewContext(ctx,
|
2019-10-30 13:21:13 -07:00
|
|
|
fooKey.String("foo1"),
|
|
|
|
barKey.String("bar1"),
|
2019-10-22 13:19:11 -07:00
|
|
|
)
|
|
|
|
|
2020-08-08 12:10:36 -07:00
|
|
|
var span trace.Span
|
|
|
|
ctx, span = tracer.Start(ctx, "operation")
|
|
|
|
defer span.End()
|
2020-08-17 20:25:03 -07:00
|
|
|
span.AddEvent(ctx, "Nice operation!", label.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)
|
|
|
|
}
|
|
|
|
}
|