Documentation
¶
Overview ¶
Package metrics provides a metrics collection system for monitoring gort system performance and health. It supports pluggable collectors for integration with various monitoring backends (Prometheus, StatsD, etc.).
Metrics Architecture:
┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ System │────▶│ Collector │────▶│ Backend │ │ Components │ │ Interface │ │ (Prometheus)│ └─────────────┘ └─────────────┘ └─────────────┘
Metric Types:
- Counters: Messages received/sent, errors
- Gauges: Active sessions, channel status
- Histograms: Processing durations, retry attempts
Basic Usage:
// Use the global collector
metrics.RecordMessageReceived("dingtalk", "inbound", "text")
metrics.RecordMessageSent("feishu", "outbound", "image")
// Or use a custom collector
stats := metrics.NewStatsCollector()
metrics.SetGlobalCollector(stats)
// Get statistics
stats := stats.GetStats()
fmt.Printf("Messages: %d, Errors: %d\n", stats.MessagesReceived, stats.Errors)
Custom Collector Implementation:
type PrometheusCollector struct {
messagesReceived *prometheus.CounterVec
}
func (c *PrometheusCollector) RecordMessageReceived(channel, direction, msgType string) {
c.messagesReceived.WithLabelValues(channel, direction, msgType).Inc()
}
// Implement other methods...
Thread Safety:
All collectors in this package are safe for concurrent use. The global collector functions are also thread-safe.
Index ¶
- func RecordChannelError(channel, channelType, errorType string)
- func RecordChannelStatus(channel, channelType string, running bool)
- func RecordMessageDuration(channel string, duration time.Duration)
- func RecordMessageReceived(channel, direction, msgType string)
- func RecordMessageSent(channel, direction, msgType string)
- func RecordMiddlewareDuration(middleware string, duration time.Duration)
- func RecordRetry(operation string, attempts int, success bool)
- func RecordSessionActive(count int)
- func RecordSessionDuration(duration time.Duration)
- func SetGlobalCollector(c Collector)
- type Collector
- type DefaultCollector
- func (d *DefaultCollector) RecordChannelError(channel, channelType, errorType string)
- func (d *DefaultCollector) RecordChannelStatus(channel, channelType string, running bool)
- func (d *DefaultCollector) RecordMessageDuration(channel string, duration time.Duration)
- func (d *DefaultCollector) RecordMessageReceived(channel, direction, msgType string)
- func (d *DefaultCollector) RecordMessageSent(channel, direction, msgType string)
- func (d *DefaultCollector) RecordMiddlewareDuration(middleware string, duration time.Duration)
- func (d *DefaultCollector) RecordRetry(operation string, attempts int, success bool)
- func (d *DefaultCollector) RecordSessionActive(count int)
- func (d *DefaultCollector) RecordSessionDuration(duration time.Duration)
- type PrometheusCollector
- func (p *PrometheusCollector) RecordChannelError(channel, channelType, errorType string)
- func (p *PrometheusCollector) RecordChannelStatus(channel, channelType string, running bool)
- func (p *PrometheusCollector) RecordMessageDuration(channel string, duration time.Duration)
- func (p *PrometheusCollector) RecordMessageReceived(channel, direction, msgType string)
- func (p *PrometheusCollector) RecordMessageSent(channel, direction, msgType string)
- func (p *PrometheusCollector) RecordMiddlewareDuration(middleware string, duration time.Duration)
- func (p *PrometheusCollector) RecordRetry(operation string, attempts int, success bool)
- func (p *PrometheusCollector) RecordSessionActive(count int)
- func (p *PrometheusCollector) RecordSessionDuration(duration time.Duration)
- type Stats
- type StatsCollector
- func (s *StatsCollector) GetStats() Stats
- func (s *StatsCollector) RecordChannelError(channel, channelType, errorType string)
- func (s *StatsCollector) RecordChannelStatus(channel, channelType string, running bool)
- func (s *StatsCollector) RecordMessageDuration(channel string, duration time.Duration)
- func (s *StatsCollector) RecordMessageReceived(channel, direction, msgType string)
- func (s *StatsCollector) RecordMessageSent(channel, direction, msgType string)
- func (s *StatsCollector) RecordMiddlewareDuration(middleware string, duration time.Duration)
- func (s *StatsCollector) RecordRetry(operation string, attempts int, success bool)
- func (s *StatsCollector) RecordSessionActive(count int)
- func (s *StatsCollector) RecordSessionDuration(duration time.Duration)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RecordChannelError ¶
func RecordChannelError(channel, channelType, errorType string)
RecordChannelError records a channel error using the global collector.
Parameters:
- channel: The channel ID
- channelType: The type of channel
- errorType: The type of error
Example:
metrics.RecordChannelError("my-dingtalk", "dingtalk", "connection")
func RecordChannelStatus ¶
RecordChannelStatus records channel status using the global collector.
Parameters:
- channel: The channel ID
- channelType: The type of channel
- running: Whether the channel is running
Example:
metrics.RecordChannelStatus("my-dingtalk", "dingtalk", true)
func RecordMessageDuration ¶
RecordMessageDuration records message processing duration using the global collector.
Parameters:
- channel: The channel ID
- duration: The processing duration
Example:
start := time.Now()
processMessage(msg)
metrics.RecordMessageDuration("dingtalk", time.Since(start))
func RecordMessageReceived ¶
func RecordMessageReceived(channel, direction, msgType string)
RecordMessageReceived records a received message using the global collector.
Parameters:
- channel: The channel ID
- direction: The message direction
- msgType: The message type
Example:
metrics.RecordMessageReceived("dingtalk", "inbound", "text")
func RecordMessageSent ¶
func RecordMessageSent(channel, direction, msgType string)
RecordMessageSent records a sent message using the global collector.
Parameters:
- channel: The channel ID
- direction: The message direction
- msgType: The message type
Example:
metrics.RecordMessageSent("feishu", "outbound", "image")
func RecordMiddlewareDuration ¶
RecordMiddlewareDuration records middleware execution time using the global collector.
Parameters:
- middleware: The middleware name
- duration: The execution duration
Example:
start := time.Now()
next(ctx, msg)
metrics.RecordMiddlewareDuration("auth", time.Since(start))
func RecordRetry ¶
RecordRetry records retry attempts using the global collector.
Parameters:
- operation: The operation being retried
- attempts: The number of attempts made
- success: Whether the operation succeeded
Example:
metrics.RecordRetry("api-call", 3, true)
func RecordSessionActive ¶
func RecordSessionActive(count int)
RecordSessionActive records active session count using the global collector.
Parameters:
- count: The number of active sessions
Example:
metrics.RecordSessionActive(len(sessions))
func RecordSessionDuration ¶
RecordSessionDuration records session duration using the global collector.
Parameters:
- duration: The session duration
Example:
metrics.RecordSessionDuration(time.Since(sessionStart))
func SetGlobalCollector ¶
func SetGlobalCollector(c Collector)
SetGlobalCollector sets the global metrics collector. All subsequent metric recordings will use this collector.
Parameters:
- c: The collector to set as global
Example:
collector := metrics.NewStatsCollector() metrics.SetGlobalCollector(collector)
Types ¶
type Collector ¶
type Collector interface {
// RecordMessageReceived records a message being received.
//
// Parameters:
// - channel: The channel ID (e.g., "dingtalk", "feishu")
// - direction: The message direction ("inbound" or "outbound")
// - msgType: The message type (e.g., "text", "image")
RecordMessageReceived(channel, direction, msgType string)
// RecordMessageSent records a message being sent.
//
// Parameters:
// - channel: The channel ID
// - direction: The message direction
// - msgType: The message type
RecordMessageSent(channel, direction, msgType string)
// RecordMessageDuration records the time taken to process a message.
//
// Parameters:
// - channel: The channel ID
// - duration: The processing duration
RecordMessageDuration(channel string, duration time.Duration)
// RecordChannelStatus records the current status of a channel.
//
// Parameters:
// - channel: The channel ID
// - channelType: The type of channel (e.g., "dingtalk", "feishu")
// - running: Whether the channel is currently running
RecordChannelStatus(channel, channelType string, running bool)
// RecordChannelError records an error from a channel.
//
// Parameters:
// - channel: The channel ID
// - channelType: The type of channel
// - errorType: The type of error (e.g., "connection", "auth")
RecordChannelError(channel, channelType, errorType string)
// RecordSessionActive records the current number of active sessions.
//
// Parameters:
// - count: The number of active sessions
RecordSessionActive(count int)
// RecordSessionDuration records the duration of a session.
//
// Parameters:
// - duration: The session duration
RecordSessionDuration(duration time.Duration)
// RecordMiddlewareDuration records the time spent in middleware.
//
// Parameters:
// - middleware: The middleware name
// - duration: The execution duration
RecordMiddlewareDuration(middleware string, duration time.Duration)
// RecordRetry records a retry attempt.
//
// Parameters:
// - operation: The operation being retried
// - attempts: The number of attempts made
// - success: Whether the operation eventually succeeded
RecordRetry(operation string, attempts int, success bool)
}
Collector defines the interface for collecting metrics. Implementations can integrate with various monitoring systems such as Prometheus, StatsD, CloudWatch, etc.
All methods should be safe for concurrent use.
func GetGlobalCollector ¶
func GetGlobalCollector() Collector
GetGlobalCollector returns the global metrics collector.
Returns the currently configured global collector.
Example:
collector := metrics.GetGlobalCollector()
collector.RecordMessageReceived("channel", "inbound", "text")
type DefaultCollector ¶
type DefaultCollector struct{}
DefaultCollector is a no-op collector. Use this when metrics collection is not needed.
func (*DefaultCollector) RecordChannelError ¶
func (d *DefaultCollector) RecordChannelError(channel, channelType, errorType string)
RecordChannelError is a no-op.
func (*DefaultCollector) RecordChannelStatus ¶
func (d *DefaultCollector) RecordChannelStatus(channel, channelType string, running bool)
RecordChannelStatus is a no-op.
func (*DefaultCollector) RecordMessageDuration ¶
func (d *DefaultCollector) RecordMessageDuration(channel string, duration time.Duration)
RecordMessageDuration is a no-op.
func (*DefaultCollector) RecordMessageReceived ¶
func (d *DefaultCollector) RecordMessageReceived(channel, direction, msgType string)
RecordMessageReceived is a no-op.
func (*DefaultCollector) RecordMessageSent ¶
func (d *DefaultCollector) RecordMessageSent(channel, direction, msgType string)
RecordMessageSent is a no-op.
func (*DefaultCollector) RecordMiddlewareDuration ¶
func (d *DefaultCollector) RecordMiddlewareDuration(middleware string, duration time.Duration)
RecordMiddlewareDuration is a no-op.
func (*DefaultCollector) RecordRetry ¶
func (d *DefaultCollector) RecordRetry(operation string, attempts int, success bool)
RecordRetry is a no-op.
func (*DefaultCollector) RecordSessionActive ¶
func (d *DefaultCollector) RecordSessionActive(count int)
RecordSessionActive is a no-op.
func (*DefaultCollector) RecordSessionDuration ¶
func (d *DefaultCollector) RecordSessionDuration(duration time.Duration)
RecordSessionDuration is a no-op.
type PrometheusCollector ¶
type PrometheusCollector struct {
// contains filtered or unexported fields
}
PrometheusCollector implements the Collector interface using Prometheus metrics.
func NewPrometheusCollector ¶
func NewPrometheusCollector() *PrometheusCollector
NewPrometheusCollector creates a new PrometheusCollector with default metrics.
func NewPrometheusCollectorWithRegistry ¶
func NewPrometheusCollectorWithRegistry(reg prometheus.Registerer) *PrometheusCollector
NewPrometheusCollectorWithRegistry creates a new PrometheusCollector with a custom registry.
func (*PrometheusCollector) RecordChannelError ¶
func (p *PrometheusCollector) RecordChannelError(channel, channelType, errorType string)
RecordChannelError increments the channel errors counter.
func (*PrometheusCollector) RecordChannelStatus ¶
func (p *PrometheusCollector) RecordChannelStatus(channel, channelType string, running bool)
RecordChannelStatus updates the channel status gauge.
func (*PrometheusCollector) RecordMessageDuration ¶
func (p *PrometheusCollector) RecordMessageDuration(channel string, duration time.Duration)
RecordMessageDuration records the duration of message processing.
func (*PrometheusCollector) RecordMessageReceived ¶
func (p *PrometheusCollector) RecordMessageReceived(channel, direction, msgType string)
RecordMessageReceived increments the received messages counter.
func (*PrometheusCollector) RecordMessageSent ¶
func (p *PrometheusCollector) RecordMessageSent(channel, direction, msgType string)
RecordMessageSent increments the sent messages counter.
func (*PrometheusCollector) RecordMiddlewareDuration ¶
func (p *PrometheusCollector) RecordMiddlewareDuration(middleware string, duration time.Duration)
RecordMiddlewareDuration records the duration of middleware execution.
func (*PrometheusCollector) RecordRetry ¶
func (p *PrometheusCollector) RecordRetry(operation string, attempts int, success bool)
RecordRetry records retry attempts.
func (*PrometheusCollector) RecordSessionActive ¶
func (p *PrometheusCollector) RecordSessionActive(count int)
RecordSessionActive updates the active sessions gauge.
func (*PrometheusCollector) RecordSessionDuration ¶
func (p *PrometheusCollector) RecordSessionDuration(duration time.Duration)
RecordSessionDuration records the duration of a session.
type Stats ¶
type Stats struct {
// MessagesReceived is the total number of messages received.
MessagesReceived int64
// MessagesSent is the total number of messages sent.
MessagesSent int64
// Errors is the total number of errors encountered.
Errors int64
// ActiveSessions is the current number of active sessions.
ActiveSessions int64
}
Stats holds basic statistics. This is a simple data structure for retrieving collected metrics.
type StatsCollector ¶
type StatsCollector struct {
// contains filtered or unexported fields
}
StatsCollector is a simple collector that tracks basic statistics. It provides thread-safe access to counters and gauges.
This type is safe for concurrent use.
func NewStatsCollector ¶
func NewStatsCollector() *StatsCollector
NewStatsCollector creates a new StatsCollector.
Returns a new StatsCollector with zeroed statistics.
Example:
collector := metrics.NewStatsCollector() metrics.SetGlobalCollector(collector)
func (*StatsCollector) GetStats ¶
func (s *StatsCollector) GetStats() Stats
GetStats returns a copy of the current statistics.
Returns a Stats struct containing the current values. The returned Stats is a snapshot and will not be updated.
Example:
stats := collector.GetStats()
fmt.Printf("Total messages: %d\n", stats.MessagesReceived)
func (*StatsCollector) RecordChannelError ¶
func (s *StatsCollector) RecordChannelError(channel, channelType, errorType string)
RecordChannelError increments the error counter.
func (*StatsCollector) RecordChannelStatus ¶
func (s *StatsCollector) RecordChannelStatus(channel, channelType string, running bool)
RecordChannelStatus is currently a no-op for StatsCollector.
func (*StatsCollector) RecordMessageDuration ¶
func (s *StatsCollector) RecordMessageDuration(channel string, duration time.Duration)
RecordMessageDuration is currently a no-op for StatsCollector.
func (*StatsCollector) RecordMessageReceived ¶
func (s *StatsCollector) RecordMessageReceived(channel, direction, msgType string)
RecordMessageReceived increments the received message counter.
func (*StatsCollector) RecordMessageSent ¶
func (s *StatsCollector) RecordMessageSent(channel, direction, msgType string)
RecordMessageSent increments the sent message counter.
func (*StatsCollector) RecordMiddlewareDuration ¶
func (s *StatsCollector) RecordMiddlewareDuration(middleware string, duration time.Duration)
RecordMiddlewareDuration is currently a no-op for StatsCollector.
func (*StatsCollector) RecordRetry ¶
func (s *StatsCollector) RecordRetry(operation string, attempts int, success bool)
RecordRetry is currently a no-op for StatsCollector.
func (*StatsCollector) RecordSessionActive ¶
func (s *StatsCollector) RecordSessionActive(count int)
RecordSessionActive updates the active session count.
func (*StatsCollector) RecordSessionDuration ¶
func (s *StatsCollector) RecordSessionDuration(duration time.Duration)
RecordSessionDuration is currently a no-op for StatsCollector.