Documentation ¶
Overview ¶
Package stdouttrace contains an OpenTelemetry exporter for tracing telemetry to be written to an output destination as JSON.
Example ¶
package main import ( "context" "fmt" "log" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.12.0" "go.opentelemetry.io/otel/trace" ) const ( instrumentationName = "github.com/instrumentron" instrumentationVersion = "v0.1.0" ) var ( tracer = otel.GetTracerProvider().Tracer( instrumentationName, trace.WithInstrumentationVersion(instrumentationVersion), trace.WithSchemaURL(semconv.SchemaURL), ) ) func add(ctx context.Context, x, y int64) int64 { var span trace.Span _, span = tracer.Start(ctx, "Addition") defer span.End() return x + y } func multiply(ctx context.Context, x, y int64) int64 { var span trace.Span _, span = tracer.Start(ctx, "Multiplication") defer span.End() return x * y } func Resource() *resource.Resource { return resource.NewWithAttributes( semconv.SchemaURL, semconv.ServiceNameKey.String("stdout-example"), semconv.ServiceVersionKey.String("0.0.1"), ) } func InstallExportPipeline(ctx context.Context) (func(context.Context) error, error) { exporter, err := stdouttrace.New(stdouttrace.WithPrettyPrint()) if err != nil { return nil, fmt.Errorf("creating stdout exporter: %w", err) } tracerProvider := sdktrace.NewTracerProvider( sdktrace.WithBatcher(exporter), sdktrace.WithResource(Resource()), ) otel.SetTracerProvider(tracerProvider) return tracerProvider.Shutdown, nil } func main() { ctx := context.Background() // Registers a tracer Provider globally. shutdown, err := InstallExportPipeline(ctx) if err != nil { log.Fatal(err) } defer func() { if err := shutdown(ctx); err != nil { log.Fatal(err) } }() log.Println("the answer is", add(ctx, multiply(ctx, multiply(ctx, 2, 2), 10), 2)) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Exporter ¶
type Exporter struct {
// contains filtered or unexported fields
}
Exporter is an implementation of trace.SpanSyncer that writes spans to stdout.
func (*Exporter) ExportSpans ¶
ExportSpans writes spans in json format to stdout.
func (*Exporter) MarshalLog ¶ added in v1.4.0
func (e *Exporter) MarshalLog() interface{}
MarshalLog is the marshaling function used by the logging system to represent this exporter.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option sets the value of an option for a Config.
func WithPrettyPrint ¶
func WithPrettyPrint() Option
WithPrettyPrint sets the export stream format to use JSON.
func WithWriter ¶
WithWriter sets the export stream destination.
func WithoutTimestamps ¶
func WithoutTimestamps() Option
WithoutTimestamps sets the export stream to not include timestamps.
Click to show internal directories.
Click to hide internal directories.