Repository navigation
Expand file tree
/
Copy pathobservability.go
More file actions
1367 lines (1115 loc) · 44.3 KB
/
Copy pathobservability.go
File metadata and controls
1367 lines (1115 loc) · 44.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"context"
"crypto/sha256"
"crypto/subtle"
"crypto/tls"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
const (
defaultPrometheusAddress = "127.0.0.1"
defaultPrometheusPort = 9464
defaultPrometheusPath = "/metrics"
defaultOTelServiceName = "pfxhttp"
defaultOTelSampleRatio = 1.0
instrumentationName = "pfxhttp"
componentBackendHTTP = "backend_http"
componentDovecotSASL = "dovecot_sasl"
componentOIDC = "oidc"
componentPolicyService = "policy_service"
componentSocketMap = "socket_map"
eventAccept = "accept"
eventClose = "close"
defaultBackendName = "default"
statusClassError = "error"
postfixActionReject = "REJECT"
labelComponent = "component"
labelEvent = "event"
labelListener = "listener"
labelMethod = "method"
labelName = "name"
labelOutcome = "outcome"
labelResult = "result"
labelReason = "reason"
labelState = "state"
labelStatus = "status"
labelStatusClass = "status_class"
outcomeContinue = "continue"
outcomeError = "error"
outcomeFail = "fail"
outcomeNotFound = "not_found"
outcomeOK = "ok"
outcomeTempfail = "tempfail"
outcomeTimeout = "timeout"
outcomeUnknown = "unknown"
resultError = "error"
resultOK = "ok"
resultStatusCode = "status_code"
)
const (
socketMapReadSpanName = "socket_map read request"
socketMapDecodeSpanName = "socket_map decode request"
socketMapBackendSpanName = "socket_map backend exchange"
socketMapEncodeSpanName = "socket_map encode response"
socketMapWriteSpanName = "socket_map write response"
policyServiceReadSpanName = "policy_service read request"
policyServiceDecodeSpanName = "policy_service decode request"
policyServiceBackendSpanName = "policy_service backend exchange"
policyServiceEncodeSpanName = "policy_service encode response"
policyServiceWriteSpanName = "policy_service write response"
dovecotSASLMechanismSpanName = "dovecot_sasl mechanism step"
dovecotSASLBackendSpanName = "dovecot_sasl backend auth"
dovecotSASLResponseSpanName = "dovecot_sasl write response"
dovecotSASLWaitSpanName = "dovecot_sasl continuation wait"
requestQueueSpanName = "listener request queue"
)
const (
observabilityContextKey ctxKey = "observability"
backendOperationKey ctxKey = "backend_operation"
)
// ObservabilityConfig contains all opt-in metrics and tracing settings.
type ObservabilityConfig struct {
PrometheusEnabled bool `mapstructure:"prometheus_enabled"`
PrometheusAddress string `mapstructure:"prometheus_address"`
PrometheusPort int `mapstructure:"prometheus_port"`
PrometheusPath string `mapstructure:"prometheus_path"`
PrometheusRuntimeMetrics bool `mapstructure:"prometheus_runtime_metrics"`
PrometheusHTTPAuthBasic string `mapstructure:"prometheus_http_auth_basic"`
PrometheusTLS PrometheusTLS `mapstructure:"prometheus_tls" validate:"omitempty"`
OTelEnabled bool `mapstructure:"otel_enabled"`
OTelTracesEnabled bool `mapstructure:"otel_traces_enabled"`
OTelMetricsEnabled bool `mapstructure:"otel_metrics_enabled"`
OTelServiceName string `mapstructure:"otel_service_name"`
OTelServiceVersion string `mapstructure:"otel_service_version"`
OTLPEndpoint string `mapstructure:"otel_exporter_otlp_endpoint"`
OTLPHeaders map[string]string `mapstructure:"otel_exporter_otlp_headers"`
OTLPInsecure bool `mapstructure:"otel_exporter_otlp_insecure"`
OTelSampleRatio *float64 `mapstructure:"otel_sample_ratio"`
}
// PrometheusTLS configures server-side TLS for the optional Prometheus endpoint.
type PrometheusTLS struct {
Enabled bool `mapstructure:"enabled"`
Cert string `mapstructure:"cert" validate:"omitempty,file"`
Key string `mapstructure:"key" validate:"omitempty,file"`
MinVersion string `mapstructure:"min_tls_version" validate:"omitempty,oneof=1.2 1.3"`
}
// Observability owns Prometheus collectors and OpenTelemetry providers for the process.
type Observability struct {
config ObservabilityConfig
logger *slog.Logger
registry *prometheus.Registry
metrics *prometheusMetrics
tracer trace.Tracer
meter metric.Meter
traceProvider *sdktrace.TracerProvider
meterProvider *sdkmetric.MeterProvider
otelMetrics *otelMetricInstruments
prometheusServer *http.Server
prometheusListener net.Listener
}
// prometheusMetrics groups all Prometheus collectors owned by this process.
type prometheusMetrics struct {
listenerConnections *prometheus.CounterVec
listenerActive *prometheus.GaugeVec
listenerDuration *prometheus.HistogramVec
listenerState *prometheus.GaugeVec
listenerDrains *prometheus.CounterVec
requestState *prometheus.GaugeVec
requestAdmissions *prometheus.CounterVec
requestQueueWait *prometheus.HistogramVec
protocolFailures *prometheus.CounterVec
applicationRequests *prometheus.CounterVec
applicationDuration *prometheus.HistogramVec
backendHTTPRequests *prometheus.CounterVec
backendHTTPDuration *prometheus.HistogramVec
backendGRPCRequests *prometheus.CounterVec
backendGRPCDuration *prometheus.HistogramVec
observabilityStartups *prometheus.CounterVec
observabilityShutdowns *prometheus.CounterVec
}
// otelMetricInstruments mirrors key Prometheus signals to an OTLP metrics exporter.
type otelMetricInstruments struct {
listenerConnections metric.Int64Counter
listenerActive metric.Int64UpDownCounter
listenerDuration metric.Float64Histogram
listenerState metric.Int64UpDownCounter
listenerDrains metric.Int64Counter
requestState metric.Int64UpDownCounter
requestAdmissions metric.Int64Counter
requestQueueWait metric.Float64Histogram
protocolFailures metric.Int64Counter
applicationRequests metric.Int64Counter
applicationDuration metric.Float64Histogram
backendHTTPRequests metric.Int64Counter
backendHTTPDuration metric.Float64Histogram
backendGRPCRequests metric.Int64Counter
backendGRPCDuration metric.Float64Histogram
}
type prometheusCounterDuration struct {
counter *prometheus.CounterVec
duration *prometheus.HistogramVec
}
type otelCounterDuration struct {
counter metric.Int64Counter
duration metric.Float64Histogram
}
type backendOperation struct {
component string
name string
}
// NewObservability initializes enabled Prometheus and OpenTelemetry components.
func NewObservability(ctx context.Context, cfg ObservabilityConfig, serviceVersion string, logger *slog.Logger) (*Observability, error) {
cfg = normalizeObservabilityConfig(cfg, serviceVersion)
if logger == nil {
logger = slog.Default()
}
obs := &Observability{
config: cfg,
logger: logger,
tracer: otel.Tracer(instrumentationName),
meter: otel.Meter(instrumentationName),
}
otel.SetTextMapPropagator(propagation.TraceContext{})
if cfg.PrometheusEnabled {
obs.registry = prometheus.NewRegistry()
obs.metrics = newPrometheusMetrics(obs.registry, cfg.PrometheusRuntimeMetrics)
}
if cfg.OTelEnabled {
if err := obs.initializeOpenTelemetry(ctx); err != nil {
return nil, err
}
}
return obs, nil
}
// newTraceTestObservabilityWithProvider builds a test runtime around a supplied tracer provider.
func newTraceTestObservabilityWithProvider(provider *sdktrace.TracerProvider) *Observability {
otel.SetTextMapPropagator(propagation.TraceContext{})
return &Observability{
config: ObservabilityConfig{
OTelEnabled: true,
OTelTracesEnabled: true,
},
tracer: provider.Tracer(instrumentationName),
traceProvider: provider,
}
}
// normalizeObservabilityConfig applies runtime defaults that cannot be represented through zero values.
func normalizeObservabilityConfig(cfg ObservabilityConfig, serviceVersion string) ObservabilityConfig {
if cfg.PrometheusAddress == "" {
cfg.PrometheusAddress = defaultPrometheusAddress
}
if cfg.PrometheusPort == 0 {
cfg.PrometheusPort = defaultPrometheusPort
}
if cfg.PrometheusPath == "" {
cfg.PrometheusPath = defaultPrometheusPath
}
if !strings.HasPrefix(cfg.PrometheusPath, "/") {
cfg.PrometheusPath = "/" + cfg.PrometheusPath
}
if cfg.OTelServiceName == "" {
cfg.OTelServiceName = defaultOTelServiceName
}
if cfg.OTelServiceVersion == "" {
cfg.OTelServiceVersion = serviceVersion
}
if cfg.OTelSampleRatio == nil {
defaultRatio := defaultOTelSampleRatio
cfg.OTelSampleRatio = &defaultRatio
}
if cfg.OTLPHeaders == nil {
cfg.OTLPHeaders = make(map[string]string)
}
return cfg
}
// defaultedOTelSampleRatio returns the configured ratio after applying the runtime default.
func defaultedOTelSampleRatio(cfg ObservabilityConfig) float64 {
if cfg.OTelSampleRatio == nil {
return defaultOTelSampleRatio
}
return *cfg.OTelSampleRatio
}
// initializeOpenTelemetry configures OTLP HTTP tracing and metrics exporters.
func (o *Observability) initializeOpenTelemetry(ctx context.Context) error {
res, err := resource.New(ctx,
resource.WithAttributes(
attribute.String("service.name", o.config.OTelServiceName),
attribute.String("service.version", o.config.OTelServiceVersion),
),
)
if err != nil {
return fmt.Errorf("creating OpenTelemetry resource: %w", err)
}
if o.config.OTelTracesEnabled {
if err = o.initializeOpenTelemetryTracing(ctx, res); err != nil {
return err
}
}
if o.config.OTelMetricsEnabled {
if err = o.initializeOpenTelemetryMetrics(ctx, res); err != nil {
return err
}
}
return nil
}
// initializeOpenTelemetryTracing starts a batch trace provider backed by OTLP HTTP.
func (o *Observability) initializeOpenTelemetryTracing(ctx context.Context, res *resource.Resource) error {
exporter, err := otlptracehttp.New(ctx, traceHTTPOptions(o.config)...)
if err != nil {
return fmt.Errorf("creating OTLP trace exporter: %w", err)
}
sampler := sdktrace.ParentBased(sdktrace.TraceIDRatioBased(defaultedOTelSampleRatio(o.config)))
o.traceProvider = sdktrace.NewTracerProvider(
sdktrace.WithResource(res),
sdktrace.WithSampler(sampler),
sdktrace.WithBatcher(exporter),
)
o.tracer = o.traceProvider.Tracer(instrumentationName)
otel.SetTracerProvider(o.traceProvider)
return nil
}
// initializeOpenTelemetryMetrics starts a periodic OTLP HTTP metric reader.
func (o *Observability) initializeOpenTelemetryMetrics(ctx context.Context, res *resource.Resource) error {
exporter, err := otlpmetrichttp.New(ctx, metricHTTPOptions(o.config)...)
if err != nil {
return fmt.Errorf("creating OTLP metric exporter: %w", err)
}
o.meterProvider = sdkmetric.NewMeterProvider(
sdkmetric.WithResource(res),
sdkmetric.WithReader(sdkmetric.NewPeriodicReader(exporter)),
)
o.meter = o.meterProvider.Meter(instrumentationName)
otel.SetMeterProvider(o.meterProvider)
return o.initializeOpenTelemetryInstruments()
}
// traceHTTPOptions converts local config into OTLP trace HTTP exporter options.
func traceHTTPOptions(cfg ObservabilityConfig) []otlptracehttp.Option {
endpoint, insecure := normalizedOTLPEndpoint(cfg)
options := []otlptracehttp.Option{otlptracehttp.WithEndpoint(endpoint)}
if insecure {
options = append(options, otlptracehttp.WithInsecure())
}
if len(cfg.OTLPHeaders) > 0 {
options = append(options, otlptracehttp.WithHeaders(cfg.OTLPHeaders))
}
return options
}
// metricHTTPOptions converts local config into OTLP metric HTTP exporter options.
func metricHTTPOptions(cfg ObservabilityConfig) []otlpmetrichttp.Option {
endpoint, insecure := normalizedOTLPEndpoint(cfg)
options := []otlpmetrichttp.Option{otlpmetrichttp.WithEndpoint(endpoint)}
if insecure {
options = append(options, otlpmetrichttp.WithInsecure())
}
if len(cfg.OTLPHeaders) > 0 {
options = append(options, otlpmetrichttp.WithHeaders(cfg.OTLPHeaders))
}
return options
}
// normalizedOTLPEndpoint accepts either host:port or a base URL and returns exporter host:port settings.
func normalizedOTLPEndpoint(cfg ObservabilityConfig) (string, bool) {
if parsed, err := url.Parse(cfg.OTLPEndpoint); err == nil && parsed.Host != "" {
return parsed.Host, cfg.OTLPInsecure || parsed.Scheme == "http"
}
return cfg.OTLPEndpoint, cfg.OTLPInsecure
}
// initializeOpenTelemetryInstruments creates the OTLP metric instruments.
func (o *Observability) initializeOpenTelemetryInstruments() error {
instruments := &otelMetricInstruments{}
var err error
if instruments.listenerConnections, err = o.meter.Int64Counter("pfxhttp_listener_connections"); err != nil {
return err
}
if instruments.listenerActive, err = o.meter.Int64UpDownCounter("pfxhttp_listener_active_connections"); err != nil {
return err
}
if instruments.listenerDuration, err = o.meter.Float64Histogram("pfxhttp_listener_connection_duration", metric.WithUnit("s")); err != nil {
return err
}
if instruments.listenerState, err = o.meter.Int64UpDownCounter("pfxhttp_listener_connection_state"); err != nil {
return err
}
if instruments.listenerDrains, err = o.meter.Int64Counter("pfxhttp_listener_drains"); err != nil {
return err
}
if instruments.requestState, err = o.meter.Int64UpDownCounter("pfxhttp_listener_request_state"); err != nil {
return err
}
if instruments.requestAdmissions, err = o.meter.Int64Counter("pfxhttp_listener_request_admissions"); err != nil {
return err
}
if instruments.requestQueueWait, err = o.meter.Float64Histogram("pfxhttp_listener_request_queue_wait", metric.WithUnit("s")); err != nil {
return err
}
if instruments.protocolFailures, err = o.meter.Int64Counter("pfxhttp_listener_protocol_failures"); err != nil {
return err
}
if instruments.applicationRequests, err = o.meter.Int64Counter("pfxhttp_application_requests"); err != nil {
return err
}
if instruments.applicationDuration, err = o.meter.Float64Histogram("pfxhttp_application_request_duration", metric.WithUnit("s")); err != nil {
return err
}
if instruments.backendHTTPRequests, err = o.meter.Int64Counter("pfxhttp_backend_http_requests"); err != nil {
return err
}
if instruments.backendHTTPDuration, err = o.meter.Float64Histogram("pfxhttp_backend_http_request_duration", metric.WithUnit("s")); err != nil {
return err
}
if instruments.backendGRPCRequests, err = o.meter.Int64Counter("pfxhttp_backend_grpc_requests"); err != nil {
return err
}
if instruments.backendGRPCDuration, err = o.meter.Float64Histogram("pfxhttp_backend_grpc_request_duration", metric.WithUnit("s")); err != nil {
return err
}
o.otelMetrics = instruments
return nil
}
// newPrometheusMetrics registers process, runtime, and application collectors.
func newPrometheusMetrics(registry *prometheus.Registry, runtimeMetrics bool) *prometheusMetrics {
metrics := buildPrometheusMetrics()
if runtimeMetrics {
registry.MustRegister(collectors.NewGoCollector(), collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
}
registry.MustRegister(
metrics.listenerConnections,
metrics.listenerActive,
metrics.listenerDuration,
metrics.listenerState,
metrics.listenerDrains,
metrics.requestState,
metrics.requestAdmissions,
metrics.requestQueueWait,
metrics.protocolFailures,
metrics.applicationRequests,
metrics.applicationDuration,
metrics.backendHTTPRequests,
metrics.backendHTTPDuration,
metrics.backendGRPCRequests,
metrics.backendGRPCDuration,
metrics.observabilityStartups,
metrics.observabilityShutdowns,
)
return metrics
}
// buildPrometheusMetrics creates all application collectors without registering them.
func buildPrometheusMetrics() *prometheusMetrics {
return &prometheusMetrics{
listenerConnections: newCounterVec("pfxhttp_listener_connections_total", "Total listener connection events.", labelComponent, labelName, labelEvent, labelResult),
listenerActive: newGaugeVec("pfxhttp_listener_active_connections", "Currently active listener connections.", labelComponent, labelName),
listenerDuration: newDurationVec("pfxhttp_listener_connection_duration_seconds", "Listener connection duration in seconds.", labelComponent, labelName, labelResult),
listenerState: newGaugeVec("pfxhttp_listener_connection_state", "Current listener connections by bounded protocol state.", labelComponent, labelName, labelState),
listenerDrains: newCounterVec("pfxhttp_listener_drains_total", "Listener drain outcomes.", labelComponent, labelName, labelResult),
requestState: newGaugeVec("pfxhttp_listener_request_state", "Current listener requests by scheduler state.", labelComponent, labelName, labelState),
requestAdmissions: newCounterVec("pfxhttp_listener_request_admissions_total", "Listener request admission outcomes.", labelComponent, labelName, labelOutcome),
requestQueueWait: newDurationVec("pfxhttp_listener_request_queue_wait_seconds", "Listener request queue wait in seconds.", labelComponent, labelName, labelOutcome),
protocolFailures: newCounterVec("pfxhttp_listener_protocol_failures_total", "Listener protocol failures by bounded reason.", labelComponent, labelName, labelReason),
applicationRequests: newCounterVec(
"pfxhttp_application_requests_total",
"Total application-level requests by component, name, and outcome.",
labelComponent,
labelName,
labelListener,
labelOutcome,
),
applicationDuration: newDurationVec("pfxhttp_application_request_duration_seconds", "Application-level request duration in seconds.", labelComponent, labelName, labelListener, labelOutcome),
backendHTTPRequests: newCounterVec("pfxhttp_backend_http_requests_total", "Total outgoing HTTP backend requests.", labelComponent, labelName, labelMethod, labelStatusClass, labelResult),
backendHTTPDuration: newDurationVec("pfxhttp_backend_http_request_duration_seconds", "Outgoing HTTP backend request duration in seconds.", labelComponent, labelName, labelMethod, labelStatusClass, labelResult),
backendGRPCRequests: newCounterVec("pfxhttp_backend_grpc_requests_total", "Total outgoing gRPC backend requests.", labelComponent, labelName, labelMethod, labelStatus, labelResult),
backendGRPCDuration: newDurationVec("pfxhttp_backend_grpc_request_duration_seconds", "Outgoing gRPC backend request duration in seconds.", labelComponent, labelName, labelMethod, labelStatus, labelResult),
observabilityStartups: newCounterVec("pfxhttp_observability_startups_total", "Prometheus endpoint startup attempts.", labelResult),
observabilityShutdowns: newCounterVec("pfxhttp_observability_shutdowns_total", "Observability shutdown attempts.", labelResult),
}
}
// newCounterVec builds a Prometheus counter vector with a consistent option shape.
func newCounterVec(name, help string, labels ...string) *prometheus.CounterVec {
return prometheus.NewCounterVec(prometheus.CounterOpts{Name: name, Help: help}, labels)
}
// newDurationVec builds a Prometheus duration histogram vector with default buckets.
func newDurationVec(name, help string, labels ...string) *prometheus.HistogramVec {
return prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: name,
Help: help,
Buckets: prometheus.DefBuckets,
}, labels)
}
// newGaugeVec builds a Prometheus gauge vector with a consistent option shape.
func newGaugeVec(name, help string, labels ...string) *prometheus.GaugeVec {
return prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: name, Help: help}, labels)
}
// StartPrometheusServer starts the optional Prometheus HTTP endpoint.
func (o *Observability) StartPrometheusServer() error {
if o == nil || !o.PrometheusEnabled() {
return nil
}
address := net.JoinHostPort(o.config.PrometheusAddress, fmt.Sprintf("%d", o.config.PrometheusPort))
listener, err := net.Listen("tcp", address)
if err != nil {
o.observeObservabilityStartup(resultError)
return fmt.Errorf("listen prometheus endpoint %s: %w", address, err)
}
mux := http.NewServeMux()
mux.Handle(o.PrometheusPath(), o.PrometheusHandler())
tlsConfig, err := buildPrometheusServerTLSConfig(o.config.PrometheusTLS)
if err != nil {
_ = listener.Close()
o.observeObservabilityStartup(resultError)
return err
}
server := &http.Server{
Addr: address,
Handler: mux,
ReadHeaderTimeout: 5 * time.Second,
TLSConfig: tlsConfig,
}
o.prometheusListener = listener
o.prometheusServer = server
o.observeObservabilityStartup(resultOK)
go func() {
var serveErr error
if tlsConfig != nil {
serveErr = server.ServeTLS(listener, "", "")
} else {
serveErr = server.Serve(listener)
}
if serveErr != nil && !errors.Is(serveErr, http.ErrServerClosed) {
o.logger.Error("Prometheus endpoint stopped", slog.String("reason", boundedErrorKind(serveErr)))
}
}()
o.logger.Info(
"Prometheus metrics endpoint started",
slog.String("address", address),
slog.String("path", o.PrometheusPath()),
slog.Bool("tls", tlsConfig != nil),
slog.Bool("basic_auth", o.config.PrometheusHTTPAuthBasic != ""),
)
return nil
}
// PrometheusHandler returns the HTTP handler for this runtime's Prometheus registry.
func (o *Observability) PrometheusHandler() http.Handler {
if o == nil || o.registry == nil {
return http.NotFoundHandler()
}
handler := promhttp.HandlerFor(o.registry, promhttp.HandlerOpts{})
if o.config.PrometheusHTTPAuthBasic == "" {
return handler
}
username, password, err := splitBasicAuthCredentials(o.config.PrometheusHTTPAuthBasic)
if err != nil {
return http.HandlerFunc(func(responseWriter http.ResponseWriter, _ *http.Request) {
http.Error(responseWriter, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
})
}
return requireHTTPBasicAuth(username, password, handler)
}
// buildPrometheusServerTLSConfig creates the server-side TLS settings for the scrape endpoint.
func buildPrometheusServerTLSConfig(cfg PrometheusTLS) (*tls.Config, error) {
if !cfg.Enabled {
return nil, nil
}
if cfg.Cert == "" || cfg.Key == "" {
return nil, errors.New("observability prometheus_tls requires cert and key when enabled")
}
minVersion, err := resolveTLSMinVersion(cfg.MinVersion)
if err != nil {
return nil, fmt.Errorf("observability prometheus_tls: %w", err)
}
cert, err := tls.LoadX509KeyPair(cfg.Cert, cfg.Key)
if err != nil {
return nil, fmt.Errorf("observability prometheus_tls load cert/key: %w", err)
}
return &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: minVersion,
}, nil
}
// requireHTTPBasicAuth protects an HTTP handler with constant-time Basic auth checks.
func requireHTTPBasicAuth(username, password string, next http.Handler) http.Handler {
return http.HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) {
gotUser, gotPassword, ok := request.BasicAuth()
if !ok || !secureStringEqual(gotUser, username) || !secureStringEqual(gotPassword, password) {
responseWriter.Header().Set("WWW-Authenticate", `Basic realm="pfxhttp metrics"`)
http.Error(responseWriter, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
next.ServeHTTP(responseWriter, request)
})
}
// secureStringEqual compares strings without leaking useful timing information about their contents.
func secureStringEqual(got, want string) bool {
gotHash := sha256.Sum256([]byte(got))
wantHash := sha256.Sum256([]byte(want))
return subtle.ConstantTimeCompare(gotHash[:], wantHash[:]) == 1
}
// PrometheusEnabled reports whether the Prometheus endpoint should be started.
func (o *Observability) PrometheusEnabled() bool {
return o != nil && o.config.PrometheusEnabled && o.registry != nil
}
// PrometheusPath returns the configured metrics path with its runtime default applied.
func (o *Observability) PrometheusPath() string {
if o == nil || o.config.PrometheusPath == "" {
return defaultPrometheusPath
}
return o.config.PrometheusPath
}
// Shutdown flushes OpenTelemetry providers and stops the optional Prometheus endpoint.
func (o *Observability) Shutdown(ctx context.Context) error {
if o == nil {
return nil
}
var shutdownErrors []error
if o.prometheusServer != nil {
shutdownErrors = append(shutdownErrors, o.prometheusServer.Shutdown(ctx))
}
if o.meterProvider != nil {
shutdownErrors = append(shutdownErrors, o.meterProvider.Shutdown(ctx))
}
if o.traceProvider != nil {
shutdownErrors = append(shutdownErrors, o.traceProvider.Shutdown(ctx))
}
err := errors.Join(shutdownErrors...)
if err != nil {
o.observeObservabilityShutdown(resultError)
} else {
o.observeObservabilityShutdown(resultOK)
}
return err
}
// StartSpan creates an internal trace span when tracing is enabled.
func (o *Observability) StartSpan(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span) {
return o.StartSpanWithKind(ctx, name, trace.SpanKindInternal, attrs...)
}
func (o *Observability) traceSpansEnabled() bool {
return o != nil && o.config.OTelEnabled && o.config.OTelTracesEnabled && o.tracer != nil
}
// StartSpanWithKind creates a trace span with the supplied kind when tracing is enabled.
func (o *Observability) StartSpanWithKind(ctx context.Context, name string, kind trace.SpanKind, attrs ...attribute.KeyValue) (context.Context, trace.Span) {
if ctx == nil {
ctx = context.Background()
}
if !o.traceSpansEnabled() {
return ctx, trace.SpanFromContext(ctx)
}
return o.tracer.Start(ctx, name, trace.WithSpanKind(kind), trace.WithAttributes(attrs...))
}
func startInternalSpanFromContext(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, *Observability, trace.Span) {
obs := ObservabilityFromContext(ctx)
if obs == nil || !obs.traceSpansEnabled() {
return ctx, nil, nil
}
spanCtx, span := obs.StartSpan(ctx, name, attrs...)
return spanCtx, obs, span
}
func finishObservedSpan(obs *Observability, span trace.Span, err error) {
if span == nil {
return
}
if err != nil && obs != nil {
obs.RecordSpanError(span, err)
}
span.End()
}
func setSpanAttributes(span trace.Span, attrs ...attribute.KeyValue) {
if span == nil {
return
}
span.SetAttributes(attrs...)
}
// RecordSpanError annotates a span with an error and marks it failed.
func (o *Observability) RecordSpanError(span trace.Span, err error) {
if span == nil || err == nil {
return
}
errorType := boundedErrorKind(err)
span.SetAttributes(attribute.String("error.type", errorType))
span.SetStatus(codes.Error, errorType)
}
func boundedErrorKind(err error) string {
if err == nil {
return resultOK
}
if kind, ok := protocolErrorKindOf(err); ok {
return string(kind)
}
switch {
case errors.Is(err, context.DeadlineExceeded):
return "deadline_exceeded"
case errors.Is(err, context.Canceled):
return "canceled"
}
if grpcStatus, ok := status.FromError(err); ok {
return "grpc_" + strings.ToLower(grpcStatus.Code().String())
}
if netErr, ok := errors.AsType[net.Error](err); ok && netErr.Timeout() {
return "network_timeout"
}
return "internal_error"
}
// InstrumentHTTPClient wraps a client transport with outbound HTTP metrics, spans, and trace propagation.
func (o *Observability) InstrumentHTTPClient(client *http.Client) {
if o == nil || client == nil {
return
}
if _, ok := client.Transport.(*observabilityRoundTripper); ok {
return
}
if client.Transport == nil {
client.Transport = http.DefaultTransport
}
client.Transport = &observabilityRoundTripper{base: client.Transport, obs: o}
}
// ObserveListenerConnection records listener connection events and active connection deltas.
func (o *Observability) ObserveListenerConnection(ctx context.Context, component, name, event, result string, activeDelta int64) {
if o == nil {
return
}
if o.metrics != nil {
o.metrics.listenerConnections.WithLabelValues(component, name, event, result).Inc()
if activeDelta != 0 {
o.metrics.listenerActive.WithLabelValues(component, name).Add(float64(activeDelta))
}
}
if o.otelMetrics != nil {
attrs := []attribute.KeyValue{
attribute.String(labelComponent, component),
attribute.String(labelName, name),
attribute.String(labelEvent, event),
attribute.String(labelResult, result),
}
o.otelMetrics.listenerConnections.Add(ctx, 1, metric.WithAttributes(attrs...))
if activeDelta != 0 {
o.otelMetrics.listenerActive.Add(ctx, activeDelta, metric.WithAttributes(attribute.String(labelComponent, component), attribute.String(labelName, name)))
}
}
}
// ObserveListenerDuration records how long a connection remained active.
func (o *Observability) ObserveListenerDuration(ctx context.Context, component, name, result string, duration time.Duration) {
if o == nil {
return
}
if o.metrics != nil {
o.metrics.listenerDuration.WithLabelValues(component, name, result).Observe(duration.Seconds())
}
if o.otelMetrics != nil {
o.otelMetrics.listenerDuration.Record(ctx, duration.Seconds(), metric.WithAttributes(
attribute.String(labelComponent, component),
attribute.String(labelName, name),
attribute.String(labelResult, result),
))
}
}
func (o *Observability) ObserveListenerState(ctx context.Context, component, name, state string, delta int64) {
if o == nil || delta == 0 {
return
}
if o.metrics != nil {
o.metrics.listenerState.WithLabelValues(component, name, state).Add(float64(delta))
}
if o.otelMetrics != nil {
o.otelMetrics.listenerState.Add(ctx, delta, metric.WithAttributes(
attribute.String(labelComponent, component),
attribute.String(labelName, name),
attribute.String(labelState, state),
))
}
}
func (o *Observability) ObserveListenerDrain(ctx context.Context, component, name, result string) {
if o == nil {
return
}
if o.metrics != nil {
o.metrics.listenerDrains.WithLabelValues(component, name, result).Inc()
}
if o.otelMetrics != nil {
o.otelMetrics.listenerDrains.Add(ctx, 1, metric.WithAttributes(
attribute.String(labelComponent, component),
attribute.String(labelName, name),
attribute.String(labelResult, result),
))
}
}
func (o *Observability) ObserveRequestLimiter(ctx context.Context, component, name string, event requestLimiterEvent) {
if o == nil {
return
}
if o.metrics != nil {
if event.inFlightDelta != 0 {
o.metrics.requestState.WithLabelValues(component, name, "inflight").Add(float64(event.inFlightDelta))
}
if event.queuedDelta != 0 {
o.metrics.requestState.WithLabelValues(component, name, "queued").Add(float64(event.queuedDelta))
}
if event.outcome != "" {
o.metrics.requestAdmissions.WithLabelValues(component, name, string(event.outcome)).Inc()
if event.queueWait > 0 {
o.metrics.requestQueueWait.WithLabelValues(component, name, string(event.outcome)).Observe(event.queueWait.Seconds())
}
}
}
if o.otelMetrics != nil {
base := []attribute.KeyValue{attribute.String(labelComponent, component), attribute.String(labelName, name)}
if event.inFlightDelta != 0 {
o.otelMetrics.requestState.Add(ctx, int64(event.inFlightDelta), metric.WithAttributes(append(base, attribute.String(labelState, "inflight"))...))
}
if event.queuedDelta != 0 {
o.otelMetrics.requestState.Add(ctx, int64(event.queuedDelta), metric.WithAttributes(append(base, attribute.String(labelState, "queued"))...))
}
if event.outcome != "" {
attrs := append(base, attribute.String(labelOutcome, string(event.outcome)))
o.otelMetrics.requestAdmissions.Add(ctx, 1, metric.WithAttributes(attrs...))
if event.queueWait > 0 {
o.otelMetrics.requestQueueWait.Record(ctx, event.queueWait.Seconds(), metric.WithAttributes(attrs...))
}
}
}
}
func (o *Observability) ObserveProtocolFailure(ctx context.Context, component, name string, reason protocolErrorKind) {
if o == nil {
return
}
if o.metrics != nil {
o.metrics.protocolFailures.WithLabelValues(component, name, string(reason)).Inc()
}
if o.otelMetrics != nil {
o.otelMetrics.protocolFailures.Add(ctx, 1, metric.WithAttributes(
attribute.String(labelComponent, component),
attribute.String(labelName, name),
attribute.String(labelReason, string(reason)),
))
}
}
// ObserveApplicationRequest records one protocol-level application request outcome and duration.
func (o *Observability) ObserveApplicationRequest(ctx context.Context, component, name, listener, outcome string, duration time.Duration) {
attrs := []attribute.KeyValue{
attribute.String(labelComponent, component),
attribute.String(labelName, name),
attribute.String(labelListener, listener),
attribute.String(labelOutcome, outcome),
}
o.observeCounterDuration(
ctx,
o.applicationPrometheusPair(),
o.applicationOTelPair(),
[]string{component, name, listener, outcome},
attrs,
duration,
)
}
// ObserveBackendHTTPRequest records one outgoing HTTP backend request.