Giter Club home page Giter Club logo

opentelemetry-logs-go's Introduction

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".

Compatibility

Minimal supported go version 1.21

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

Getting Started

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/agoda-com/opentelemetry-logs-go"
	"github.com/agoda-com/opentelemetry-logs-go/logs"
	semconv "go.opentelemetry.io/otel/semconv/v1.24.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/agoda-com/opentelemetry-logs-go"
	"github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs"
	"github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs/otlplogshttp"
	"go.opentelemetry.io/otel/sdk/resource"
	semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
	sdk "github.com/agoda-com/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")
}

References

Logger Bridge API implementations for zap, slog, zerolog and other loggers can be found in https://github.com/agoda-com/opentelemetry-go

opentelemetry-logs-go's People

Contributors

chameleon82 avatar def avatar mralves avatar simonswine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

opentelemetry-logs-go's Issues

autoconfigure emit error even when exporter is initilaized successfully

Hi,
Thanks for sharing and maintaining this repo.

When I use the autoconfigure package, I get these errors in my console:

2023/10/10 13:51:17 internal_logging.go:58: "msg"="Can't instantiate otlp exporter" "error"=null

I checked the code, and it seems that this line (along with the one for the logging exporter here) are emitting errors even when err is nil. my sdk setup is able to export log records successfully even with this console error.

I hope I am not missing something obvious, but should these prints be wrapped in an if statement? and should the code skip executing cfg.processors = append(cfg.processors, sdk.NewBatchLogRecordProcessor(otlpExporter)) in case of an error in otlplogs.NewExporter(ctx)?

Resource attributes separated from Scope attributes

Follow up for #21

Bug report

Describe the bug

During log transformation Logger/Exporter Resource Attributes should be as resource attributes and log attributes from instrumentation should be exported as scope attributes

Currently both exporter resource and logger instrumentation resources merged as scope resources and exported as Resource Logs, however Exporter don't have information about LoggerProvider resources to separate ResourceLogs from ScopeResources.

otel.SetLogger or otel.SetLoggerProvider?

On the code example in README.md

Question

The code example in README.md has
otel.SetLoggerProvider(loggerProvider)
but the function is
[func SetLogger(logger logr.Logger)](https://pkg.go.dev/go.opentelemetry.io/otel#SetLogger)

Suggestion (Optional)

Consider changing the example.

How do you log panics and stdout/stderr?

Thank you for putting this package together. I think it's a really valuable addition to the ecosystem's support of OpenTelemetry. One gap, however, is that there is no way to easily redirect stdout/stderr to the Otel collector as well. One vital part of maintaining a production application is seeing any unintended errors or panics that occur.

Third party libraries also often use fmt.Println, etc which prints to stderr. This is resulting in my application sending nicely formatted logs to the collector, but then occasionally key details on an unexpected error get lost. In the case of a panic, it is easy enough to recover(); however, not having third party logs get sent to my logging platform has the potentialy to create confusion. Any ideas on how this will likely be handled by the Go community and this library?

We are currently doing something like this:

// logging package
func RedirectStdOut(writer io.Writer) func() {
	old := os.Stdout

	r, w, err := os.Pipe()
	if err != nil {
		panic(err)
	}

	os.Stdout = w

	go func() {
		if _, err := io.Copy(writer, r); err != nil {
			fmt.Fprintf(os.Stderr, "error: %v\n", err)
			panic(err)
		}
	}()

	return func() {
		w.Close()
		os.Stdout = old
	}
}

func RedirectStdErr(writer io.Writer) func() {
	old := os.Stderr

	r, w, err := os.Pipe()
	if err != nil {
		panic(err)
	}

	os.Stderr = w

	go func() {
		if _, err := io.Copy(writer, r); err != nil {
			fmt.Fprintf(os.Stderr, "error: %v\n", err)
			panic(err)
		}
	}()

	return func() {
		w.Close()
		os.Stderr = old
	}
}

// When instantiating the logger
func redirectStdLog() func() {
	// redirect std out and std err to the new slog logger
	sw := logging.NewSlogWriter(slog.Default())

	restoreErr := logging.RedirectStdErr(sw)
	restoreOut := logging.RedirectStdOut(sw)

	return func() {
		sw.Close()
		restoreErr()
		restoreOut()

		if otelShutdown != nil {
			err := otelShutdown(context.Background())
			if err != nil {
				panic(err)
			}
		}
	}
}

We have also thought about wrapping the execution of our application in another Go program that reads all of the stdout/stdin from the running application and pipes it to OpenTelemetry. Any thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.