observability

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithAttributes

func WithAttributes(attributes map[string]string) trace.SpanStartOption

WithAttributes sets span attributes

func WithResource

func WithResource(resource map[string]string) trace.SpanStartOption

WithResource sets span resource attributes

func WithSpanKind

func WithSpanKind(kind trace.SpanKind) trace.SpanStartOption

WithSpanKind sets the span kind

Types

type Alert

type Alert struct {
	ID          string            `json:"id"`
	Name        string            `json:"name"`
	Description string            `json:"description"`
	Severity    AlertSeverity     `json:"severity"`
	Status      AlertStatus       `json:"status"`
	Condition   string            `json:"condition"`
	Threshold   float64           `json:"threshold"`
	Duration    time.Duration     `json:"duration"`
	Labels      map[string]string `json:"labels"`
	CreatedAt   time.Time         `json:"created_at"`
	UpdatedAt   time.Time         `json:"updated_at"`
	FiredAt     time.Time         `json:"fired_at,omitempty"`
	ResolvedAt  time.Time         `json:"resolved_at,omitempty"`
}

Alert represents an alert

type AlertHandler

type AlertHandler interface {
	HandleAlert(ctx context.Context, alert *Alert) error
	GetName() string
}

AlertHandler handles alert notifications

type AlertSeverity

type AlertSeverity int

AlertSeverity represents the severity of an alert

const (
	AlertSeverityInfo AlertSeverity = iota
	AlertSeverityWarning
	AlertSeverityCritical
	AlertSeverityEmergency
)

func (AlertSeverity) String

func (s AlertSeverity) String() string

type AlertStatus

type AlertStatus int

AlertStatus represents the status of an alert

const (
	AlertStatusInactive AlertStatus = iota
	AlertStatusPending
	AlertStatusFiring
	AlertStatusResolved
)

func (AlertStatus) String

func (s AlertStatus) String() string

type ApplicationMetricsCollector

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

ApplicationMetricsCollector collects application-level metrics

func NewApplicationMetricsCollector

func NewApplicationMetricsCollector() *ApplicationMetricsCollector

NewApplicationMetricsCollector creates a new application metrics collector

func (*ApplicationMetricsCollector) Collect

func (c *ApplicationMetricsCollector) Collect(ch chan<- prometheus.Metric)

Collect implements prometheus.Collector

func (*ApplicationMetricsCollector) Describe

func (c *ApplicationMetricsCollector) Describe(ch chan<- *prometheus.Desc)

Describe implements prometheus.Collector

type EmailAlertHandler

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

EmailAlertHandler sends alerts via email

func NewEmailAlertHandler

func NewEmailAlertHandler(recipients []string, smtpConfig map[string]string) *EmailAlertHandler

func (*EmailAlertHandler) GetName

func (h *EmailAlertHandler) GetName() string

func (*EmailAlertHandler) HandleAlert

func (h *EmailAlertHandler) HandleAlert(ctx context.Context, alert *Alert) error

type HealthCheck

type HealthCheck struct {
	Name       string            `json:"name"`
	Status     HealthStatus      `json:"status"`
	Message    string            `json:"message"`
	Duration   time.Duration     `json:"duration"`
	Timestamp  time.Time         `json:"timestamp"`
	Attributes map[string]string `json:"attributes"`
}

HealthCheck represents a health check

type HealthStatus

type HealthStatus int

HealthStatus represents the status of a health check

const (
	HealthStatusUnknown HealthStatus = iota
	HealthStatusHealthy
	HealthStatusDegraded
	HealthStatusUnhealthy
)

func (HealthStatus) String

func (s HealthStatus) String() string

type JaegerExporter

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

JaegerExporter implements SpanExporter for Jaeger

func (*JaegerExporter) ExportSpans

func (e *JaegerExporter) ExportSpans(ctx context.Context, spans []*Span) error

func (*JaegerExporter) Shutdown

func (e *JaegerExporter) Shutdown(ctx context.Context) error

type LogAlertHandler

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

LogAlertHandler logs alerts to the logger

func NewLogAlertHandler

func NewLogAlertHandler(logger logger.Logger) *LogAlertHandler

func (*LogAlertHandler) GetName

func (h *LogAlertHandler) GetName() string

func (*LogAlertHandler) HandleAlert

func (h *LogAlertHandler) HandleAlert(ctx context.Context, alert *Alert) error

type Metric

type Metric struct {
	Name        string            `json:"name"`
	Type        MetricType        `json:"type"`
	Value       float64           `json:"value"`
	Unit        string            `json:"unit"`
	Labels      map[string]string `json:"labels"`
	Timestamp   time.Time         `json:"timestamp"`
	Description string            `json:"description"`
}

Metric represents a monitoring metric

type MetricExporter

type MetricExporter interface {
	ExportMetric(ctx context.Context, metric *Metric) error
	Shutdown(ctx context.Context) error
	GetName() string
}

MetricExporter interface for metric exporters

type MetricType

type MetricType int

MetricType represents the type of metric

const (
	MetricTypeCounter MetricType = iota
	MetricTypeGauge
	MetricTypeHistogram
	MetricTypeSummary
)

type Monitor

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

Monitor provides comprehensive monitoring and alerting

func NewMonitor

func NewMonitor(config MonitoringConfig) *Monitor

NewMonitor creates a new monitor

func (*Monitor) AddAlert

func (m *Monitor) AddAlert(alert *Alert) error

AddAlert adds a new alert

func (*Monitor) GetAlert

func (m *Monitor) GetAlert(alertID string) (*Alert, error)

GetAlert gets an alert by ID

func (*Monitor) GetHealthStatus

func (m *Monitor) GetHealthStatus() *HealthCheck

GetHealthStatus returns the current health status

func (*Monitor) GetMetric

func (m *Monitor) GetMetric(name string) (*Metric, error)

GetMetric gets a metric by name

func (*Monitor) GetStats

func (m *Monitor) GetStats() map[string]interface{}

GetStats returns monitoring statistics

func (*Monitor) ListAlerts

func (m *Monitor) ListAlerts() []*Alert

ListAlerts returns all alerts

func (*Monitor) ListMetrics

func (m *Monitor) ListMetrics() []*Metric

ListMetrics returns all metrics

func (*Monitor) RecordMetric

func (m *Monitor) RecordMetric(metric *Metric)

RecordMetric records a metric

func (*Monitor) RegisterAlertHandler

func (m *Monitor) RegisterAlertHandler(handler AlertHandler)

RegisterAlertHandler registers an alert handler

func (*Monitor) RemoveAlert

func (m *Monitor) RemoveAlert(alertID string) error

RemoveAlert removes an alert

func (*Monitor) Shutdown

func (m *Monitor) Shutdown(ctx context.Context) error

Shutdown shuts down the monitor

func (*Monitor) UnregisterAlertHandler

func (m *Monitor) UnregisterAlertHandler(name string)

UnregisterAlertHandler unregisters an alert handler

func (*Monitor) UpdateAlert

func (m *Monitor) UpdateAlert(alert *Alert) error

UpdateAlert updates an existing alert

type MonitoringConfig

type MonitoringConfig struct {
	EnableMetrics     bool          `yaml:"enable_metrics" default:"true"`
	EnableAlerts      bool          `yaml:"enable_alerts" default:"true"`
	EnableHealth      bool          `yaml:"enable_health" default:"true"`
	EnableUptime      bool          `yaml:"enable_uptime" default:"true"`
	EnablePerformance bool          `yaml:"enable_performance" default:"true"`
	CheckInterval     time.Duration `yaml:"check_interval" default:"30s"`
	AlertTimeout      time.Duration `yaml:"alert_timeout" default:"5m"`
	RetentionPeriod   time.Duration `yaml:"retention_period" default:"7d"`
	MaxMetrics        int           `yaml:"max_metrics" default:"10000"`
	MaxAlerts         int           `yaml:"max_alerts" default:"1000"`
	Logger            logger.Logger `yaml:"-"`
}

MonitoringConfig contains monitoring configuration

type OTelTracer

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

OTelTracer provides OpenTelemetry-based distributed tracing

func NewOTelTracer

func NewOTelTracer(config OTelTracingConfig) (*OTelTracer, error)

NewOTelTracer creates a new OpenTelemetry tracer

func (*OTelTracer) AddEvent

func (t *OTelTracer) AddEvent(span trace.Span, name string, attributes map[string]string)

AddEvent adds an event to a span

func (t *OTelTracer) AddLink(span trace.Span, traceID, spanID string, attributes map[string]string)

AddLink adds a link to another span

func (*OTelTracer) EndSpan

func (t *OTelTracer) EndSpan(span trace.Span)

EndSpan ends a span

func (*OTelTracer) ExtractTraceContext

func (t *OTelTracer) ExtractTraceContext(ctx context.Context, headers map[string]string) context.Context

ExtractTraceContext extracts trace context from headers

func (*OTelTracer) GetSpanFromContext

func (t *OTelTracer) GetSpanFromContext(ctx context.Context) trace.Span

GetSpanFromContext gets the current span from context

func (*OTelTracer) GetSpanIDFromContext

func (t *OTelTracer) GetSpanIDFromContext(ctx context.Context) string

GetSpanIDFromContext gets the span ID from context

func (*OTelTracer) GetStats

func (t *OTelTracer) GetStats() map[string]interface{}

GetStats returns tracing statistics

func (*OTelTracer) GetTraceIDFromContext

func (t *OTelTracer) GetTraceIDFromContext(ctx context.Context) string

GetTraceIDFromContext gets the trace ID from context

func (*OTelTracer) InjectTraceContext

func (t *OTelTracer) InjectTraceContext(ctx context.Context, headers map[string]string)

InjectTraceContext injects trace context into headers

func (*OTelTracer) SetAttribute

func (t *OTelTracer) SetAttribute(span trace.Span, key, value string)

SetAttribute sets an attribute on a span

func (*OTelTracer) SetStatus

func (t *OTelTracer) SetStatus(span trace.Span, code codes.Code, description string)

SetStatus sets the status of a span

func (*OTelTracer) Shutdown

func (t *OTelTracer) Shutdown(ctx context.Context) error

Shutdown shuts down the tracer

func (*OTelTracer) StartSpan

func (t *OTelTracer) StartSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)

StartSpan starts a new span

type OTelTracingConfig

type OTelTracingConfig struct {
	ServiceName    string        `yaml:"service_name" default:"forge-service"`
	ServiceVersion string        `yaml:"service_version" default:"1.0.0"`
	Environment    string        `yaml:"environment" default:"development"`
	SamplingRate   float64       `yaml:"sampling_rate" default:"1.0"`
	MaxSpans       int           `yaml:"max_spans" default:"10000"`
	FlushInterval  time.Duration `yaml:"flush_interval" default:"5s"`

	// Exporters
	EnableJaeger   bool   `yaml:"enable_jaeger" default:"false"`
	JaegerEndpoint string `yaml:"jaeger_endpoint" default:"http://localhost:14268/api/traces"`
	EnableOTLP     bool   `yaml:"enable_otlp" default:"true"`
	OTLPEndpoint   string `yaml:"otlp_endpoint" default:"http://localhost:4318/v1/traces"`
	EnableConsole  bool   `yaml:"enable_console" default:"false"`

	// Propagation
	EnableB3  bool `yaml:"enable_b3" default:"true"`
	EnableW3C bool `yaml:"enable_w3c" default:"true"`

	Logger logger.Logger `yaml:"-"`
}

OTelTracingConfig contains OpenTelemetry tracing configuration

type Observability

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

Observability provides unified observability functionality

func NewObservability

func NewObservability(config ObservabilityConfig) (*Observability, error)

NewObservability creates a new observability instance

func (*Observability) AddAlert

func (o *Observability) AddAlert(alert *Alert) error

AddAlert adds an alert

func (*Observability) EndSpan

func (o *Observability) EndSpan(span interface{})

EndSpan ends a span

func (*Observability) GetHealthStatus

func (o *Observability) GetHealthStatus() *HealthCheck

GetHealthStatus returns the current health status

func (*Observability) GetPrometheusHandler

func (o *Observability) GetPrometheusHandler() interface{}

GetPrometheusHandler returns the Prometheus metrics HTTP handler

func (*Observability) GetStats

func (o *Observability) GetStats() map[string]interface{}

GetStats returns observability statistics

func (*Observability) ObserveRequest

func (o *Observability) ObserveRequest(ctx context.Context, name string, handler func(context.Context) error) error

ObserveRequest observes a request with tracing and metrics

func (*Observability) RecordError

func (o *Observability) RecordError(ctx context.Context, err error, attributes map[string]string) error

RecordError records an error with tracing and metrics

func (*Observability) RecordMetric

func (o *Observability) RecordMetric(metric *Metric)

RecordMetric records a metric

func (*Observability) RegisterAlertHandler

func (o *Observability) RegisterAlertHandler(handler AlertHandler)

RegisterAlertHandler registers an alert handler

func (*Observability) Shutdown

func (o *Observability) Shutdown(ctx context.Context) error

Shutdown shuts down the observability instance

func (*Observability) StartSpan

func (o *Observability) StartSpan(ctx context.Context, name string, opts ...interface{}) (context.Context, interface{})

StartSpan starts a new span with tracing

func (*Observability) WithMetrics

func (o *Observability) WithMetrics(name string, labels map[string]string, fn func() error) error

WithMetrics records metrics around a function execution

func (*Observability) WithSpan

func (o *Observability) WithSpan(ctx context.Context, name string, fn func(context.Context, interface{}) error) error

WithSpan creates a span and ensures it's properly closed

type ObservabilityConfig

type ObservabilityConfig struct {
	// Monitor configuration
	Monitor MonitoringConfig `yaml:"monitor"`

	// Tracer configuration
	Tracer OTelTracingConfig `yaml:"tracer"`

	// Prometheus configuration
	Prometheus PrometheusConfig `yaml:"prometheus"`

	// Global settings
	EnableMetrics   bool          `yaml:"enable_metrics" default:"true"`
	EnableTracing   bool          `yaml:"enable_tracing" default:"true"`
	EnableAlerts    bool          `yaml:"enable_alerts" default:"true"`
	EnableHealth    bool          `yaml:"enable_health" default:"true"`
	ShutdownTimeout time.Duration `yaml:"shutdown_timeout" default:"30s"`

	Logger logger.Logger `yaml:"-"`
}

ObservabilityConfig contains unified observability configuration

func CreateDefaultConfig

func CreateDefaultConfig() ObservabilityConfig

CreateDefaultConfig creates a default observability configuration

type PrometheusConfig

type PrometheusConfig struct {
	EnableMetrics   bool          `yaml:"enable_metrics" default:"true"`
	EnableRuntime   bool          `yaml:"enable_runtime" default:"true"`
	ListenAddress   string        `yaml:"listen_address" default:":9090"`
	MetricsPath     string        `yaml:"metrics_path" default:"/metrics"`
	EnableGoMetrics bool          `yaml:"enable_go_metrics" default:"true"`
	Logger          logger.Logger `yaml:"-"`
}

PrometheusConfig contains Prometheus configuration

type PrometheusExporter

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

PrometheusExporter provides Prometheus metrics export

func NewPrometheusExporter

func NewPrometheusExporter(config PrometheusConfig) (*PrometheusExporter, error)

NewPrometheusExporter creates a new Prometheus exporter

func (*PrometheusExporter) ExportMetric

func (p *PrometheusExporter) ExportMetric(ctx context.Context, metric *Metric) error

ExportMetric exports a metric to Prometheus

func (*PrometheusExporter) GetHandler

func (p *PrometheusExporter) GetHandler() http.Handler

GetHandler returns the HTTP handler for metrics

func (*PrometheusExporter) GetName

func (p *PrometheusExporter) GetName() string

GetName returns the exporter name

func (*PrometheusExporter) GetRegistry

func (p *PrometheusExporter) GetRegistry() *prometheus.Registry

GetRegistry returns the Prometheus registry

func (*PrometheusExporter) GetStats

func (p *PrometheusExporter) GetStats() map[string]interface{}

GetStats returns Prometheus exporter statistics

func (*PrometheusExporter) Shutdown

func (p *PrometheusExporter) Shutdown(ctx context.Context) error

Shutdown shuts down the exporter

func (*PrometheusExporter) Start

func (p *PrometheusExporter) Start() error

Start starts the Prometheus metrics server

func (*PrometheusExporter) Stop

func (p *PrometheusExporter) Stop(ctx context.Context) error

Stop stops the Prometheus metrics server

type SlackAlertHandler

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

SlackAlertHandler sends alerts to Slack

func NewSlackAlertHandler

func NewSlackAlertHandler(webhookURL, channel string) *SlackAlertHandler

func (*SlackAlertHandler) GetName

func (h *SlackAlertHandler) GetName() string

func (*SlackAlertHandler) HandleAlert

func (h *SlackAlertHandler) HandleAlert(ctx context.Context, alert *Alert) error

type Span

type Span struct {
	TraceID      string            `json:"trace_id"`
	SpanID       string            `json:"span_id"`
	ParentSpanID string            `json:"parent_span_id,omitempty"`
	Name         string            `json:"name"`
	Kind         SpanKind          `json:"kind"`
	StartTime    time.Time         `json:"start_time"`
	EndTime      time.Time         `json:"end_time,omitempty"`
	Duration     time.Duration     `json:"duration,omitempty"`
	Status       SpanStatus        `json:"status"`
	Attributes   map[string]string `json:"attributes"`
	Events       []SpanEvent       `json:"events"`
	Links        []SpanLink        `json:"links"`
	Resource     map[string]string `json:"resource"`
	// contains filtered or unexported fields
}

Span represents a tracing span

type SpanEvent

type SpanEvent struct {
	Name       string            `json:"name"`
	Timestamp  time.Time         `json:"timestamp"`
	Attributes map[string]string `json:"attributes"`
}

SpanEvent represents an event within a span

type SpanExporter

type SpanExporter interface {
	ExportSpans(ctx context.Context, spans []*Span) error
	Shutdown(ctx context.Context) error
}

SpanExporter interface for exporting spans

func NewJaegerExporter

func NewJaegerExporter(endpoint string) SpanExporter

NewJaegerExporter creates a new Jaeger exporter

func NewZipkinExporter

func NewZipkinExporter(endpoint string) SpanExporter

NewZipkinExporter creates a new Zipkin exporter

type SpanKind

type SpanKind int

SpanKind represents the kind of span

const (
	SpanKindUnspecified SpanKind = iota
	SpanKindInternal
	SpanKindServer
	SpanKindClient
	SpanKindProducer
	SpanKindConsumer
)
type SpanLink struct {
	TraceID    string            `json:"trace_id"`
	SpanID     string            `json:"span_id"`
	Attributes map[string]string `json:"attributes"`
}

SpanLink represents a link to another span

type SpanOption

type SpanOption func(*Span)

SpanOption represents a span option

type SpanStatus

type SpanStatus int

SpanStatus represents the status of a span

const (
	SpanStatusUnset SpanStatus = iota
	SpanStatusOK
	SpanStatusError
)

type SystemMetricsCollector

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

SystemMetricsCollector collects system-level metrics

func NewSystemMetricsCollector

func NewSystemMetricsCollector() *SystemMetricsCollector

NewSystemMetricsCollector creates a new system metrics collector

func (*SystemMetricsCollector) Collect

func (c *SystemMetricsCollector) Collect(ch chan<- prometheus.Metric)

Collect implements prometheus.Collector

func (*SystemMetricsCollector) Describe

func (c *SystemMetricsCollector) Describe(ch chan<- *prometheus.Desc)

Describe implements prometheus.Collector

type TraceContext

type TraceContext struct {
	TraceID string `json:"trace_id"`
	SpanID  string `json:"span_id"`
	Sampled bool   `json:"sampled"`
	Flags   string `json:"flags"`
}

TraceContext represents trace context information

type Tracer

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

Tracer provides distributed tracing functionality

func NewTracer

func NewTracer(config TracingConfig) *Tracer

NewTracer creates a new tracer

func (*Tracer) AddEvent

func (t *Tracer) AddEvent(span *Span, name string, attributes map[string]string)

AddEvent adds an event to a span

func (t *Tracer) AddLink(span *Span, traceID, spanID string, attributes map[string]string)

AddLink adds a link to another span

func (*Tracer) EndSpan

func (t *Tracer) EndSpan(span *Span)

EndSpan ends a span

func (*Tracer) ExtractTraceContext

func (t *Tracer) ExtractTraceContext(ctx context.Context, headers map[string]string) context.Context

ExtractTraceContext extracts trace context from headers

func (*Tracer) GetSpanFromContext

func (t *Tracer) GetSpanFromContext(ctx context.Context) *Span

GetSpanFromContext gets the current span from context

func (*Tracer) GetSpanIDFromContext

func (t *Tracer) GetSpanIDFromContext(ctx context.Context) string

GetSpanIDFromContext gets the span ID from context

func (*Tracer) GetStats

func (t *Tracer) GetStats() map[string]interface{}

GetStats returns tracing statistics

func (*Tracer) GetTraceIDFromContext

func (t *Tracer) GetTraceIDFromContext(ctx context.Context) string

GetTraceIDFromContext gets the trace ID from context

func (*Tracer) InjectTraceContext

func (t *Tracer) InjectTraceContext(ctx context.Context, headers map[string]string)

InjectTraceContext injects trace context into headers

func (*Tracer) SetAttribute

func (t *Tracer) SetAttribute(span *Span, key, value string)

SetAttribute sets an attribute on a span

func (*Tracer) SetStatus

func (t *Tracer) SetStatus(span *Span, status SpanStatus)

SetStatus sets the status of a span

func (*Tracer) Shutdown

func (t *Tracer) Shutdown(ctx context.Context) error

Shutdown shuts down the tracer

func (*Tracer) StartSpan

func (t *Tracer) StartSpan(ctx context.Context, name string, opts ...SpanOption) (context.Context, *Span)

StartSpan starts a new span

type TracingConfig

type TracingConfig struct {
	ServiceName    string        `yaml:"service_name" default:"forge-service"`
	ServiceVersion string        `yaml:"service_version" default:"1.0.0"`
	Environment    string        `yaml:"environment" default:"development"`
	SamplingRate   float64       `yaml:"sampling_rate" default:"1.0"`
	MaxSpans       int           `yaml:"max_spans" default:"10000"`
	FlushInterval  time.Duration `yaml:"flush_interval" default:"5s"`
	EnableB3       bool          `yaml:"enable_b3" default:"true"`
	EnableW3C      bool          `yaml:"enable_w3c" default:"true"`
	EnableJaeger   bool          `yaml:"enable_jaeger" default:"false"`
	JaegerEndpoint string        `yaml:"jaeger_endpoint" default:"http://localhost:14268/api/traces"`
	EnableZipkin   bool          `yaml:"enable_zipkin" default:"false"`
	ZipkinEndpoint string        `yaml:"zipkin_endpoint" default:"http://localhost:9411/api/v2/spans"`
	Logger         logger.Logger `yaml:"-"`
}

TracingConfig contains tracing configuration

type ZipkinExporter

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

ZipkinExporter implements SpanExporter for Zipkin

func (*ZipkinExporter) ExportSpans

func (e *ZipkinExporter) ExportSpans(ctx context.Context, spans []*Span) error

func (*ZipkinExporter) Shutdown

func (e *ZipkinExporter) Shutdown(ctx context.Context) error

Jump to

Keyboard shortcuts

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