uptraceloggerzap

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

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

Go to latest
Published: Jul 17, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

README

Zap OpenTelemetry instrumentation example

PkgGoDev

Install:

go get -u go.uber.org/zap
go get github.com/aphagon/go-uptrace-logger-zap

Then wrap zap logger:

import (
	"context"
	"log"
	
	logger "github.com/aphagon/go-uptrace-logger-zap"
	"go.opentelemetry.io/otel"
	"github.com/uptrace/uptrace-go/uptrace"
	"go.uber.org/zap"
)
func main() {
	ctx_bg := context.Background()
	
	// Uptrace.
	uptrace.ConfigureOpentelemetry(&uptrace.Config{
		DSN: "https://<token>@api.uptrace.dev/<project_id>",
		ServiceName:    "development",
		ServiceVersion: "1.0.0",
		PrettyPrint: true,
	})
	defer uptrace.Shutdown(ctx_bg)
	defer uptrace.ReportPanic(ctx_bg)
	log.Println("UpTrace connected")

	tracer := otel.Tracer("app_or_package_name")
	ctx, span := tracer.Start(ctx_bg, "main")
	
	aLogger := logger.New(&logger.Config{
		Development: true,
		Level: "debug",
		TimeZone: "Asia/Bangkok",
	})
	// You must use Ctx to propagate the active span.
	aLogger.Ctx(ctx).Info("Hello World!!")
	
	span.End()

	fmt.Printf("trace: %s\n", uptrace.TraceURL(span))
}

And use zap.Ctx to propagate the active span:

aLogger.Ctx(ctx).Error("hello from zap",
	zap.Error(errors.New("hello world")),
	zap.String("foo", "bar"))

Example

To run this example:

UPTRACE_DSN="https://<token>@api.uptrace.dev/<project_id>" go run main.go

Note that this example requires patching the zap package:

go mod edit -replace go.uber.org/zap=github.com/uptrace/zap@master

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cfg *Config) *repository

New Logger constructor

Types

type Config

type Config struct {
	Development bool
	Level       string
	TimeZone    string
}

Config is the uptrace

type Service

type Service interface {
	Ctx(ctx context.Context) zap.LoggerWithCtx
}

Service methods interface

Jump to

Keyboard shortcuts

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