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.
- 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
- Go 1.21 or higher
- SigNoz Cloud account (Sign up here)
git clone https://github.com/shreyanshjain7174/otel-metrics-demo.git
cd otel-metrics-demogo mod downloadCopy the example environment file and fill in your SigNoz credentials:
cp .env.example .envEdit .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=8080Then 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>"go run main.goOr build and run:
go build -o otel-metrics-demo
./otel-metrics-demoYou 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
| 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 |
| 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 | - |
curl -X POST http://localhost:8080/cart/user1/add \
-H "Content-Type: application/json" \
-d '{"item":"laptop","quantity":1}'curl http://localhost:8080/cart/user1curl -X DELETE http://localhost:8080/cart/user1/remove \
-H "Content-Type: application/json" \
-d '{"item":"laptop"}'curl http://localhost:8080/errorUse 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!"| 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 |
- Log in to your SigNoz Cloud account
- Navigate to Metrics → Metrics Explorer
- Search for your metrics:
http_server_errors(Counter)http_server_duration(Histogram)cart_items_total(Gauge)
- Use aggregations like Sum, Rate, P50, P95, P99
.
├── 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
- Verify environment variables are set correctly
- Check the OTLP endpoint format:
ingest.<region>.signoz.cloud:443 - Ensure your access token is valid
- Wait 10-30 seconds (metrics export every 10s)
- Check application logs for connection errors
Modify the logger level in main.go:
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))MIT