Skip to content

Go application demonstrating OpenTelemetry metrics (Counter, Histogram, Gauge) with SigNoz Cloud

Notifications You must be signed in to change notification settings

shreyanshjain7174/otel-metrics-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenTelemetry Metrics Demo

A Go application demonstrating OpenTelemetry metrics (Counter, Histogram, Gauge) integrated with SigNoz Cloud. This simple e-commerce cart API tracks HTTP errors, request latency, and cart item counts.

What This Project Demonstrates

  • Counter Metric: Tracking HTTP error counts (4xx/5xx responses)
  • Histogram Metric: Measuring request latency distribution
  • Gauge Metric: Monitoring current cart item totals using observable callbacks

Prerequisites

Quick Start

1. Clone the Repository

git clone https://github.com/shreyanshjain7174/otel-metrics-demo.git
cd otel-metrics-demo

2. Install Dependencies

go mod download

3. Configure Environment Variables

Copy the example environment file and fill in your SigNoz credentials:

cp .env.example .env

Edit .env with your SigNoz Cloud details:

OTEL_EXPORTER_OTLP_ENDPOINT=ingest.us.signoz.cloud:443
OTEL_EXPORTER_OTLP_HEADERS=signoz-access-token=<your-token>
SERVICE_NAME=otel-metrics-demo
SERVER_PORT=8080

Then load the environment variables:

source .env
# Or export them individually
export OTEL_EXPORTER_OTLP_ENDPOINT="ingest.us.signoz.cloud:443"
export OTEL_EXPORTER_OTLP_HEADERS="signoz-access-token=<your-token>"

4. Run the Application

go run main.go

Or build and run:

go build -o otel-metrics-demo
./otel-metrics-demo

You should see output like:

level=INFO msg="Configuration loaded" endpoint=ingest.us.signoz.cloud:443 service=otel-metrics-demo port=8080
level=INFO msg="Initializing OpenTelemetry" ...
level=INFO msg="OpenTelemetry initialized successfully"
level=INFO msg="Starting server" port=8080

Environment Variables

Variable Description Default
OTEL_EXPORTER_OTLP_ENDPOINT SigNoz Cloud OTLP endpoint (required)
OTEL_EXPORTER_OTLP_HEADERS Authentication header with token (required)
SERVICE_NAME Service name in SigNoz otel-metrics-demo
SERVER_PORT HTTP server port 8080

API Endpoints

Method Endpoint Description Request Body
POST /cart/:userId/add Add item to cart {"item": "name", "quantity": 1}
DELETE /cart/:userId/remove Remove item from cart {"item": "name"}
GET /cart/:userId Get cart details -
GET /error Trigger 500 error (testing) -
GET /health Health check -

Testing the API

Add items to cart

curl -X POST http://localhost:8080/cart/user1/add \
  -H "Content-Type: application/json" \
  -d '{"item":"laptop","quantity":1}'

Get cart

curl http://localhost:8080/cart/user1

Remove item

curl -X DELETE http://localhost:8080/cart/user1/remove \
  -H "Content-Type: application/json" \
  -d '{"item":"laptop"}'

Trigger error (for testing error counter)

curl http://localhost:8080/error

Generate Load for Testing

Use this script to generate traffic and see metrics in SigNoz:

#!/bin/bash
for i in {1..100}; do
    curl -s -X POST http://localhost:8080/cart/user$((i%10))/add \
      -H "Content-Type: application/json" \
      -d "{\"item\":\"item$i\",\"quantity\":$((i%5+1))}" > /dev/null

    # Occasionally trigger errors
    if [ $((i % 10)) -eq 0 ]; then
        curl -s http://localhost:8080/error > /dev/null
    fi

    sleep 0.1
done
echo "Load test complete!"

Metrics Exported

Metric Name Type Description
http.server.errors Counter Total HTTP error responses (4xx/5xx)
http.server.duration Histogram Request latency in milliseconds
cart.items.total Gauge Current total items across all carts

Viewing Metrics in SigNoz

  1. Log in to your SigNoz Cloud account
  2. Navigate to MetricsMetrics Explorer
  3. Search for your metrics:
    • http_server_errors (Counter)
    • http_server_duration (Histogram)
    • cart_items_total (Gauge)
  4. Use aggregations like Sum, Rate, P50, P95, P99

Project Structure

.
├── main.go              # Application entry point
├── config/
│   └── config.go        # Configuration management
├── handlers/
│   └── cart.go          # HTTP endpoint handlers
├── middleware/
│   └── metrics.go       # Metrics collection middleware
├── metrics/
│   └── otel.go          # OpenTelemetry initialization
├── models/
│   └── cart.go          # Request/response models
├── storage/
│   └── memory.go        # In-memory cart storage
├── .env.example         # Environment variables template
├── go.mod
└── go.sum

Troubleshooting

Metrics not appearing in SigNoz

  1. Verify environment variables are set correctly
  2. Check the OTLP endpoint format: ingest.<region>.signoz.cloud:443
  3. Ensure your access token is valid
  4. Wait 10-30 seconds (metrics export every 10s)
  5. Check application logs for connection errors

Debug logging

Modify the logger level in main.go:

logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
    Level: slog.LevelDebug,
}))

Learn More

License

MIT

About

Go application demonstrating OpenTelemetry metrics (Counter, Histogram, Gauge) with SigNoz Cloud

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages