trace

package module
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 14 Imported by: 0

README

trace

trace 是 infrago 的模块

包定位

  • 类型:模块
  • 作用:链路追踪模块,负责 Span/Trace 采集与上报。

主要功能

  • 对上提供统一模块接口
  • 对下通过驱动接口接入具体后端
  • 支持按配置切换驱动实现

快速接入

import _ "github.com/infrago/trace"
[trace]
driver = "default"

驱动实现接口列表

以下接口由驱动实现(来自模块 driver.go):

Driver
  • Connect(*Instance) (Connection, error)
Connection
  • Open() error
  • Close() error
  • Write(spans ...Span) error

全局配置项(所有配置键)

配置段:[trace]

  • driver
  • json
  • flag
  • format
  • buffer
  • timeout
  • sample
  • fields
  • setting

说明

  • setting 一般用于向具体驱动透传专用参数
  • 多实例配置请参考模块源码中的 Config/configure 处理逻辑

Documentation

Index

Constants

View Source
const (
	StatusOK    = "ok"
	StatusFail  = "fail"
	StatusError = "error"
)
View Source
const NAME = "TRACE"

Variables

This section is empty.

Functions

func Emit

func Emit(meta *infra.Meta, name string, status string, attrs ...Map)

func ResolveFields

func ResolveFields(raw Any, defaults map[string]string) map[string]string

ResolveFields parses fields config into source->target mapping. Supports:

  • []string / []any: ["trace_id","span_id"]
  • map[string]any: { trace_id = "tid", span_id = "sid" }

func SpanValues

func SpanValues(span Span, instance, flag string) map[string]Any

SpanValues returns all known trace fields (standard + compat aliases).

func Stats

func Stats() Map

func Write

func Write(span Span)

Types

type Config

type Config struct {
	Driver  string
	Json    bool
	Buffer  int
	Timeout time.Duration
	Flag    string
	Format  string
	Sample  float64
	Fields  Any
	Setting Map
}

type Configs

type Configs map[string]Config

type Connection

type Connection interface {
	Open() error
	Close() error
	Write(spans ...Span) error
}

type Driver

type Driver interface {
	Connect(*Instance) (Connection, error)
}

type Handle

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

func Begin

func Begin(meta *infra.Meta, name string, attrs ...Map) *Handle

func (*Handle) End

func (h *Handle) End(results ...Any)

type Instance

type Instance struct {
	Name    string
	Config  Config
	Setting map[string]any
	// contains filtered or unexported fields
}

func (*Instance) Allow

func (inst *Instance) Allow(span Span) bool

func (*Instance) AllowWithFactor

func (inst *Instance) AllowWithFactor(span Span, factor float64) bool

func (*Instance) Format

func (inst *Instance) Format(span Span) string

type Module

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

func (*Module) Begin

func (m *Module) Begin(meta *infra.Meta, name string, attrs Map) infra.TraceSpan

func (*Module) Close

func (m *Module) Close()

func (*Module) Config

func (m *Module) Config(global Map)

func (*Module) Open

func (m *Module) Open()

func (*Module) Register

func (m *Module) Register(name string, value Any)

func (*Module) RegisterConfig

func (m *Module) RegisterConfig(name string, cfg Config)

func (*Module) RegisterConfigs

func (m *Module) RegisterConfigs(configs Configs)

func (*Module) RegisterDriver

func (m *Module) RegisterDriver(name string, driver Driver)

func (*Module) Setup

func (m *Module) Setup()

func (*Module) Start

func (m *Module) Start()

func (*Module) Stats

func (m *Module) Stats() Map

func (*Module) Stop

func (m *Module) Stop()

func (*Module) Trace

func (m *Module) Trace(meta *infra.Meta, name string, status string, attrs Map) error

func (*Module) Write

func (m *Module) Write(span Span)

type Span

type Span struct {
	Time         time.Time `json:"time"`
	TraceId      string    `json:"trace_id,omitempty"`
	SpanId       string    `json:"span_id,omitempty"`
	ParentSpanId string    `json:"parent_span_id,omitempty"`
	Name         string    `json:"name,omitempty"`
	Kind         string    `json:"kind,omitempty"`
	ServiceName  string    `json:"service_name,omitempty"`
	Target       string    `json:"target,omitempty"`
	Status       string    `json:"status,omitempty"`
	Code         int       `json:"code"`
	Result       string    `json:"result,omitempty"`
	Duration     int64     `json:"duration"`
	Start        int64     `json:"start"`
	End          int64     `json:"end"`
	Attributes   Map       `json:"attributes,omitempty"`
	Resource     Map       `json:"resource,omitempty"`
}

Jump to

Keyboard shortcuts

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