otel

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: Apache-2.0 Imports: 2 Imported by: 1

README

OpenTelemetry-Logs-Go

Go Reference codecov

OpenTelemetry-Logs-Go is the Go implementation of OpenTelemetry Logs. It provides API to directly send logging data to observability platforms. It is an extension of official open-telemetry/opentelemetry-go to support Logs.

Project Life Cycle

This project was created due log module freeze in official opentelemetry-go repository:

The Logs signal development is halted for this project while we stablize the Metrics SDK. 
No Logs Pull Requests are currently being accepted.

This project will be deprecated once official opentelemetry-go repository Logs module will have status "Stable".

Project packages

Packages Description
autoconfigure Autoconfiguration SDK. Allow to configure log exporters with env variables
sdk Opentelemetry Logs SDK
exporters/otlp OTLP format exporter
exporters/stdout Console exporter

Quick start

This is an implementation of Logs Bridge API and not intended to use by developers directly. It is provided for logging library authors to build log appenders, which use this API to bridge between existing logging libraries and the OpenTelemetry log data model.

Example bellow will show how logging library could be instrumented with current API:

package myInstrumentedLogger

import (
	otel "github.com/kudarap/opentelemetry-logs-go"
	"github.com/kudarap/opentelemetry-logs-go/logs"
	semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
)

const (
	instrumentationName    = "otel/zap"
	instrumentationVersion = "0.0.1"
)

var (
	logger = otel.GetLoggerProvider().Logger(
		instrumentationName,
		logs.WithInstrumentationVersion(instrumentationVersion),
		logs.WithSchemaURL(semconv.SchemaURL),
	)
)

func (c otlpCore) Write(ent zapcore.Entry, fields []zapcore.Field) error {

	lrc := logs.LogRecordConfig{
		Body: &ent.Message,
		...
	}
	logRecord := logs.NewLogRecord(lrc)
	logger.Emit(logRecord)
}

and application initialization code:

package main

import (
	"os"
	"context"
	"github.com/kudarap/opentelemetry-logs-go"
	"github.com/kudarap/opentelemetry-logs-go/exporters/otlp/otlplogs"
	"github.com/kudarap/opentelemetry-logs-go/exporters/otlp/otlplogs/otlplogshttp"
	"go.opentelemetry.io/otel/sdk/resource"
	semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
	sdk "github.com/kudarap/opentelemetry-logs-go/sdk/logs"
)

func newResource() *resource.Resource {
	host, _ := os.Hostname()
	return resource.NewWithAttributes(
		semconv.SchemaURL,
		semconv.ServiceName("otlplogs-example"),
		semconv.ServiceVersion("0.0.1"),
		semconv.HostName(host),
	)
}

func main() {
	ctx := context.Background()

	exporter, _ := otlplogs.NewExporter(ctx, otlplogs.WithClient(otlplogshttp.NewClient()))
	loggerProvider := sdk.NewLoggerProvider(
		sdk.WithBatcher(exporter),
		sdk.WithResource(newResource()),
	)
	otel.SetLoggerProvider(loggerProvider)

	myInstrumentedLogger.Info("Hello OpenTelemetry")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLoggerProvider

func GetLoggerProvider() logs.LoggerProvider

GetLoggerProvider returns the registered global logger provider. If none is registered then an instance of NoopLoggerProvider is returned.

loggerProvider := otel.GetLoggerProvider()

func SetLoggerProvider

func SetLoggerProvider(lp logs.LoggerProvider)

SetLoggerProvider registers `lp` as the global logger provider.

Types

This section is empty.

Directories

Path Synopsis
autoconfigure
exporters
internal
Package logs provides an implementation of the logging part of the OpenTelemetry API.
Package logs provides an implementation of the logging part of the OpenTelemetry API.
sdk

Jump to

Keyboard shortcuts

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