otelslog

package module
v0.0.0-...-d4b635c Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

otelslog

log/slog handler for OTel

Quick start

Export env variable OTEL_EXPORTER_OTLP_ENDPOINT=https://localhost:4318 to your OTLP collector

To start with otelslog first you need to configure opentelemetry-logs-go exporters. See full example bellow:

package main

import (
	"context"
	"github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs"
	sdk "github.com/agoda-com/opentelemetry-logs-go/sdk/logs"
	"github.com/agoda-com/otelslog"
	"go.opentelemetry.io/otel/sdk/resource"
	semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
	"log/slog"
	"os"
)

// configure common attributes for all logs
func newResource() *resource.Resource {
	hostName, _ := os.Hostname()
	return resource.NewWithAttributes(
		semconv.SchemaURL,
		semconv.ServiceName("otelslog-example"),
		semconv.ServiceVersion("1.0.0"),
		semconv.HostName(hostName),
	)
}

func main() {

	ctx := context.Background()

	// configure opentelemetry logger provider
	logExporter, _ := otlplogs.NewExporter(ctx)
	loggerProvider := sdk.NewLoggerProvider(
		sdk.WithBatcher(logExporter),
		sdk.WithResource(newResource()),
	)
	// gracefully shutdown logger to flush accumulated signals before program finish
	defer loggerProvider.Shutdown(ctx)

	otelLogger := slog.New(otelslog.NewOtelHandler(loggerProvider, &otelslog.HandlerOptions{}))
	
	//configure default logger
	slog.SetDefault(otelLogger)

	doSomething(ctx)
}

func doSomething(ctx context.Context) {
	slog.InfoContext(ctx, "hello", slog.String("myKey", "myValue"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version() string

Version is the current release version of OpenTelemetry Slog in use.

Types

type HandlerOptions

type HandlerOptions struct {
}

HandlerOptions are options for a OtelHandler. A zero HandlerOptions consists entirely of default values.

type OtelHandler

type OtelHandler struct {
	// contains filtered or unexported fields
}

OtelHandler is a Handler that writes Records to OTLP

func NewOtelHandler

func NewOtelHandler(loggerProvider otel.LoggerProvider, opts *HandlerOptions) *OtelHandler

NewOtelHandler creates a OtelHandler that writes to otlp, using the given options. If opts is nil, the default options are used.

func (OtelHandler) Enabled

func (o OtelHandler) Enabled(ctx context.Context, level slog.Level) bool

func (OtelHandler) Handle

func (o OtelHandler) Handle(ctx context.Context, record slog.Record) error

func (OtelHandler) WithAttrs

func (o OtelHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (OtelHandler) WithGroup

func (o OtelHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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