Categorygithub.com/velmie/x/svc/otelx
modulepackage
0.1.0
Repository: https://github.com/velmie/x.git
Documentation: pkg.go.dev

# README

otelx

This package simplifies the process of configuring and initializing OpenTelemetry tracing.

Environment variables

It includes a function for reading configuration values from environment variables. Below is a table describing the available environment variables:

VariableDescriptionDefault Value
TRACING_DISABLEDDisables tracing by setting noop exporterfalse
TRACING_PROPAGATION_BAGGAGEEnables or disables baggage propagationtrue
TRACING_PROPAGATION_TRACE_CONTEXTEnables or disables trace context propagationtrue
TRACING_PROPAGATION_B3_MULTIPLE_HEADEREnables or disables B3 multiple header propagationfalse
TRACING_PROPAGATION_B3_SINGLE_HEADEREnables or disables B3 single header propagationtrue
TRACING_SAMPLING_RATIOSets the sampling ratio for traces1
TRACING_RESOURCE_ATTRIBUTESSets resource attributesnone
TRACING_RESOURCE_DETECTORSList of resource detectors to usenone
TRACING_RESOURCE_SERVICE_NAMESets the service name for the resourcenone
TRACING_RESOURCE_DEPLOYMENT_ENVIRONMENTSets the deployment environment for the resourcenone
TRACING_SECURITY_AUTHORIZATION_HEADERSets the authorization header for securitynone
TRACING_SECURITY_INSECURESets the security mode (insecure or not)true
TRACING_COMMUNICATION_ENDPOINTSets the endpoint for communicationnone
TRACING_COMMUNICATION_EXPORT_METHODSets the export method for communication (grpc, http, stdout)grpc

TRACING_RESOURCE_ATTRIBUTES can be extended with other environment variables:

TRACING_RESOURCE_ATTRIBUTES=attr.key=val,from.another.var=$ANOTHER_VAR

Usage example

    cfg, err := otelx.ConfigFromEnv()

// optionally rewrite parameters
cfg.Resource.ServiceName = "My Service"
cfg.Communication.ExportMethod = otelx.ExportMethodGRPC

// this will create and set tracer provider, exporter and propagators based on the given configuration
// every component could be configured or overridden by specifying options
telemetry, err := otelx.Setup(
context.Background(),
cfg,
otelx.WithResourceAttributes(attribute.String("my.custom.param", "test")),
)

if err != nil {
fmt.Println("failed to setup OpenTelemetry: ", err)
return
}
// ...

You can use custom detectors. For this, they need to be registered in advance using the otelx.RegisterDetector function.

There are three ways to add detectors:

  • Through the TRACING_RESOURCE_DETECTORS environment variable, where the detector names should be listed separated by commas.
  • You can add the detector name after loading the configuration.
  • You can directly set a detector instance using the WithResourceDetectors option.
package example

import (
	"context"
	"github.com/velmie/x/svc/otelx"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/sdk/resource"
)

type MyCustomDetector struct{}

func (m MyCustomDetector) Detect(ctx context.Context) (*resource.Resource, error) {
	return resource.NewWithAttributes(
		semconv.SchemaURL,
		attribute.String("my.custom.attribute1", "hello"),
		attribute.Int("my.custom.attribute2", 42),
	), nil
}

// ... 

func main() {
	cfg, err := otelx.ConfigFromEnv()

	otelx.RegisterDetector("my.custom_detector", &MyCustomDetector{})

	// it's possible to specify directly which detectors to use
	cfg.Resource.Detectors = append(cfg.Resource.Detectors, "my.custom_detector")

	otelx.Setup(context.Background(), cfg) // alternatively you may specify it directly using otelx.WithResourceDetectors(&MyCustomDetector{})
}

# Packages

No description provided by the author

# Functions

ConfigFromEnv creates a new Config structure by reading environment variables.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Setup initializes OpenTelemetry with specified configuration and optional overrides.
TPWithResourceAttributes adds an attribute.KeyValue.
TPWithResourceDetectors adds a resource.Detector.
TPWithTracerProviderOptions adds a tracerProviderOption.
WithExporter is an option to provide a custom SpanExporter for OpenTelemetry setup.
WithPropagator is an option to provide a custom TextMapPropagator for OpenTelemetry setup.
WithResourceAttributes is an option to provide additional resource attributes for OpenTelemetry setup.
No description provided by the author
No description provided by the author

# Constants

ExportMethodGRPC indicates that the tracer should use a gRPC client for exporting traces.
ExportMethodHTTP indicates that the tracer should use an HTTP client for exporting traces.
ExportMethodStdout indicates that the tracer should output data to the standard output (stdout).

# Variables

No description provided by the author

# Structs

CommunicationConfig holds settings related to the communication method and protocol for tracing data.
Config aggregates all the configuration sub-structures.
No description provided by the author
PropagationConfig contains settings related to trace context propagation.
ResourceConfig contains settings related to the service resource information used by the tracer.
SamplingConfig deals with settings related to trace sampling.
SecurityConfig groups settings related to security and authentication for the tracer.
SetupResult contains the components resulting from setting up OpenTelemetry.