2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-09-09 23:59:39 +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.
|
|
|
|
|
|
|
|
// Command jaeger is an example program that creates spans
|
|
|
|
// and uploads to Jaeger.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
2021-04-21 01:48:34 +02:00
|
|
|
"time"
|
2019-09-09 23:59:39 +02:00
|
|
|
|
2020-11-16 19:30:54 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2019-10-17 18:59:30 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-03-02 23:54:57 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/trace/jaeger"
|
2021-04-21 01:48:34 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
|
|
|
tracesdk "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
"go.opentelemetry.io/otel/semconv"
|
2019-09-09 23:59:39 +02:00
|
|
|
)
|
|
|
|
|
2021-04-21 01:48:34 +02:00
|
|
|
const (
|
|
|
|
service = "trace-demo"
|
|
|
|
environment = "production"
|
|
|
|
id = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
// tracerProvider returns an OpenTelemetry TracerProvider configured to use
|
|
|
|
// the Jaeger exporter that will send spans to the provided url. The returned
|
|
|
|
// TracerProvider will also use a Resource configured with all the information
|
|
|
|
// about the application.
|
|
|
|
func tracerProvider(url string) (*tracesdk.TracerProvider, error) {
|
|
|
|
// Create the Jaeger exporter
|
2021-04-21 21:02:06 +02:00
|
|
|
exp, err := jaeger.NewRawExporter(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(url)))
|
2021-04-21 01:48:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tp := tracesdk.NewTracerProvider(
|
|
|
|
// Always be sure to batch in production.
|
|
|
|
tracesdk.WithBatcher(exp),
|
|
|
|
// Record information about this application in an Resource.
|
|
|
|
tracesdk.WithResource(resource.NewWithAttributes(
|
|
|
|
semconv.ServiceNameKey.String(service),
|
|
|
|
attribute.String("environment", environment),
|
|
|
|
attribute.Int64("ID", id),
|
|
|
|
)),
|
2019-10-04 21:07:42 +02:00
|
|
|
)
|
2021-04-21 01:48:34 +02:00
|
|
|
return tp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
tp, err := tracerProvider("http://localhost:14268/api/traces")
|
2019-09-09 23:59:39 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-04-21 01:48:34 +02:00
|
|
|
|
|
|
|
// Register our TracerProvider as the global so any imported
|
|
|
|
// instrumentation in the future will default to using it.
|
|
|
|
otel.SetTracerProvider(tp)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Cleanly shutdown and flush telemetry when the application exits.
|
|
|
|
defer func(ctx context.Context) {
|
|
|
|
// Do not make the application hang when it is shutdown.
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, time.Second*5)
|
|
|
|
defer cancel()
|
|
|
|
if err := tp.Shutdown(ctx); err != nil {
|
2021-04-20 18:31:59 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-04-21 01:48:34 +02:00
|
|
|
}(ctx)
|
2019-10-22 22:19:11 +02:00
|
|
|
|
2021-04-20 18:31:59 +02:00
|
|
|
tr := tp.Tracer("component-main")
|
2020-10-28 17:08:13 +02:00
|
|
|
|
2019-11-05 19:18:09 +02:00
|
|
|
ctx, span := tr.Start(ctx, "foo")
|
2020-10-28 17:08:13 +02:00
|
|
|
defer span.End()
|
|
|
|
|
2019-09-09 23:59:39 +02:00
|
|
|
bar(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func bar(ctx context.Context) {
|
2021-04-20 18:31:59 +02:00
|
|
|
// Use the global TracerProvider.
|
2020-11-16 19:30:54 +02:00
|
|
|
tr := otel.Tracer("component-bar")
|
2019-11-05 19:18:09 +02:00
|
|
|
_, span := tr.Start(ctx, "bar")
|
2021-03-16 18:04:46 +02:00
|
|
|
span.SetAttributes(attribute.Key("testset").String("value"))
|
2019-09-27 19:48:10 +02:00
|
|
|
defer span.End()
|
2019-09-09 23:59:39 +02:00
|
|
|
|
|
|
|
// Do bar...
|
|
|
|
}
|