Skip to content

OTelemetry is a wrapper around the OpenTelemetry library (over gRPC)

License

Notifications You must be signed in to change notification settings

rorua/otelemetry

Repository files navigation

OTelemetry

OTelemetry is a wrapper around (over gRPC) the OpenTelemetry library that provides a simple interface to instrument your code with telemetry.

ToDo

  • Jetstream utils
  • RabbitMQ utils
  • TLS support
  • Add tests
  • Modify examples
  • Add documentation

Install

go get -u github.com/rorua/otelemetry

Usage

Here is a simple example of how to use OTelemetry in your Go application:

import "github.com/rorua/otelemetry"

func main() {
	// Configuration for OTelemetry
	cfg := otelemetry.Config{
		Service: otelemetry.ServiceConfig{
			Name: "example-service",
		},
		Collector: otelemetry.CollectorConfig{
			Host: "localhost",
			Port: "4317",
		},
                TracerOptions: otelemetry.TracerOptions{
                    ClientOption: []otlptracegrpc.Option{
                        otlptracegrpc.WithCompressor("gzip"),
                    },
                },
		WithTraces:  true,
		WithMetrics: true,
		WithLogs:    true,
	}

	// Initialize OTelemetry
	tel, err := otelemetry.New(cfg)
	if err != nil {
		log.Fatalf("failed to initialize telemetry: %v", err)
	}
	defer tel.Shutdown(context.Background())
	
	// your code 
}	

Example usage of tracer and span:

// Example usage of tracer and span
ctx, span := tel.Trace().StartSpan(context.Background(), "example-span")
defer span.End()

span.AddEvent("example event", otelemetry.Attribute("key", "value"))

Get span from context:

span := tel.Trace().SpanFromContext(ctx)

span.AddEvent("example of continuing span get from context", otelemetry.Attribute("key", "value"))

Example usage meter:

// Example usage meter
counter, err := tel.Metric().Float64Counter("example_counter")
if err != nil {
    panic(err)
}

counter.Add(ctx, 1)

Example usage of logger:

// Example usage of logger
tel.Log().Info(ctx, "log message", otelemetry.LogAttribute("key", "value"))

Example of getting a context with tracing data from Nats message:

import (
    otelemetryutils "github.com/rorua/otelemetry/utils"
)

func (h *handler) SignedIn(msg jetstream.Msg) {
    
    ctx := otelemetryutils.GetNatsTraceContext(context.Background(), *msg)
    
    ctx, span := tel.Trace().StartSpan(ctx, "NatsHandler: user.SignedIn")
    defer span.End()
    
    // code ...
}

Contributing

Pull requests are welcome.

About

OTelemetry is a wrapper around the OpenTelemetry library (over gRPC)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages