forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Telemetry holds validated SDK settings; absence of Endpoint disables export.
|
||||
type Telemetry struct {
|
||||
// Endpoint is the private Collector OTLP HTTP base URL.
|
||||
Endpoint string
|
||||
// Service distinguishes API and worker resource identity.
|
||||
Service string
|
||||
// Version identifies the application build.
|
||||
Version string
|
||||
// Instance is stable for one process lifetime.
|
||||
Instance string
|
||||
// SampleRatio is bounded to [0,1].
|
||||
SampleRatio float64
|
||||
}
|
||||
|
||||
// NewTelemetry validates explicit export settings without contacting the backend.
|
||||
func NewTelemetry(service string) (*Telemetry, error) {
|
||||
cfg := &Telemetry{Endpoint: os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"), Service: service, Version: os.Getenv("APP_VERSION"), Instance: uuid.NewString(), SampleRatio: 1}
|
||||
if cfg.Version == "" {
|
||||
cfg.Version = "lab6"
|
||||
}
|
||||
if raw := os.Getenv("OTEL_SAMPLE_RATIO"); raw != "" {
|
||||
value, err := strconv.ParseFloat(raw, 64)
|
||||
if err != nil || math.IsNaN(value) || value < 0 || value > 1 {
|
||||
return nil, fmt.Errorf("OTEL_SAMPLE_RATIO must be in [0,1]")
|
||||
}
|
||||
cfg.SampleRatio = value
|
||||
}
|
||||
if cfg.Endpoint != "" {
|
||||
u, err := url.Parse(cfg.Endpoint)
|
||||
if err != nil || u.Host == "" || (u.Scheme != "http" && u.Scheme != "https") || u.User != nil || u.RawQuery != "" {
|
||||
return nil, fmt.Errorf("OTLP endpoint must be an HTTP(S) base URL without credentials")
|
||||
}
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user