-
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.
Merge pull request #62 from tgeoghegan/prometheus
Add Prometheus metric scrapes
- Loading branch information
Showing
495 changed files
with
223,687 additions
and
30 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
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,94 @@ | ||
// Copyright (c) 2024 Cloudflare, Inc. All rights reserved. | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"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) { | ||
serveMux := http.NewServeMux() | ||
serveMux.Handle(config.ScrapePath, promhttp.Handler()) | ||
server := http.Server{ | ||
Addr: net.JoinHostPort(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}), | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.