stdout

package module
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2020 License: Apache-2.0 Imports: 18 Imported by: 42

Documentation

Overview

Package stdout contains an OpenTelemetry exporter for both tracing and metric telemetry to be written to an output destination as JSON.

Example
package main

import (
	"context"
	"log"

	"go.opentelemetry.io/otel/api/global"
	"go.opentelemetry.io/otel/api/metric"
	"go.opentelemetry.io/otel/api/trace"
	"go.opentelemetry.io/otel/exporters/stdout"
	"go.opentelemetry.io/otel/label"
)

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

var (
	tracer = global.TraceProvider().Tracer(
		instrumentationName,
		trace.WithInstrumentationVersion(instrumentationVersion),
	)

	meter = global.MeterProvider().Meter(
		instrumentationName,
		metric.WithInstrumentationVersion(instrumentationVersion),
	)

	loopCounter = metric.Must(meter).NewInt64Counter("function.loops")
	paramValue  = metric.Must(meter).NewInt64ValueRecorder("function.param")

	nameKey = label.Key("function.name")
)

func add(ctx context.Context, x, y int64) int64 {
	nameKV := nameKey.String("add")

	var span trace.Span
	ctx, span = tracer.Start(ctx, "Addition")
	defer span.End()

	loopCounter.Add(ctx, 1, nameKV)
	paramValue.Record(ctx, x, nameKV)
	paramValue.Record(ctx, y, nameKV)

	return x + y
}

func multiply(ctx context.Context, x, y int64) int64 {
	nameKV := nameKey.String("multiply")

	var span trace.Span
	ctx, span = tracer.Start(ctx, "Multiplication")
	defer span.End()

	loopCounter.Add(ctx, 1, nameKV)
	paramValue.Record(ctx, x, nameKV)
	paramValue.Record(ctx, y, nameKV)

	return x * y
}

func main() {
	exportOpts := []stdout.Option{
		stdout.WithQuantiles([]float64{0.5}),
		stdout.WithPrettyPrint(),
	}
	// Registers both a trace and meter Provider globally.
	pusher, err := stdout.InstallNewPipeline(exportOpts, nil)
	if err != nil {
		log.Fatal("Could not initialize stdout exporter:", err)
	}
	defer pusher.Stop()

	ctx := context.Background()
	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

func InstallNewPipeline

func InstallNewPipeline(exportOpts []Option, pushOpts []push.Option) (*push.Controller, error)

InstallNewPipeline creates a complete export pipelines with defaults and registers it globally. It is the responsibility of the caller to stop the returned push Controller.

Typically this is called as:

pipeline, err := stdout.InstallNewPipeline(stdout.Config{...})
if err != nil {
	...
}
defer pipeline.Stop()
... Done

func NewExportPipeline

func NewExportPipeline(exportOpts []Option, pushOpts []push.Option) (apitrace.Provider, *push.Controller, error)

NewExportPipeline creates a complete export pipeline with the default selectors, processors, and trace registration. It is the responsibility of the caller to stop the returned push Controller.

Types

type Config

type Config struct {
	// Writer is the destination.  If not set, os.Stdout is used.
	Writer io.Writer

	// PrettyPrint will encode the output into readable JSON. Default is
	// false.
	PrettyPrint bool

	// Timestamps specifies if timestamps should be pritted. Default is
	// true.
	Timestamps bool

	// Quantiles are the desired aggregation quantiles for distribution
	// summaries, used when the configured aggregator supports
	// quantiles.
	//
	// Note: this exporter is meant as a demonstration; a real
	// exporter may wish to configure quantiles on a per-metric
	// basis.
	Quantiles []float64

	// LabelEncoder encodes the labels.
	LabelEncoder label.Encoder

	// DisableTraceExport prevents any export of trace telemetry.
	DisableTraceExport bool

	// DisableMetricExport prevents any export of metric telemetry.
	DisableMetricExport bool
}

Config contains options for the STDOUT exporter.

func Configure

func Configure(options ...Option) (Config, error)

Configure creates a validated Config configured with options.

type Exporter

type Exporter struct {
	// contains filtered or unexported fields
}

func NewExporter

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

NewExporter creates an Exporter with the passed options.

func (*Exporter) Export

func (e *Exporter) Export(_ context.Context, checkpointSet metric.CheckpointSet) error

func (*Exporter) ExportKindFor

func (e *Exporter) ExportKindFor(*apimetric.Descriptor, aggregation.Kind) metric.ExportKind

func (*Exporter) ExportSpan

func (e *Exporter) ExportSpan(ctx context.Context, data *trace.SpanData)

ExportSpan writes a SpanData in json format to stdout.

func (*Exporter) ExportSpans

func (e *Exporter) ExportSpans(ctx context.Context, data []*trace.SpanData)

ExportSpans writes SpanData in json format to stdout.

type Option

type Option interface {
	// Apply option value to Config.
	Apply(*Config)
}

Option sets the value of an option for a Config.

func WithLabelEncoder

func WithLabelEncoder(enc label.Encoder) Option

WithLabelEncoder sets the label encoder used in export.

func WithPrettyPrint

func WithPrettyPrint() Option

WithPrettyPrint sets the export stream format to use JSON.

func WithQuantiles

func WithQuantiles(quantiles []float64) Option

WithQuantiles sets the quantile values to export.

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter sets the export stream destination.

func WithoutMetricExport

func WithoutMetricExport() Option

WithoutMetricExport disables all metric exporting.

func WithoutTimestamps

func WithoutTimestamps() Option

WithoutTimestamps sets the export stream to not include timestamps.

func WithoutTraceExport

func WithoutTraceExport() Option

WithoutTraceExport disables all trace exporting.

Directories

Path Synopsis
stdoutlog module
stdoutmetric module
stdouttrace module

Jump to

Keyboard shortcuts

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