stdoutlogs

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2023 License: Apache-2.0 Imports: 13 Imported by: 3

Documentation

Overview

Example
package main

import (
	"context"
	"github.com/agoda-com/opentelemetry-logs-go"
	"github.com/agoda-com/opentelemetry-logs-go/logs"
	sdk "github.com/agoda-com/opentelemetry-logs-go/sdk/logs"
	"go.opentelemetry.io/otel/sdk/resource"
	semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
	"log"
	"time"
)

const (
	instrumentationName    = "github.com/instrumentron"
	instrumentationVersion = "v0.1.0"
)

func newResource() *resource.Resource {
	return resource.NewWithAttributes(
		semconv.SchemaURL,
		semconv.ServiceName("otlplogs-example"),
		semconv.ServiceVersion("0.0.1"),
	)
}

func doSomething() {

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

	body := "My message"
	now := time.Now()
	sn := logs.INFO
	cfg := logs.LogRecordConfig{
		Timestamp:         &now,
		ObservedTimestamp: now,
		Body:              &body,
		SeverityNumber:    &sn,
	}
	logRecord := logs.NewLogRecord(cfg)
	logger.Emit(logRecord)
}

func installExportPipeline(ctx context.Context) (func(context.Context) error, error) {
	exporter, _ := NewExporter()

	loggerProvider := sdk.NewLoggerProvider(
		sdk.WithSyncer(exporter),
		sdk.WithResource(newResource()),
	)
	otel.SetLoggerProvider(loggerProvider)

	return loggerProvider.Shutdown, nil
}

func main() {
	{
		ctx := context.Background()
		// Registers a logger Provider globally.
		shutdown, err := installExportPipeline(ctx)
		if err != nil {
			log.Fatal(err)
		}
		doSomething()

		defer func() {
			if err := shutdown(ctx); err != nil {
				log.Fatal(err)
			}
		}()
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Exporter

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

Exporter is an implementation of logs.LogRecordSyncer that writes spans to stdout.

func NewExporter

func NewExporter(options ...Option) (*Exporter, error)

NewExporter creates an Exporter with the passed options.

func (*Exporter) Export

func (e *Exporter) Export(ctx context.Context, logs []sdk.ReadableLogRecord) error

Export writes logs in json format to stdout.

func (*Exporter) MarshalLog

func (e *Exporter) MarshalLog() interface{}

MarshalLog is the marshaling function used by the logging system to represent this exporter.

func (*Exporter) Shutdown

func (e *Exporter) Shutdown(ctx context.Context) error

Shutdown is called to stop the exporter, it performs no action.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option sets the value of an option for a Config.

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter sets the export stream destination.

Jump to

Keyboard shortcuts

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