Documentation
¶
Overview ¶
Example (RealWorldUsage) ¶
package main
import (
"fmt"
"time"
"github.com/massonsky/buffalo/pkg/metrics"
)
func main() {
// Initialize metrics collector
collector := metrics.NewCollector()
collector.SetLabel("service", "api-gateway")
collector.SetLabel("version", "1.0.0")
// Create metrics
requestCounter := collector.Counter("http_requests_total")
errorCounter := collector.Counter("http_errors_total")
activeConnections := collector.Gauge("active_connections")
requestDuration := collector.Histogram("request_duration_seconds",
[]float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0})
// Simulate some activity
for i := 0; i < 10; i++ {
// Track request
requestCounter.Inc()
activeConnections.Inc()
// Simulate request processing
start := time.Now()
time.Sleep(time.Millisecond * 10)
duration := time.Since(start).Seconds()
requestDuration.Observe(duration)
// Simulate occasional errors
if i%3 == 0 {
errorCounter.Inc()
}
activeConnections.Dec()
}
// Export metrics
fmt.Println("=== Metrics Summary ===")
fmt.Printf("Total Requests: %d\n", requestCounter.Get())
fmt.Printf("Total Errors: %d\n", errorCounter.Get())
fmt.Printf("Active Connections: %d\n", activeConnections.Get())
fmt.Printf("Average Duration: %.3f seconds\n", requestDuration.Average())
}
Index ¶
- type Collector
- func (c *Collector) All() map[string]Metric
- func (c *Collector) Counter(name string) *Counter
- func (c *Collector) Gauge(name string) *Gauge
- func (c *Collector) Get(name string) (Metric, error)
- func (c *Collector) GetLabels() map[string]string
- func (c *Collector) GetOrCreate(name string, factory func() Metric) Metric
- func (c *Collector) Histogram(name string, buckets []float64) *Histogram
- func (c *Collector) Register(name string, metric Metric) error
- func (c *Collector) Reset()
- func (c *Collector) SetLabel(key, value string)
- func (c *Collector) Snapshot() *Snapshot
- type Counter
- type ExportFormat
- type Exporter
- type Gauge
- type Histogram
- type Metric
- type MetricSnapshot
- type Snapshot
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector collects and manages metrics.
Example ¶
package main
import (
"fmt"
"github.com/massonsky/buffalo/pkg/metrics"
)
func main() {
// Create a new metrics collector
collector := metrics.NewCollector()
collector.SetLabel("service", "buffalo")
collector.SetLabel("version", "0.1.0")
// Create and use a counter
counter := collector.Counter("requests_total")
counter.Add(100)
// Create and use a gauge
gauge := collector.Gauge("active_connections")
gauge.Set(42)
fmt.Printf("Requests: %d\n", counter.Get())
fmt.Printf("Connections: %d\n", gauge.Get())
}
Output: Requests: 100 Connections: 42
func (*Collector) GetOrCreate ¶
GetOrCreate retrieves a metric or creates it if it doesn't exist.
type Counter ¶
type Counter struct {
// contains filtered or unexported fields
}
Counter is a metric that can only increase.
Example ¶
package main
import (
"fmt"
"github.com/massonsky/buffalo/pkg/metrics"
)
func main() {
counter := metrics.NewCounter("http_requests")
// Increment by 1
counter.Inc()
// Add a specific value
counter.Add(5)
fmt.Printf("Total requests: %d\n", counter.Get())
}
Output: Total requests: 6
type ExportFormat ¶
type ExportFormat string
ExportFormat represents the format for exporting metrics.
const ( FormatText ExportFormat = "text" FormatJSON ExportFormat = "json" FormatPrometheus ExportFormat = "prometheus" )
type Exporter ¶
type Exporter struct {
// contains filtered or unexported fields
}
Exporter exports metrics in various formats.
Example (Prometheus) ¶
package main
import (
"os"
"github.com/massonsky/buffalo/pkg/metrics"
)
func main() {
collector := metrics.NewCollector()
collector.SetLabel("env", "production")
counter := collector.Counter("http_requests_total")
counter.Add(1000)
gauge := collector.Gauge("cpu_usage_percent")
gauge.Set(75)
exporter := metrics.NewExporter(collector)
exporter.Export(metrics.FormatPrometheus, os.Stdout)
}
Example (Text) ¶
package main
import (
"os"
"github.com/massonsky/buffalo/pkg/metrics"
)
func main() {
collector := metrics.NewCollector()
collector.SetLabel("service", "buffalo")
counter := collector.Counter("operations_total")
counter.Add(150)
exporter := metrics.NewExporter(collector)
exporter.Export(metrics.FormatText, os.Stdout)
}
func NewExporter ¶
NewExporter creates a new metrics exporter.
func (*Exporter) Export ¶
func (e *Exporter) Export(format ExportFormat, writer io.Writer) error
Export exports metrics in the specified format.
func (*Exporter) LogMetrics ¶
func (e *Exporter) LogMetrics()
LogMetrics logs all metrics using the configured logger.
type Gauge ¶
type Gauge struct {
// contains filtered or unexported fields
}
Gauge is a metric that can increase or decrease.
Example ¶
package main
import (
"fmt"
"github.com/massonsky/buffalo/pkg/metrics"
)
func main() {
gauge := metrics.NewGauge("memory_usage_mb")
// Set to a specific value
gauge.Set(512)
// Increment
gauge.Inc()
// Decrement
gauge.Dec()
// Add/subtract values
gauge.Add(100)
gauge.Sub(50)
fmt.Printf("Memory usage: %d MB\n", gauge.Get())
}
Output: Memory usage: 562 MB
type Histogram ¶
type Histogram struct {
// contains filtered or unexported fields
}
Histogram tracks the distribution of values.
Example ¶
package main
import (
"fmt"
"github.com/massonsky/buffalo/pkg/metrics"
)
func main() {
// Define buckets for request duration in seconds
buckets := []float64{0.1, 0.5, 1.0, 2.0, 5.0}
histogram := metrics.NewHistogram("request_duration_seconds", buckets)
// Record observations
histogram.Observe(0.2)
histogram.Observe(0.8)
histogram.Observe(1.5)
histogram.Observe(3.0)
fmt.Printf("Count: %d\n", histogram.Count())
fmt.Printf("Average: %.2f seconds\n", histogram.Average())
}
Output: Count: 4 Average: 1.38 seconds
func NewHistogram ¶
NewHistogram creates a new histogram metric.
type Metric ¶
type Metric interface {
// Name returns the metric name.
Name() string
// Type returns the metric type.
Type() string
// Value returns the current value of the metric.
Value() interface{}
// Reset resets the metric to its initial state.
Reset()
}
Metric is the interface for all metrics.
type MetricSnapshot ¶
MetricSnapshot represents a snapshot of a metric.