Skip to content

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"funcmain() {
// Configuration for OTelemetrycfg:= 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 OTelemetrytel, err:=otelemetry.New(cfg)
iferr!=nil {
log.Fatalf("failed to initialize telemetry: %v", err)
}
defertel.Shutdown(context.Background())
// your code 
}	

Example usage of tracer and span:

// Example usage of tracer and spanctx, span:=tel.Trace().StartSpan(context.Background(), "example-span")
deferspan.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 metercounter, err:=tel.Metric().Float64Counter("example_counter")
iferr!=nil {
panic(err)
}
counter.Add(ctx, 1)

Example usage of logger:

// Example usage of loggertel.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")
deferspan.End()
// code ...
}

Contributing

Pull requests are welcome.

About

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

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages