Skip to main content
Version: 2.8.x(Latest)

Metric - Basic Example

Code Source: https://github.com/gogf/examples/tree/main/observability/metric/basic

Description

This example demonstrates the basic usage of various metric types in GoFrame using OpenTelemetry and Prometheus integration. It shows how to:

  • Create and use different types of metrics
  • Configure metric attributes
  • Export metrics in Prometheus format
  • Set up a metrics endpoint

Structure

- `go.mod`: The Go module file for dependency management
- `go.sum`: The Go module checksums file
- `main.go`: The main application demonstrating metric usage

Features

The example showcases the following metric types:

  1. Counter

    • Cumulative measurements
    • Only increases
    • Used for counting events
  2. UpDownCounter

    • Bidirectional counter
    • Can increase and decrease
    • Used for measuring varying quantities
  3. Histogram

    • Distribution of measurements
    • Configurable buckets
    • Used for latency/size measurements
  4. Observable Metrics

    • Counter
    • UpDownCounter
    • Gauge
    • Updated via callbacks

Requirements

Usage

  1. Run the example:

    go run main.go
  2. Access the metrics:

    # Using curl
    curl http://localhost:8000/metrics

    # Or open in browser
    http://localhost:8000/metrics
  3. Example metrics output:

    # HELP goframe_metric_demo_counter This is a simple demo for Counter usage
    goframe_metric_demo_counter{const_attr_1="1"} 11

    # HELP goframe_metric_demo_histogram This is a simple demo for histogram usage
    goframe_metric_demo_histogram_bucket{const_attr_3="3",le="0"} 0
    goframe_metric_demo_histogram_bucket{const_attr_3="3",le="10"} 1
    ...

Implementation Details

The example demonstrates:

  1. Proper metric initialization and configuration
  2. Different ways to update metrics
  3. Attribute handling
  4. Prometheus integration
  5. Observable metric callbacks

Notes

  • Metrics are exported in Prometheus format
  • Default port is 8000
  • Metrics endpoint is at /metrics
  • All metrics include constant attributes
  • Observable metrics are updated via callbacks
  • Proper cleanup is handled via defer