|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "math/rand" |
| 7 | + "time" |
| 8 | + |
| 9 | + "go.opentelemetry.io/otel" |
| 10 | + "go.opentelemetry.io/otel/attribute" |
| 11 | + "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" |
| 12 | + otelmetric "go.opentelemetry.io/otel/metric" |
| 13 | + "go.opentelemetry.io/otel/sdk/metric" |
| 14 | + "go.opentelemetry.io/otel/sdk/resource" |
| 15 | + semconv "go.opentelemetry.io/otel/semconv/v1.26.0" |
| 16 | +) |
| 17 | + |
| 18 | +func initMeterProvider(ctx context.Context) (*metric.MeterProvider, error) { |
| 19 | + exporter, err := otlpmetrichttp.New(ctx) |
| 20 | + if err != nil { |
| 21 | + return nil, err |
| 22 | + } |
| 23 | + |
| 24 | + res, err := resource.New(ctx, |
| 25 | + resource.WithFromEnv(), |
| 26 | + resource.WithAttributes( |
| 27 | + semconv.ServiceNameKey.String("custom-metrics-example"), |
| 28 | + semconv.DeploymentEnvironmentKey.String("development"), |
| 29 | + ), |
| 30 | + ) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + |
| 35 | + mp := metric.NewMeterProvider( |
| 36 | + metric.WithResource(res), |
| 37 | + metric.WithReader(metric.NewPeriodicReader(exporter, |
| 38 | + metric.WithInterval(15*time.Second), |
| 39 | + )), |
| 40 | + ) |
| 41 | + otel.SetMeterProvider(mp) |
| 42 | + return mp, nil |
| 43 | +} |
| 44 | + |
| 45 | +func main() { |
| 46 | + ctx := context.Background() |
| 47 | + |
| 48 | + mp, err := initMeterProvider(ctx) |
| 49 | + if err != nil { |
| 50 | + log.Fatalf("init meter provider: %v", err) |
| 51 | + } |
| 52 | + defer func() { |
| 53 | + if err := mp.ForceFlush(ctx); err != nil { |
| 54 | + log.Printf("flush error: %v", err) |
| 55 | + } |
| 56 | + if err := mp.Shutdown(ctx); err != nil { |
| 57 | + log.Printf("shutdown error: %v", err) |
| 58 | + } |
| 59 | + }() |
| 60 | + |
| 61 | + meter := otel.Meter("custom-metrics-example") |
| 62 | + |
| 63 | + // Counter: tracks how many times an event occurred |
| 64 | + resolutionCounter, err := meter.Int64Counter( |
| 65 | + "subscription.upgrade.notification.resolution", |
| 66 | + otelmetric.WithDescription("Count of subscription upgrade notification resolutions"), |
| 67 | + ) |
| 68 | + if err != nil { |
| 69 | + log.Fatalf("create counter: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + // Histogram: tracks distribution of a value (e.g. latency) |
| 73 | + requestDuration, err := meter.Float64Histogram( |
| 74 | + "request.duration", |
| 75 | + otelmetric.WithDescription("Request duration in seconds"), |
| 76 | + otelmetric.WithUnit("s"), |
| 77 | + ) |
| 78 | + if err != nil { |
| 79 | + log.Fatalf("create histogram: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + // Gauge: tracks a current value that can go up or down |
| 83 | + queueDepth, err := meter.Int64Gauge( |
| 84 | + "queue.depth", |
| 85 | + otelmetric.WithDescription("Current number of items in the queue"), |
| 86 | + ) |
| 87 | + if err != nil { |
| 88 | + log.Fatalf("create gauge: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + statuses := []string{"success", "failure", "timeout"} |
| 92 | + reasons := []string{"completed", "rejected", "expired"} |
| 93 | + products := []string{"premium", "basic", "trial"} |
| 94 | + |
| 95 | + log.Println("emitting metrics every 5s, press Ctrl+C to stop") |
| 96 | + |
| 97 | + for i := 0; i < 10; i++ { |
| 98 | + status := statuses[rand.Intn(len(statuses))] |
| 99 | + reason := reasons[rand.Intn(len(reasons))] |
| 100 | + product := products[rand.Intn(len(products))] |
| 101 | + |
| 102 | + // IMPORTANT: never pass empty string as attribute value. |
| 103 | + // Last9 follows the Prometheus data model: labels with empty values |
| 104 | + // silently — the metric is recorded but that label dimension is absent. |
| 105 | + // Use a sentinel like "unknown" if the value may be empty. |
| 106 | + if status == "" { |
| 107 | + status = "unknown" |
| 108 | + } |
| 109 | + if reason == "" { |
| 110 | + reason = "unknown" |
| 111 | + } |
| 112 | + if product == "" { |
| 113 | + product = "unknown" |
| 114 | + } |
| 115 | + |
| 116 | + resolutionCounter.Add(ctx, 1, otelmetric.WithAttributes( |
| 117 | + attribute.String("status", status), |
| 118 | + attribute.String("reason", reason), |
| 119 | + attribute.String("product_type", product), |
| 120 | + )) |
| 121 | + |
| 122 | + duration := 0.1 + rand.Float64()*0.9 |
| 123 | + requestDuration.Record(ctx, duration, otelmetric.WithAttributes( |
| 124 | + attribute.String("method", "POST"), |
| 125 | + attribute.String("status_code", "200"), |
| 126 | + attribute.String("product_type", product), |
| 127 | + )) |
| 128 | + |
| 129 | + depth := int64(rand.Intn(100)) |
| 130 | + queueDepth.Record(ctx, depth, otelmetric.WithAttributes( |
| 131 | + attribute.String("queue_name", "notifications"), |
| 132 | + )) |
| 133 | + |
| 134 | + log.Printf("recorded: status=%s reason=%s product_type=%s duration=%.3fs queue_depth=%d", |
| 135 | + status, reason, product, duration, depth) |
| 136 | + |
| 137 | + time.Sleep(5 * time.Second) |
| 138 | + } |
| 139 | + |
| 140 | + log.Println("done — metrics will appear in Last9 as:") |
| 141 | + log.Println(" subscription_upgrade_notification_resolution_total") |
| 142 | + log.Println(" request_duration_seconds_bucket / _count / _sum") |
| 143 | + log.Println(" queue_depth") |
| 144 | +} |
0 commit comments