observability

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Application scenarios: - Define health check contracts for services and their dependencies. - Support aggregated health status reporting for microservices. - Provide unified health check endpoint for HTTP and gRPC.

适用场景: - 定义服务及其依赖的健康检查契约。 - 支持微服务的聚合健康状态报告。 - 为 HTTP 和 gRPC 提供统一的健康检查端点。

Application scenarios: - Define the framework-wide logging contract used across runtime, middleware, and providers. - Keep structured log fields portable across different logger implementations. - Expose one minimal logger surface for common application and framework logging flows.

适用场景: - 定义 runtime、中间件和 provider 共同使用的框架级日志契约。 - 让结构化日志字段在不同 logger 实现之间保持可移植。 - 为常见应用和框架日志流程提供最小 logger 接口面。

Index

Constants

View Source
const (
	TracerKey         = "framework.tracer"
	TracerProviderKey = "framework.tracer.provider"
)
View Source
const HealthCheckerKey = "framework.health.checker"

HealthCheckerKey is the container key for the health checker capability.

HealthCheckerKey 是健康检查能力的容器键。

View Source
const LogKey = "framework.log"

LogKey is the container key for the logging capability.

LogKey 是日志能力的容器键。

View Source
const MetricsKey = "framework.metrics"

MetricsKey is the container key for the metrics capability.

MetricsKey 是 metrics 能力的容器键。

View Source
const ObservabilityKey = "framework.observability"

Variables

This section is empty.

Functions

This section is empty.

Types

type ComponentChecker

type ComponentChecker func(ctx context.Context) HealthCheckResult

ComponentChecker is a function that checks the health of a component.

ComponentChecker 是检查组件健康的函数。

type DependencyChecker

type DependencyChecker func(ctx context.Context) DependencyHealth

DependencyChecker is a function that checks the health of a dependency.

DependencyChecker 是检查依赖健康的函数。

type DependencyHealth

type DependencyHealth struct {
	// Name is the name of the dependency.
	//
	// Name 是依赖的名称。
	Name string `json:"name"`

	// Type is the type of dependency (e.g., "database", "redis", "grpc").
	//
	// Type 是依赖的类型(如 "database", "redis", "grpc")。
	Type string `json:"type"`

	// Status is the health status of the dependency.
	//
	// Status 是依赖的健康状态。
	Status HealthStatus `json:"status"`

	// Message provides additional details.
	//
	// Message 提供额外详情。
	Message string `json:"message,omitempty"`

	// Latency is the time taken to check the dependency.
	//
	// Latency 是检查依赖所花费的时间。
	Latency time.Duration `json:"latency"`
}

DependencyHealth represents the health status of a dependency.

DependencyHealth 表示依赖的健康状态。

type Field

type Field struct {
	Key   string
	Value any
}

Field describes one structured log field.

Field 描述一个结构化日志字段。

type HealthCheckResult

type HealthCheckResult struct {
	// Name is the name of the component being checked.
	//
	// Name 是被检查组件的名称。
	Name string `json:"name"`

	// Status is the health status of the component.
	//
	// Status 是组件的健康状态。
	Status HealthStatus `json:"status"`

	// Message provides additional details about the health status.
	//
	// Message 提供健康状态的额外详情。
	Message string `json:"message,omitempty"`

	// Error contains the error if the health check failed.
	//
	// Error 包含健康检查失败的错误信息。
	Error string `json:"error,omitempty"`

	// Latency is the time taken to perform the health check.
	//
	// Latency 是执行健康检查所花费的时间。
	Latency time.Duration `json:"latency"`

	// Timestamp is when the health check was performed.
	//
	// Timestamp 是健康检查执行的时间戳。
	Timestamp time.Time `json:"timestamp"`

	// Metadata contains additional component-specific information.
	//
	// Metadata 包含额外的组件特定信息。
	Metadata map[string]any `json:"metadata,omitempty"`
}

HealthCheckResult represents the result of a health check.

HealthCheckResult 表示健康检查的结果。

type HealthChecker

type HealthChecker interface {
	// Check performs a health check and returns the aggregated report.
	//
	// Check 执行健康检查并返回聚合报告。
	Check(ctx context.Context) (*HealthReport, error)

	// CheckComponent performs a health check for a specific component.
	//
	// CheckComponent 对特定组件执行健康检查。
	CheckComponent(ctx context.Context, name string) (*HealthCheckResult, error)

	// AddChecker registers a component health checker.
	//
	// AddChecker 注册组件健康检查器。
	AddChecker(name string, checker ComponentChecker)

	// AddDependency registers a dependency health checker.
	//
	// AddDependency 注册依赖健康检查器。
	AddDependency(name string, dep DependencyChecker)
}

HealthChecker defines the health checking contract. Implementations aggregate health status from multiple components.

HealthChecker 定义健康检查契约。 实现聚合多个组件的健康状态。

type HealthCheckerConfig

type HealthCheckerConfig struct {
	// ServiceName is the name of the service.
	//
	// ServiceName 是服务名称。
	ServiceName string `mapstructure:"service_name"`

	// Version is the service version.
	//
	// Version 是服务版本。
	Version string `mapstructure:"version"`

	// Timeout is the timeout for individual health checks.
	//
	// Timeout 是单个健康检查的超时时间。
	Timeout time.Duration `mapstructure:"timeout"`

	// CheckDependencies indicates whether to check dependencies.
	//
	// CheckDependencies 表示是否检查依赖。
	CheckDependencies bool `mapstructure:"check_dependencies"`
}

HealthCheckerConfig configures the health checker.

HealthCheckerConfig 配置健康检查器。

type HealthReport

type HealthReport struct {
	// Service is the name of the service.
	//
	// Service 是服务名称。
	Service string `json:"service"`

	// Status is the overall health status of the service.
	//
	// Status 是服务的整体健康状态。
	Status HealthStatus `json:"status"`

	// Version is the service version.
	//
	// Version 是服务版本。
	Version string `json:"version"`

	// Timestamp is when the report was generated.
	//
	// Timestamp 是报告生成的时间戳。
	Timestamp time.Time `json:"timestamp"`

	// Checks contains individual health check results.
	//
	// Checks 包含各个健康检查的结果。
	Checks map[string]HealthCheckResult `json:"checks"`

	// Dependencies contains health status of dependent services.
	//
	// Dependencies 包含依赖服务的健康状态。
	Dependencies map[string]DependencyHealth `json:"dependencies,omitempty"`
}

HealthReport represents an aggregated health report for a service.

HealthReport 表示服务的聚合健康报告。

type HealthStatus

type HealthStatus string

HealthStatus represents the health status of a component or service.

HealthStatus 表示组件或服务的健康状态。

const (
	// HealthStatusHealthy indicates the component is healthy.
	//
	// HealthStatusHealthy 表示组件健康。
	HealthStatusHealthy HealthStatus = "healthy"

	// HealthStatusUnhealthy indicates the component is unhealthy.
	//
	// HealthStatusUnhealthy 表示组件不健康。
	HealthStatusUnhealthy HealthStatus = "unhealthy"

	// HealthStatusDegraded indicates the component is degraded but functional.
	//
	// HealthStatusDegraded 表示组件降级但仍可用。
	HealthStatusDegraded HealthStatus = "degraded"
)

type Logger

type Logger interface {
	Debug(msg string, fields ...Field)
	Info(msg string, fields ...Field)
	Warn(msg string, fields ...Field)
	Error(msg string, fields ...Field)

	With(fields ...Field) Logger
}

Logger defines the framework logging contract.

Logger 定义框架日志契约。

type Metrics

type Metrics interface {
	Counter(name string, labels map[string]string, delta float64)
	Gauge(name string, labels map[string]string, value float64)
	Histogram(name string, labels map[string]string, value float64)
	Timing(name string, labels map[string]string, duration time.Duration)
}

type Observability

type Observability interface {
	Metrics() Metrics
	Tracer() Tracer
	Logger() Logger
	ErrorReporter() resiliencecontract.ErrorReporter
}

type ObservabilityConfig

type ObservabilityConfig struct {
	MetricsEnabled        bool
	TracingEnabled        bool
	ErrorReportingEnabled bool
	ServiceName           string
	Environment           string
	Version               string
	SamplingRate          float64
}

type RequestContext

type RequestContext struct {
	TraceID   string
	RequestID string
	Span      Span
	StartTime time.Time
	Labels    map[string]string
}

type Span

type Span interface {
	End(options ...SpanEndOption)
	AddEvent(name string, attributes map[string]interface{})
	SetTag(key string, value interface{})
	SetAttributes(attributes map[string]interface{})
	SetError(err error)
	SetStatus(code SpanStatusCode, description string)
	SpanContext() SpanContext
	IsRecording() bool
	Context() context.Context
}

type SpanConfig

type SpanConfig struct {
	Kind       SpanKind
	Attributes map[string]interface{}
	StartTime  time.Time
	Links      []SpanLink
}

type SpanContext

type SpanContext struct {
	TraceID    string
	SpanID     string
	TraceFlags TraceFlags
	Remote     bool
}

type SpanEndConfig

type SpanEndConfig struct {
	EndTime time.Time
}

type SpanEndOption

type SpanEndOption func(*SpanEndConfig)

type SpanKind

type SpanKind int
const (
	SpanKindUnspecified SpanKind = iota
	SpanKindInternal
	SpanKindServer
	SpanKindClient
	SpanKindProducer
	SpanKindConsumer
)
type SpanLink struct {
	SpanContext SpanContext
	Attributes  map[string]interface{}
}

type SpanOption

type SpanOption func(*SpanConfig)

type SpanStatusCode

type SpanStatusCode int
const (
	SpanStatusCodeUnset SpanStatusCode = iota
	SpanStatusCodeOk
	SpanStatusCodeError
)

type TextMapCarrier

type TextMapCarrier interface {
	Get(key string) string
	Set(key string, value string)
	Keys() []string
}

type TraceFlags

type TraceFlags byte
const (
	TraceFlagsSampled TraceFlags = 0x01
)

type Tracer

type Tracer interface {
	StartSpan(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span)
	SpanFromContext(ctx context.Context) Span
	Inject(ctx context.Context, carrier TextMapCarrier) error
	Extract(ctx context.Context, carrier TextMapCarrier) (context.Context, error)
}

type TracerConfig

type TracerConfig struct {
	SchemaURL  string
	Version    string
	Attributes map[string]interface{}
}

type TracerOption

type TracerOption func(*TracerConfig)

type TracerProvider

type TracerProvider interface {
	Tracer(name string, options ...TracerOption) Tracer
	Shutdown(ctx context.Context) error
	ForceFlush(ctx context.Context) error
}

type TracingConfig

type TracingConfig struct {
	Enabled            bool
	ServiceName        string
	Environment        string
	Version            string
	ExporterType       string
	ExporterEndpoint   string
	SamplingRate       float64
	Propagators        []string
	ResourceAttributes map[string]string
	BatchTimeout       int
	MaxQueueSize       int
	MaxExportBatchSize int
}

Jump to

Keyboard shortcuts

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