observability

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package observability 提供可观测性支持

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CircuitBreakerInterceptor

func CircuitBreakerInterceptor(cb *CircuitBreaker) grpc.UnaryClientInterceptor

CircuitBreakerInterceptor 返回熔断器拦截器。

func MetricsUnaryInterceptor

func MetricsUnaryInterceptor(collector config.MetricsCollector) grpc.UnaryClientInterceptor

MetricsUnaryInterceptor 返回 Metrics 收集拦截器。

func PerMethodCircuitBreakerInterceptor

func PerMethodCircuitBreakerInterceptor(pm *PerMethodCircuitBreaker) grpc.UnaryClientInterceptor

PerMethodCircuitBreakerInterceptor 按方法熔断的拦截器。

func TracingUnaryInterceptor

func TracingUnaryInterceptor(hook config.TracingHook) grpc.UnaryClientInterceptor

TracingUnaryInterceptor 返回 Tracing 拦截器。

Types

type CircuitBreaker

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

CircuitBreaker 熔断器。

func NewCircuitBreaker

func NewCircuitBreaker(config *CircuitBreakerConfig) *CircuitBreaker

NewCircuitBreaker 创建熔断器。

func (*CircuitBreaker) Allow

func (cb *CircuitBreaker) Allow() bool

Allow 检查是否允许请求。

func (*CircuitBreaker) RecordFailure

func (cb *CircuitBreaker) RecordFailure()

RecordFailure 记录失败。

func (*CircuitBreaker) RecordSuccess

func (cb *CircuitBreaker) RecordSuccess()

RecordSuccess 记录成功。

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() CircuitState

State 获取当前状态。

type CircuitBreakerConfig

type CircuitBreakerConfig struct {
	FailureThreshold int
	OpenDuration     time.Duration
	HalfOpenRequests int
	FailureCodes     []codes.Code
	OnStateChange    func(from, to CircuitState)
	SuccessThreshold int
}

CircuitBreakerConfig 熔断器配置。

func DefaultCircuitBreakerConfig

func DefaultCircuitBreakerConfig() *CircuitBreakerConfig

DefaultCircuitBreakerConfig 默认熔断器配置。

type CircuitState

type CircuitState int
const (
	CircuitClosed CircuitState = iota
	CircuitOpen
	CircuitHalfOpen
)

func (CircuitState) String

func (s CircuitState) String() string

type ContextAwareOTelHook

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

ContextAwareOTelHook 支持 context 传递的 OTel 钩子。

func NewContextAwareOTelHook

func NewContextAwareOTelHook(tracer OTelTracer) *ContextAwareOTelHook

NewContextAwareOTelHook 创建支持 context 的 OTel 钩子。

func (*ContextAwareOTelHook) RecordError

func (h *ContextAwareOTelHook) RecordError(ctx context.Context, err error)

RecordError 从 context 获取 span 并记录错误。

func (*ContextAwareOTelHook) SetAttributes

func (h *ContextAwareOTelHook) SetAttributes(ctx context.Context, attrs map[string]string)

SetAttributes 从 context 获取 span 并设置属性。

func (*ContextAwareOTelHook) StartSpan

func (h *ContextAwareOTelHook) StartSpan(ctx context.Context, name string) (context.Context, func())

StartSpan 开始 span 并存入 context。

type NoopMetricsCollector

type NoopMetricsCollector struct{}

NoopMetricsCollector 空操作指标收集器。

func (NoopMetricsCollector) RecordRequest

func (NoopMetricsCollector) RecordRequest(method string, code string, duration time.Duration)

type NoopTracingHook

type NoopTracingHook struct{}

NoopTracingHook 空操作追踪钩子。

func (NoopTracingHook) RecordError

func (NoopTracingHook) RecordError(ctx context.Context, err error)

func (NoopTracingHook) SetAttributes

func (NoopTracingHook) SetAttributes(ctx context.Context, attrs map[string]string)

func (NoopTracingHook) StartSpan

func (NoopTracingHook) StartSpan(ctx context.Context, name string) (context.Context, func())

type OTelTracer

type OTelTracer interface {
	Start(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span)
}

OTelTracer OpenTelemetry Tracer 接口。

type OTelTracingHook

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

OTelTracingHook OpenTelemetry Tracing 钩子。

func NewOTelTracingHook

func NewOTelTracingHook(tracer OTelTracer) *OTelTracingHook

NewOTelTracingHook 创建 OTel Tracing 钩子。

func (*OTelTracingHook) RecordError

func (h *OTelTracingHook) RecordError(ctx context.Context, err error)

RecordError 实现 TracingHook 接口。

func (*OTelTracingHook) SetAttributes

func (h *OTelTracingHook) SetAttributes(ctx context.Context, attrs map[string]string)

SetAttributes 实现 TracingHook 接口。

func (*OTelTracingHook) StartSpan

func (h *OTelTracingHook) StartSpan(ctx context.Context, name string) (context.Context, func())

StartSpan 实现 TracingHook 接口。

type PerMethodCircuitBreaker

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

PerMethodCircuitBreaker 按方法的熔断器管理器。

func NewPerMethodCircuitBreaker

func NewPerMethodCircuitBreaker(config *CircuitBreakerConfig) *PerMethodCircuitBreaker

NewPerMethodCircuitBreaker 创建按方法的熔断器管理器。

func (*PerMethodCircuitBreaker) GetBreaker

func (p *PerMethodCircuitBreaker) GetBreaker(method string) *CircuitBreaker

GetBreaker 获取方法对应的熔断器。

type PrometheusMetrics

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

PrometheusMetrics Prometheus 指标收集器实现

func NewPrometheusMetrics

func NewPrometheusMetrics(opts ...PrometheusMetricsOption) *PrometheusMetrics

NewPrometheusMetrics 创建 Prometheus 指标收集器

func (*PrometheusMetrics) Collectors

func (m *PrometheusMetrics) Collectors() []prometheus.Collector

Collectors 返回所有 Collector(用于自定义注册)

func (*PrometheusMetrics) DecInFlight

func (m *PrometheusMetrics) DecInFlight(method string)

DecInFlight 减少正在处理的请求计数

func (*PrometheusMetrics) IncInFlight

func (m *PrometheusMetrics) IncInFlight(method string)

IncInFlight 增加正在处理的请求计数

func (*PrometheusMetrics) MustRegister

func (m *PrometheusMetrics) MustRegister(registerer prometheus.Registerer)

MustRegister 注册到 Prometheus(panic on error)

func (*PrometheusMetrics) RecordRequest

func (m *PrometheusMetrics) RecordRequest(method string, code string, duration time.Duration)

RecordRequest 实现 MetricsCollector 接口

func (*PrometheusMetrics) Register

func (m *PrometheusMetrics) Register(registerer prometheus.Registerer) error

Register 注册到 Prometheus

type PrometheusMetricsOption

type PrometheusMetricsOption func(*prometheusMetricsConfig)

PrometheusMetricsOption Prometheus 指标配置选项

func WithPrometheusBuckets

func WithPrometheusBuckets(buckets []float64) PrometheusMetricsOption

WithPrometheusBuckets 设置直方图桶

func WithPrometheusConstLabels

func WithPrometheusConstLabels(labels prometheus.Labels) PrometheusMetricsOption

WithPrometheusConstLabels 设置常量标签

func WithPrometheusNamespace

func WithPrometheusNamespace(ns string) PrometheusMetricsOption

WithPrometheusNamespace 设置命名空间

func WithPrometheusRegisterer

func WithPrometheusRegisterer(r prometheus.Registerer) PrometheusMetricsOption

WithPrometheusRegisterer 设置注册器

func WithPrometheusSubsystem

func WithPrometheusSubsystem(ss string) PrometheusMetricsOption

WithPrometheusSubsystem 设置子系统

type Span

type Span interface {
	End()
	SetAttributes(attrs map[string]string)
	RecordError(err error)
	SetStatus(code SpanStatusCode, description string)
}

Span 表示一个追踪 span。

type SpanConfig

type SpanConfig struct {
	Kind SpanKind
}

SpanConfig span 配置。

type SpanKind

type SpanKind int

SpanKind span 类型。

const (
	SpanKindClient SpanKind = iota
	SpanKindServer
	SpanKindProducer
	SpanKindConsumer
	SpanKindInternal
)

type SpanOption

type SpanOption func(*SpanConfig)

SpanOption span 选项。

func WithSpanKind

func WithSpanKind(kind SpanKind) SpanOption

WithSpanKind 设置 span 类型。

type SpanStatusCode

type SpanStatusCode int

SpanStatusCode span 状态码。

const (
	SpanStatusUnset SpanStatusCode = iota
	SpanStatusOK
	SpanStatusError
)

Jump to

Keyboard shortcuts

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