-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a config option `PROMETHEUS_CONFIG` that instructs the server to expose a Prometheus metrics scrape endpoint. The value is a JSON document allowing control over the host and port the metrics handler will listen on (which may not be the same host/port as the OHTTP gateway), the path at which scrapes are served and the name of the metric. The only metric currently served is a histogram of response times. Its labeling is rather awkward, in an effort to work within the existing awkward tags on the StatsD metrics, which interpolate HTTP status into result strings.
- Loading branch information
1 parent
bcd2b43
commit 3cb2207
Showing
3 changed files
with
123 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
*.pem | ||
app-gateway-go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) 2024 Cloudflare, Inc. All rights reserved. | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
//"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
type PrometheusConfig struct { | ||
Host string | ||
Port string | ||
ScrapePath string | ||
MetricName string | ||
} | ||
|
||
type PrometheusMetrics struct { | ||
startedAt time.Time | ||
histogram prometheus.ObserverVec | ||
} | ||
|
||
func (p *PrometheusMetrics) Fire(result string) { | ||
observer := p.histogram.With(prometheus.Labels{"method": "unknown", "status": "unknown", "result": result}) | ||
p.observe(observer) | ||
} | ||
|
||
func (p *PrometheusMetrics) ResponseStatus(method string, status int) { | ||
observer := p.histogram.With(prometheus.Labels{"method": method, "status": fmt.Sprint(status), "result": "unknown"}) | ||
p.observe(observer) | ||
} | ||
|
||
func (p *PrometheusMetrics) observe(observer prometheus.Observer) { | ||
elapsed := time.Now().Sub(p.startedAt) | ||
observer.Observe(float64(elapsed.Milliseconds())) | ||
} | ||
|
||
type PrometheusMetricsFactory struct { | ||
metricName string | ||
} | ||
|
||
func NewPrometheusMetricsFactory(config PrometheusConfig) (MetricsFactory, error) { | ||
log.Printf("prometheus config: %+v\n", config) | ||
|
||
serveMux := http.NewServeMux() | ||
serveMux.Handle(config.ScrapePath, promhttp.Handler()) | ||
server := http.Server{ | ||
Addr: config.Host + ":" + config.Port, | ||
Handler: serveMux, | ||
} | ||
|
||
go func() { | ||
log.Printf("Listening for Prometheus scrapes on %s:%s\n", config.Host, config.Port) | ||
log.Fatal(server.ListenAndServe()) | ||
}() | ||
|
||
return &PrometheusMetricsFactory{metricName: config.MetricName}, nil | ||
} | ||
|
||
func (p PrometheusMetricsFactory) Create(eventName string) Metrics { | ||
histogram := prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
Name: p.metricName, | ||
}, []string{"eventName", "status", "method", "result"}) | ||
|
||
if err := prometheus.Register(histogram); err != nil { | ||
are := &prometheus.AlreadyRegisteredError{} | ||
if errors.As(err, are) { | ||
// Use previously registered metric collector | ||
histogram = are.ExistingCollector.(*prometheus.HistogramVec) | ||
} else { | ||
// There's no other reason prometheus.Register should fail and the interface won't let | ||
// us return an error. | ||
panic(err) | ||
} | ||
} | ||
|
||
return &PrometheusMetrics{ | ||
startedAt: time.Now(), | ||
histogram: histogram.MustCurryWith(prometheus.Labels{"eventName": eventName}), | ||
} | ||
} |