[go: up one dir, main page]

stdouttrace

package module
v1.38.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2025 License: Apache-2.0, BSD-3-Clause Imports: 18 Imported by: 1,058

README

STDOUT Trace Exporter

PkgGoDev

Documentation

Overview

Package stdouttrace contains an OpenTelemetry exporter for tracing telemetry to be written to an output destination as JSON.

See go.opentelemetry.io/otel/exporters/stdout/stdouttrace/internal/x for information about the experimental features.

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.37.0"
	"go.opentelemetry.io/otel/trace"
)

const (
	instrumentationName    = "github.com/instrumentron"
	instrumentationVersion = "0.1.0"
)

var tracer = otel.GetTracerProvider().Tracer(
	instrumentationName,
	trace.WithInstrumentationVersion(instrumentationVersion),
	trace.WithSchemaURL(semconv.SchemaURL),
)

func add(ctx context.Context, x, y int64) int64 {
	_, span := tracer.Start(ctx, "Addition")
	defer span.End()

	return x + y
}

func multiply(ctx context.Context, x, y int64) int64 {
	_, span := tracer.Start(ctx, "Multiplication")
	defer span.End()

	return x * y
}

func Resource() *resource.Resource {
	return resource.NewWithAttributes(
		semconv.SchemaURL,
		semconv.ServiceName("stdout-example"),
		semconv.ServiceVersion("0.0.1"),
	)
}

func InstallExportPipeline() (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()
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := shutdown(ctx); err != nil {
			log.Fatal(err)
		}
	}()

	ctx, span := tracer.Start(ctx, "Calculation")
	defer span.End()
	ans := multiply(ctx, 2, 2)
	ans = multiply(ctx, ans, 10)
	ans = add(ctx, ans, 2)
	log.Println("the answer is", ans)
}

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 New

func New(options ...Option) (*Exporter, error)

New creates an Exporter with the passed options.

func (*Exporter) ExportSpans

func (e *Exporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) (err error)

ExportSpans writes spans in json format to stdout.

func (*Exporter) MarshalLog added in v1.4.0

func (e *Exporter) MarshalLog() any

MarshalLog is the marshaling function used by the logging system to represent this Exporter.

func (*Exporter) Shutdown

func (e *Exporter) Shutdown(context.Context) error

Shutdown is called to stop the exporter, it performs no action.

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 prettifies the emitted output.

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter sets the export stream destination.

func WithoutTimestamps

func WithoutTimestamps() Option

WithoutTimestamps sets the export stream to not include timestamps.

Directories

Path Synopsis
Package internal provides internal functionality for the stdouttrace package.
Package internal provides internal functionality for the stdouttrace package.
counter
Package counter provides a simple counter for generating unique IDs.
Package counter provides a simple counter for generating unique IDs.
x
Package x documents experimental features for go.opentelemetry.io/otel/exporters/stdout/stdouttrace.
Package x documents experimental features for go.opentelemetry.io/otel/exporters/stdout/stdouttrace.

Jump to

Keyboard shortcuts

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