Skip to content

Commit 7384093

Browse files
authored
✨ feat(observability): add configurable Prometheus metrics endpoint, keep default enabled (#740)
Signed-off-by: samzong <[email protected]>
1 parent 8aaf62a commit 7384093

File tree

5 files changed

+83
-9
lines changed

5 files changed

+83
-9
lines changed

config/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ embedding_models:
506506

507507
# Observability Configuration
508508
observability:
509+
metrics:
510+
enabled: true # Set to false to disable the Prometheus /metrics endpoint
509511
tracing:
510512
enabled: true # Enable distributed tracing for docker-compose stack
511513
provider: "opentelemetry" # Provider: opentelemetry, openinference, openllmetry

config/observability/config.tracing.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ api:
123123
enabled: true
124124

125125
observability:
126+
metrics:
127+
enabled: true # Set to false to disable the Prometheus /metrics endpoint
126128
tracing:
127129
enabled: true
128130
provider: "opentelemetry"

src/semantic-router/cmd/main.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,26 @@ func main() {
102102
os.Exit(0)
103103
}()
104104

105-
// Start metrics server
106-
go func() {
107-
http.Handle("/metrics", promhttp.Handler())
108-
metricsAddr := fmt.Sprintf(":%d", *metricsPort)
109-
logging.Infof("Starting metrics server on %s", metricsAddr)
110-
if metricsErr := http.ListenAndServe(metricsAddr, nil); metricsErr != nil {
111-
logging.Errorf("Metrics server error: %v", metricsErr)
112-
}
113-
}()
105+
// Start metrics server if enabled
106+
metricsEnabled := true
107+
if cfg.Observability.Metrics.Enabled != nil {
108+
metricsEnabled = *cfg.Observability.Metrics.Enabled
109+
}
110+
if *metricsPort <= 0 {
111+
metricsEnabled = false
112+
}
113+
if metricsEnabled {
114+
go func() {
115+
http.Handle("/metrics", promhttp.Handler())
116+
metricsAddr := fmt.Sprintf(":%d", *metricsPort)
117+
logging.Infof("Starting metrics server on %s", metricsAddr)
118+
if metricsErr := http.ListenAndServe(metricsAddr, nil); metricsErr != nil {
119+
logging.Errorf("Metrics server error: %v", metricsErr)
120+
}
121+
}()
122+
} else {
123+
logging.Infof("Metrics server disabled")
124+
}
114125

115126
// Create and start the ExtProc server
116127
server, err := extproc.NewServer(*configPath, *port, *secure, *certPath)

src/semantic-router/pkg/config/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,15 @@ type APIConfig struct {
262262
type ObservabilityConfig struct {
263263
// Tracing configuration for distributed tracing
264264
Tracing TracingConfig `yaml:"tracing"`
265+
// Metrics configuration for Prometheus metrics endpoint
266+
Metrics MetricsConfig `yaml:"metrics"`
267+
}
268+
269+
// MetricsConfig represents configuration for metrics endpoint
270+
type MetricsConfig struct {
271+
// Enabled controls whether the Prometheus metrics endpoint is served
272+
// When omitted, defaults to true
273+
Enabled *bool `yaml:"enabled,omitempty"`
265274
}
266275

267276
// TracingConfig represents configuration for distributed tracing

src/semantic-router/pkg/config/config_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,56 @@ tools:
206206
})
207207
})
208208

209+
Context("with observability metrics configuration", func() {
210+
It("should default to enabled when metrics block is omitted", func() {
211+
configContent := `
212+
observability:
213+
tracing:
214+
enabled: false
215+
`
216+
err := os.WriteFile(configFile, []byte(configContent), 0o644)
217+
Expect(err).NotTo(HaveOccurred())
218+
219+
cfg, err := Load(configFile)
220+
Expect(err).NotTo(HaveOccurred())
221+
Expect(cfg.Observability.Metrics.Enabled).To(BeNil())
222+
})
223+
224+
It("should honor explicit metrics disable flag", func() {
225+
configContent := `
226+
observability:
227+
metrics:
228+
enabled: false
229+
tracing:
230+
enabled: false
231+
`
232+
err := os.WriteFile(configFile, []byte(configContent), 0o644)
233+
Expect(err).NotTo(HaveOccurred())
234+
235+
cfg, err := Load(configFile)
236+
Expect(err).NotTo(HaveOccurred())
237+
Expect(cfg.Observability.Metrics.Enabled).NotTo(BeNil())
238+
Expect(*cfg.Observability.Metrics.Enabled).To(BeFalse())
239+
})
240+
241+
It("should honor explicit metrics enable flag", func() {
242+
configContent := `
243+
observability:
244+
metrics:
245+
enabled: true
246+
tracing:
247+
enabled: false
248+
`
249+
err := os.WriteFile(configFile, []byte(configContent), 0o644)
250+
Expect(err).NotTo(HaveOccurred())
251+
252+
cfg, err := Load(configFile)
253+
Expect(err).NotTo(HaveOccurred())
254+
Expect(cfg.Observability.Metrics.Enabled).NotTo(BeNil())
255+
Expect(*cfg.Observability.Metrics.Enabled).To(BeTrue())
256+
})
257+
})
258+
209259
Context("with invalid YAML syntax", func() {
210260
BeforeEach(func() {
211261
invalidYAML := `

0 commit comments

Comments
 (0)