Documentation
¶
Overview ¶
Package obs sets up the OpenTelemetry SDK as automatically as possible for your application. It configures the default providers for tracing, metrics and logging. You'll still need to add instrumentation yourself.
Example ¶
package main
import (
"context"
"log"
"log/slog"
"time"
"go.opentelemetry.io/contrib/detectors/aws/ecs"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
"github.com/wolverian/obs"
)
func main() {
shutdown, err := obs.Start(context.Background(),
"test-app",
// Customise your resource by e.g. using detectors for your platform
resource.WithDetectors(ecs.NewResourceDetector()),
)
if err != nil {
panic(err)
}
defer func() {
ctx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second)
defer cancelFunc()
if err := shutdown(ctx); err != nil {
log.Printf("error shutting down observability: %v", err)
}
}()
Run(context.Background())
}
func Run(ctx context.Context) {
slog.Info("Logs to OpenTelemetry")
tracer := otel.Tracer("test-app")
ctx, span := tracer.Start(ctx, "test-span")
defer span.End()
span.SetAttributes(attribute.String("example", "attribute"))
// Do much work here
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Start ¶
func Start(ctx context.Context, name string, opts ...resource.Option) (func(context.Context) error, error)
Start sets up OpenTelemetry tracing, metrics, and logging.
After calling Start, you can use the OpenTelemetry API and instrument your code. The standard library log and slog packages are configured to send logs to OpenTelemetry. You can customize the exporters using environment variables as specified in autoexport.
Returns a shutdown function to close exporters and free resources, and an error if setup fails.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.