2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-09-09 14:59:39 -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.
|
|
|
|
|
|
|
|
// Command jaeger is an example program that creates spans
|
|
|
|
// and uploads to Jaeger.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
|
2019-11-26 12:41:24 +08:00
|
|
|
"go.opentelemetry.io/otel/api/global"
|
2020-05-14 07:06:03 +08:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2019-10-17 13:59:30 -03:00
|
|
|
|
2020-03-02 13:54:57 -08:00
|
|
|
"go.opentelemetry.io/otel/exporters/trace/jaeger"
|
2019-11-01 11:40:29 -07:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2019-09-09 14:59:39 -07:00
|
|
|
)
|
|
|
|
|
2019-10-22 13:19:11 -07:00
|
|
|
// initTracer creates a new trace provider instance and registers it as global trace provider.
|
|
|
|
func initTracer() func() {
|
2020-03-10 21:58:45 +01:00
|
|
|
// Create and install Jaeger export pipeline
|
|
|
|
_, flush, err := jaeger.NewExportPipeline(
|
2019-10-04 16:07:42 -03:00
|
|
|
jaeger.WithCollectorEndpoint("http://localhost:14268/api/traces"),
|
|
|
|
jaeger.WithProcess(jaeger.Process{
|
2019-09-09 14:59:39 -07:00
|
|
|
ServiceName: "trace-demo",
|
2020-05-14 07:06:03 +08:00
|
|
|
Tags: []kv.KeyValue{
|
|
|
|
kv.String("exporter", "jaeger"),
|
|
|
|
kv.Float64("float", 312.23),
|
2019-10-17 13:59:30 -03:00
|
|
|
},
|
2019-10-04 16:07:42 -03:00
|
|
|
}),
|
2020-03-10 21:58:45 +01:00
|
|
|
jaeger.RegisterAsGlobal(),
|
|
|
|
jaeger.WithSDK(&sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
|
2019-10-04 16:07:42 -03:00
|
|
|
)
|
2019-09-09 14:59:39 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:19:11 -07:00
|
|
|
return func() {
|
2020-03-10 21:58:45 +01:00
|
|
|
flush()
|
2019-10-22 13:19:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fn := initTracer()
|
|
|
|
defer fn()
|
2019-09-09 14:59:39 -07:00
|
|
|
|
2019-10-22 13:19:11 -07:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-03-11 12:23:32 -03:00
|
|
|
tr := global.Tracer("component-main")
|
2019-11-06 01:18:09 +08:00
|
|
|
ctx, span := tr.Start(ctx, "foo")
|
2019-09-09 14:59:39 -07:00
|
|
|
bar(ctx)
|
2019-09-27 10:48:10 -07:00
|
|
|
span.End()
|
2019-09-09 14:59:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func bar(ctx context.Context) {
|
2020-03-11 12:23:32 -03:00
|
|
|
tr := global.Tracer("component-bar")
|
2019-11-06 01:18:09 +08:00
|
|
|
_, span := tr.Start(ctx, "bar")
|
2019-09-27 10:48:10 -07:00
|
|
|
defer span.End()
|
2019-09-09 14:59:39 -07:00
|
|
|
|
|
|
|
// Do bar...
|
|
|
|
}
|