opentelemetry

package
v1.59.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2024 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 19 Imported by: 23

Documentation

Overview

Package opentelemetry provides a wrapper on top of the Datadog tracer that can be used with OpenTelemetry. This feature is currently in beta. It also provides a wrapper around TracerProvider to propagate a list of tracer.StartOption that are specific to Datadog's APM product. To use it, simply call "NewTracerProvider".

provider := opentelemetry.NewTracerProvider(tracer.WithService("opentelemetry_service"))

When using Datadog, the OpenTelemetry span name is what is called operation name in Datadog's terms. Below is an example setting the tracer provider, initializing a tracer, and creating a span.

otel.SetTracerProvider(opentelemetry.NewTracerProvider())
tracer := otel.Tracer("")
ctx, sp := tracer.Start(context.Background(), "span_name")
yourCode(ctx)
sp.End()

Not every feature provided by OpenTelemetry is supported with this wrapper today, and any new API methods added to the OpenTelemetry API will default to being a no-op until implemented by this library. See the OpenTelemetry package docs for more details: https://pkg.go.dev/go.opentelemetry.io/otel/trace#hdr-API_Implementations. This package seeks to implement a minimal set of functions within the OpenTelemetry Tracing API (https://opentelemetry.io/docs/reference/specification/trace/api) to allow users to send traces to Datadog using existing OpenTelemetry code with minimal changes to the application. Span events (https://opentelemetry.io/docs/concepts/signals/traces/#span-events) are not supported at this time.

Example
package main

import (
	"context"
	"log"
	"os"

	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
	ddotel "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentelemetry"

	ddtracer "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/attribute"
)

func main() {
	// Create a TracerProvider, optionally providing a set of options,
	// that are specific to Datadog's APM product, and defer the Shutdown method, which stops the tracer.
	provider := ddotel.NewTracerProvider(ddtracer.WithProfilerCodeHotspots(true))
	defer provider.Shutdown()

	// Use it with the OpenTelemetry API to set the global TracerProvider.
	otel.SetTracerProvider(provider)

	// Start the Tracer with the OpenTelemetry API.
	t := otel.Tracer("")

	// Start the OpenTelemetry Span, optionally providing a set of options,
	// that are specific to Datadog's APM product.
	ctx, parent := t.Start(ddotel.ContextWithStartOptions(context.Background(), ddtracer.Measured()), "span_name")
	defer parent.End()

	// Create a child of the parent span, computing the time needed to read a file.
	ctx, child := t.Start(ctx, "read.file")
	child.SetAttributes(attribute.String(ext.ResourceName, "test.json"))

	// Perform an operation.
	_, err := os.ReadFile("~/test.json")

	// We may finish the child span using the returned error. If it's
	// nil, it will be disregarded.
	ddotel.EndOptions(child, ddtracer.WithError(err))
	child.End()
	if err != nil {
		log.Fatal(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithStartOptions

func ContextWithStartOptions(ctx context.Context, opts ...tracer.StartSpanOption) context.Context

ContextWithStartOptions returns a copy of the given context which includes the span s. This can be used to pass a context with Datadog start options to the Start function on the OTel tracer to propagate the options.

func EndOptions

func EndOptions(sp oteltrace.Span, options ...tracer.FinishOption)

EndOptions sets tracer.FinishOption on a given span to be executed when span is finished.

Types

type TracerProvider

type TracerProvider struct {
	noop.TracerProvider // https://pkg.go.dev/go.opentelemetry.io/otel/trace#hdr-API_Implementations

	sync.Once
	// contains filtered or unexported fields
}

TracerProvider provides implementation of OpenTelemetry TracerProvider interface. TracerProvider provides Tracers that are used by instrumentation code to trace computational workflows. WithInstrumentationVersion and WithSchemaURL TracerOptions are not supported.

func NewTracerProvider

func NewTracerProvider(opts ...tracer.StartOption) *TracerProvider

NewTracerProvider returns an instance of an OpenTelemetry TracerProvider, and initializes the Datadog Tracer with the provided start options. This TracerProvider only supports a singleton tracer, and repeated calls to the Tracer() method will return the same instance each time.

func (*TracerProvider) ForceFlush

func (p *TracerProvider) ForceFlush(timeout time.Duration, callback func(ok bool))

ForceFlush flushes any buffered traces. Flush is in effect only if a tracer is started.

func (*TracerProvider) Shutdown

func (p *TracerProvider) Shutdown() error

Shutdown stops the started tracer. Subsequent calls are valid but become no-op.

func (*TracerProvider) Tracer

Tracer returns the singleton tracer created when NewTracerProvider was called, ignoring the provided name and any provided options to this method. If the TracerProvider has already been shut down, this will return a no-op tracer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL