telemetry

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: MIT Imports: 1 Imported by: 0

README

Telemetry

OpenTelemetry initialization helpers

Usage

Optional environment variables

export OTEL_RESOURCE_ATTRIBUTES=key=value,rk5=7
export OTEL_SERVICE_NAME=play-service

# enable metrics and tracking
export CONFY_FEATURES_METRICS_ENABLED=true
export CONFY_FEATURES_TRACING_ENABLED=true
# enable metrics target: `prometheus` and tracing target: `stdout`
export CONFY_FEATURES_METRICS_TARGET=prometheus
export CONFY_FEATURES_TRACING_TARGET=stdout
# when using with target: `gcp`
export GOOGLE_CLOUD_PROJECT=xyz
export GOOGLE_APPLICATION_CREDENTIALS=../../../Apps/micro-starter-kit.json
import (
	"go.opentelemetry.io/otel/api/global"
	"go.opentelemetry.io/otel/instrumentation/grpctrace"

    "github.com/xmlking/toolkit/telemetry/metrics"
    "github.com/xmlking/toolkit/telemetry/tracing"
)

func main() {
    cfg := config.GetConfig()
    
    if cfg.Features.Tracing.Enabled {
    closeFn := tracing.InitTracing(ctx, cfg.Features.Tracing)
    defer closeFn()
    }

    if cfg.Features.Metrics.Enabled {
    closeFn := metrics.InitMetrics(ctx, cfg.Features.Metrics)
    defer closeFn()
    }

	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}

	s := grpc.NewServer(
		grpc.UnaryInterceptor(grpctrace.UnaryServerInterceptor(global.Tracer(""))),
		grpc.StreamInterceptor(grpctrace.StreamServerInterceptor(global.Tracer(""))),
	)

	api.RegisterHelloServiceServer(s, &server{})
	if err := s.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}
Examples
Integrated GCP observability
gRPC tracking
Metrics:

Documentation

Index

Constants

View Source
const (
	GCP        = "gcp"
	PROMETHEUS = "prometheus"
	STDOUT     = "stdout"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type MetricsConfig

type MetricsConfig struct {
	Enabled  bool   `yaml:",omitempty" default:"false"`
	Backend  string `yaml:",omitempty" validate:"oneof=gcp prometheus stdout" default:"stdout"`
	Endpoint string `yaml:"endpoint,omitempty"`
	// SamplingFraction >= 1 will always sample. SamplingFraction < 0 are treated as zero.
	SamplingFraction      float64       `yaml:"sampling_fraction,omitempty" default:"1.0"`
	CollectPeriod         time.Duration `yaml:"collect_period,omitempty" default:"10s"`
	HistogramDistribution []float64     `yaml:"histogram_distribution,omitempty" validate:"omitempty,gt=0,dive,min=0,max=10"`
}

MetricsConfig struct

type TracingConfig

type TracingConfig struct {
	Enabled  bool   `yaml:",omitempty" default:"false"`
	Backend  string `yaml:",omitempty" validate:"oneof=gcp stdout" default:"stdout"`
	Endpoint string `yaml:"endpoint,omitempty"`
	// SamplingFraction >= 1 will always sample. SamplingFraction < 0 are treated as zero.
	SamplingFraction float64 `yaml:"sampling_fraction,omitempty" default:"1.0"`
}

TracingConfig struct

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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