otel

package
v0.0.0-...-daa9717 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: MIT Imports: 25 Imported by: 0

README

Otel

使用go.opentelemetry.io追踪数据。可查看例子ex

使用方法

  • 创建结构实现Run接口
type App struct {
 srv *http.Server
}

func NewApp() *App {
 return &App{}
}

func (a *App) Run() error {
 a.srv = &http.Server{
  Addr: ":9999",
  Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   ctx, span := otel.Tracer.Start(r.Context(), "handler")
   defer span.End()

   otel.Info(ctx, r.URL.String())
  }),
 }

 return a.srv.ListenAndServe()
}

func (a *App) Shutdown(ctx context.Context) error {
 return a.srv.Shutdown(ctx)
}
  • 初始化创建otel.Config变量
 cfg := &otel.Config{}
  • 使用otel.Config变量运行`Run``接口实例
 if err := cfg.Run(&App{}); err != nil {
  log.Fatal(err)
 }
  • 需要追踪使用
ctx, span := otel.Start(r.Context(), "handler")
defer span.End()
  • 日志
otel.Info(ctx, r.URL.String())
otel.Error(ctx, r.URL.String())
  • 多条日志附加参数
log := otel.With("node", "123")
log.InfoContext(ctx, " Start")
  • 启动项目使用

  • 如果需要发送到http,例如openobserve使用go.opentelemetry.io环境变量

OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:5080/api/default OTEL_RESOURCE_ATTRIBUTES="service.name=xxx,service.version=0.1.0" OTEL_EXPORTER_OTLP_HEADERS="stream-name=xxx,Authorization=Basic xxx"

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Tracer trace.Tracer
	Start  func(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span)
)

Functions

func Ctx

func Ctx(ctx context.Context, log Logger) context.Context

func Error

func Error(ctx context.Context, msg string, args ...any)

func Info

func Info(ctx context.Context, msg string, args ...any)

func SetLog

func SetLog(l Logger)

func SkipStr

func SkipStr(s string)

func Warn

func Warn(ctx context.Context, msg string, args ...any)

func With

func With(ctx context.Context, args ...any) context.Context

Types

type Config

type Config struct {
	Name          string  `json:"name" yaml:"name" mapstructure:"name"`
	LogSource     bool    `json:"logSource" yaml:"logSource" mapstructure:"logSource"`
	Type          string  `json:"type" yaml:"type" mapstructure:"type"`
	MetricDisable bool    `json:"metricDisable" yaml:"metricDisable" mapstructure:"metricDisable"`
	TraceDisable  bool    `json:"traceDisable" yaml:"traceDisable" mapstructure:"traceDisable"`
	TraceRatio    float64 `json:"traceRatio" yaml:"traceRatio" mapstructure:"traceRatio"`
}

func (*Config) Run

func (cfg *Config) Run(r Run) (err error)

func (*Config) RunFunc

func (cfg *Config) RunFunc(f func(ctx context.Context) error) (err error)

func (*Config) SetupOTelSDK

func (cfg *Config) SetupOTelSDK(ctx context.Context) (shutdown func(context.Context) error, err error)

setupOTelSDK bootstraps the OpenTelemetry pipeline. If it does not return an error, make sure to call shutdown for proper cleanup.

type Logger

type Logger interface {
	Info(ctx context.Context, msg string, args ...any)
	Error(ctx context.Context, msg string, args ...any)
	Warn(ctx context.Context, msg string, args ...any)
	With(args ...any) Logger
	Write([]byte) (int, error)
	Printf(string, ...any)
}

func For

func For(ctx context.Context) Logger

func Log

func Log() Logger

type Run

type Run interface {
	Run(context.Context) error
	Shutdown(context.Context) error
}

Directories

Path Synopsis
ex command

Jump to

Keyboard shortcuts

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