1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-26 03:52:03 +02:00

76 lines
2.4 KiB
Go
Raw Normal View History

2020-05-11 12:10:12 +02:00
// Copyright The OpenTelemetry Authors
//
// 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.
// Example from otlp package: https://pkg.go.dev/go.opentelemetry.io/otel/exporters/otlp?tab=doc#example-package-Insecure
package main
import (
"context"
"fmt"
"log"
"time"
2020-05-11 12:42:36 +02:00
"google.golang.org/grpc"
2020-05-15 12:57:21 +02:00
"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/exporters/otlp"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
2020-05-13 12:38:23 +02:00
"github.com/open-telemetry/opentelemetry-collector/translator/conventions"
)
func main() {
2020-05-15 11:46:53 +02:00
// If the OpenTelemetry Collector is running on a local cluster (minikube or microk8s),
// it should be accessible through the NodePort service at the `localhost:30080` address.
// Otherwise, replace `localhost` with the address of your cluster.
// If you run the app inside k8s, then you can probably connect directly to the service through dns
exp, err := otlp.NewExporter(otlp.WithInsecure(),
2020-05-15 11:46:53 +02:00
otlp.WithAddress("localhost:30080"),
otlp.WithGRPCDialOption(grpc.WithBlock()))
if err != nil {
log.Fatalf("Failed to create the collector exporter: %v", err)
}
defer func() {
err := exp.Stop()
if err != nil {
log.Fatalf("Failed to stop the exporter: %v", err)
}
}()
tp, err := sdktrace.NewProvider(
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
sdktrace.WithResource(resource.New(
2020-05-15 11:46:53 +02:00
// the service name used to display traces in Jaeger
2020-05-15 12:57:21 +02:00
kv.Key(conventions.AttributeServiceName).String("test-service"),
)),
sdktrace.WithSyncer(exp))
if err != nil {
log.Fatalf("error creating trace provider: %v\n", err)
}
2020-05-13 12:38:23 +02:00
2020-05-11 12:10:12 +02:00
tracer := tp.Tracer("test-tracer")
// Then use the OpenTelemetry tracing library, like we normally would.
ctx, span := tracer.Start(context.Background(), "CollectorExporter-Example")
for i := 0; i < 10; i++ {
_, iSpan := tracer.Start(ctx, fmt.Sprintf("Sample-%d", i))
2020-05-11 12:10:12 +02:00
<-time.After(time.Second)
iSpan.End()
}
2020-05-20 10:38:34 +02:00
span.End()
}