Documentation
¶
Overview ¶
Package elasticapm provides a simple and elegant wrapping of Elastic APM v2 Offers simple initialization, configuration management, and integration with zap logging Supports distributed tracing across HTTP and gRPC services
elasticapm 提供简洁优雅的 Elastic APM v2 封装 提供便捷的初始化、配置管理和 zap 日志集成 支持 HTTP 和 gRPC 服务的分布式追踪
Index ¶
- func CheckApmAgentVersion(agentVersion string) bool
- func Close()
- func ContextWithGrpcOutgoingTrace(ctx context.Context, apmTraceContext apm.TraceContext) context.Context
- func ContextWithTraceGrpcOutgoing(ctx context.Context, apmTransaction *apm.Transaction) context.Context
- func GetApmAgentVersion() string
- func Initialize(cfg *Config) error
- func InitializeWithOptions(cfg *Config, evo *EnvOption, setEnvs ...func()) error
- func SetLog(LOG apm.Logger)
- func StartApmTraceGrpcOutgoingCtx(ctx context.Context, name, apmTxnType string) (*apm.Transaction, context.Context)
- type Config
- type EnvOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckApmAgentVersion ¶
CheckApmAgentVersion verifies that APM dependencies use v2 Returns false if version mismatch is detected This check prevents mixing v1 and v2 dependencies which maintain separate tracings
Important context from v1.15.0 to v2.0.0 upgrade: - v1: go.elastic.co/apm v1.15.0 - v2: go.elastic.co/apm/v2 v2.0.0 The v1 and v2 packages maintain independent defaultTracing instances
CheckApmAgentVersion 验证 APM 依赖是否使用 v2 如果检测到版本不匹配则返回 false 此检查防止混用 v1 和 v2 依赖,它们维护独立的追踪
从 v1.15.0 升级到 v2.0.0 的重要背景: - v1: go.elastic.co/apm v1.15.0 - v2: go.elastic.co/apm/v2 v2.0.0 v1 和 v2 包维护独立的 defaultTracing 实例
func Close ¶
func Close()
Close flushes pending data and closes the APM tracing Should be called before application shutdown to ensure data is sent Logs tracing statistics when monitoring is needed
Close 刷新待处理数据并关闭 APM 追踪 应在应用关闭前调用以确保数据发送完成 在需要监控时记录追踪统计信息
func ContextWithGrpcOutgoingTrace ¶
func ContextWithGrpcOutgoingTrace(ctx context.Context, apmTraceContext apm.TraceContext) context.Context
ContextWithGrpcOutgoingTrace injects W3C trace headers into gRPC outgoing metadata Adds traceparent and tracestate headers enabling distributed tracing Headers are set in lowercase ensuring correct gRPC metadata handling
ContextWithGrpcOutgoingTrace 将 W3C 追踪头注入 gRPC 出站元数据 添加 traceparent 和 tracestate 头实现分布式追踪 头信息使用小写确保正确处理 gRPC 元数据
func ContextWithTraceGrpcOutgoing ¶
func ContextWithTraceGrpcOutgoing(ctx context.Context, apmTransaction *apm.Transaction) context.Context
ContextWithTraceGrpcOutgoing attaches transaction to context and adds gRPC outgoing trace headers First adds the transaction to context, then injects trace metadata Propagates traces across gRPC service boundaries
ContextWithTraceGrpcOutgoing 将事务附加到上下文并添加 gRPC 出站追踪头 首先将事务添加到上下文,然后注入追踪元数据 跨 gRPC 服务边界传播追踪
func GetApmAgentVersion ¶
func GetApmAgentVersion() string
GetApmAgentVersion returns the current APM agent version string Version matches the project's go.elastic.co/apm/v2 package version
Examples: - If project uses go.elastic.co/apm/v2 v2.6.3, returns "2.6.3" - If project uses go.elastic.co/apm/v2 v2.7.0, returns "2.7.0" - If project uses go.elastic.co/apm v1.15.0, this returns v2 version (not 1.15.0)
Recommendation: New projects should use go.elastic.co/apm/v2
GetApmAgentVersion 返回当前 APM agent 版本字符串 版本与项目的 go.elastic.co/apm/v2 包版本匹配
示例: - 如果项目使用 go.elastic.co/apm/v2 v2.6.3,返回 "2.6.3" - 如果项目使用 go.elastic.co/apm/v2 v2.7.0,返回 "2.7.0" - 如果项目使用 go.elastic.co/apm v1.15.0,这里返回 v2 版本(不是 1.15.0)
建议:新项目应使用 go.elastic.co/apm/v2
func Initialize ¶
Initialize sets up the APM tracing with given config and default options Uses "go.elastic.co/apm/v2" instead of the old "go.elastic.co/apm" Sets up zap logging integration with APM logs
Initialize 使用给定配置和默认选项初始化 APM 追踪 使用 "go.elastic.co/apm/v2" 而非旧版 "go.elastic.co/apm" 设置 zap 日志与 APM 日志的集成
func InitializeWithOptions ¶
InitializeWithOptions sets up APM with custom environment options Allows fine-grained controls on environment variable handling Extra setup functions can be passed when custom configuration is needed
InitializeWithOptions 使用自定义环境选项初始化 APM 允许对环境变量处理进行精细控制 可传递额外的设置函数进行自定义配置
func SetLog ¶
SetLog sets a custom logging implementation with APM tracing Replaces the default logging with the provided implementation
SetLog 设置自定义日志实现与 APM 追踪 用提供的实现替换默认日志
func StartApmTraceGrpcOutgoingCtx ¶
func StartApmTraceGrpcOutgoingCtx(ctx context.Context, name, apmTxnType string) (*apm.Transaction, context.Context)
StartApmTraceGrpcOutgoingCtx starts a new APM transaction and injects trace context into gRPC metadata Returns the transaction and context prepared to make outgoing gRPC calls The context contains W3C trace headers enabling distributed tracing
StartApmTraceGrpcOutgoingCtx 启动新的 APM 事务并将追踪上下文注入 gRPC 元数据 返回事务和准备好的上下文用于出站 gRPC 调用 上下文包含 W3C 追踪头实现分布式追踪
Types ¶
type Config ¶
type Config struct {
Environment string // Environment name (e.g., "production", "staging") // 环境名称(如 "production"、"staging")
ServerUrls []string // Multiple APM servers URLs // 多个 APM 服务地址
ServerUrl string // Single APM URL // 单个 APM 地址
ApiKey string `json:"-"` // API key used in authentication (not logged) // API 密钥用于认证(不记录日志)
SecretToken string `json:"-"` // Secret token used in authentication (not logged) // Secret Token 用于认证(不记录日志)
ServiceName string // Service name used in identification // 服务名称用于标识
ServiceVersion string // Service version // 服务版本
NodeName string // Node name used in multi-instance services // 节点名称用于多实例服务
ServerCertPath string // Path to certificate used in TLS // TLS 证书路径
SkipShortSpans bool // Skip spans when duration is too short // 跳过持续时间过短的 span
}
Config holds configuration used when initializing Elastic APM Contains connection details, authentication credentials, and service metadata Environment variables are set based on these values during initialization
Config 保存 Elastic APM 初始化配置 包含连接详情、认证凭据和服务元数据 初始化时根据这些值设置环境变量
type EnvOption ¶
type EnvOption struct {
Override bool // Override existing env vars when true // 当值是 true 时覆盖现有环境变量
}
EnvOption controls how environment variables are set during initialization When Override is false, existing environment variables are kept unchanged
EnvOption 控制初始化时如何设置环境变量 当 Override 是 false 时,保留现有的环境变量
func NewEnvOption ¶
func NewEnvOption() *EnvOption
NewEnvOption creates a new EnvOption with Override enabled Returns instance that overrides existing environment variables
NewEnvOption 创建启用 Override 的新 EnvOption 返回会覆盖现有环境变量的实例
func (*EnvOption) SetEnv ¶
SetEnv sets an environment variable based on the Override setting When Override is true, sets the variable without conditions When Override is false, sets the variable if it is not yet defined
SetEnv 根据 Override 设置设定环境变量 当 Override 是 true 时,无条件设置变量 当 Override 是 false 时,在变量未定义时才设置
Directories
¶
| Path | Synopsis |
|---|---|
|
Package apmzaplog provides zap logging integration with Elastic APM Implements apm.Logging interface to route APM logs through zap Uses frame skipping to ensure correct source location in logs
|
Package apmzaplog provides zap logging integration with Elastic APM Implements apm.Logging interface to route APM logs through zap Uses frame skipping to ensure correct source location in logs |
|
internal
|
|
|
demos/demo1x
command
|
|
|
demos/demo2x
command
|