-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): Prometheus support (#174)
* feat(metrics): Prometheus support * Bump Træfik * Bump caddy * Run prometheus registry during the configuration * Fix prometheus already started * Update newman tests * Update documentation according to the new souin_ prefix on the prometheus metrics * Add Skipper and Gin E2E
- Loading branch information
Showing
348 changed files
with
51,592 additions
and
4,679 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
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,110 @@ | ||
package prometheus | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/darkweak/souin/api/auth" | ||
"github.com/darkweak/souin/configurationtypes" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
const ( | ||
counter = "counter" | ||
average = "average" | ||
|
||
RequestCounter = "souin_request_counter" | ||
NoCachedResponseCounter = "souin_no_cached_response_counter" | ||
CachedResponseCounter = "souin_cached_response_counter" | ||
AvgResponseTime = "souin_avg_response_time" | ||
) | ||
|
||
// PrometheusAPI object contains informations related to the endpoints | ||
type PrometheusAPI struct { | ||
basePath string | ||
enabled bool | ||
security *auth.SecurityAPI | ||
} | ||
|
||
// InitializePrometheus initialize the prometheus endpoints | ||
func InitializePrometheus(configuration configurationtypes.AbstractConfigurationInterface, api *auth.SecurityAPI) *PrometheusAPI { | ||
basePath := configuration.GetAPI().Prometheus.BasePath | ||
enabled := configuration.GetAPI().Prometheus.Enable | ||
var security *auth.SecurityAPI | ||
if configuration.GetAPI().Souin.Security { | ||
security = api | ||
} | ||
if basePath == "" { | ||
basePath = "/metrics" | ||
} | ||
|
||
if registered == nil { | ||
run() | ||
} | ||
return &PrometheusAPI{ | ||
basePath, | ||
enabled, | ||
security, | ||
} | ||
} | ||
|
||
// GetBasePath will return the basepath for this resource | ||
func (p *PrometheusAPI) GetBasePath() string { | ||
return p.basePath | ||
} | ||
|
||
// IsEnabled will return enabled status | ||
func (p *PrometheusAPI) IsEnabled() bool { | ||
return p.enabled | ||
} | ||
|
||
// HandleRequest will handle the request | ||
func (p *PrometheusAPI) HandleRequest(w http.ResponseWriter, r *http.Request) { | ||
promhttp.Handler().ServeHTTP(w, r) | ||
} | ||
|
||
var registered map[string]interface{} | ||
|
||
// Increment will increment the counter. | ||
func Increment(name string) { | ||
registered[name].(prometheus.Counter).Inc() | ||
} | ||
|
||
// Increment will add the referred value the counter. | ||
func Add(name string, value float64) { | ||
if c, ok := registered[name].(prometheus.Counter); ok { | ||
c.Add(value) | ||
} | ||
if g, ok := registered[name].(prometheus.Histogram); ok { | ||
g.Observe(value) | ||
} | ||
} | ||
|
||
func push(promType, name, help string) { | ||
switch promType { | ||
case counter: | ||
registered[name] = promauto.NewCounter(prometheus.CounterOpts{ | ||
Name: name, | ||
Help: help, | ||
}) | ||
|
||
return | ||
case average: | ||
avg := prometheus.NewHistogram(prometheus.HistogramOpts{ | ||
Name: name, | ||
Help: help, | ||
}) | ||
prometheus.MustRegister(avg) | ||
registered[name] = avg | ||
} | ||
} | ||
|
||
// Run populate and prepare the map with the default values. | ||
func run() { | ||
registered = make(map[string]interface{}) | ||
push(counter, RequestCounter, "Total request counter") | ||
push(counter, NoCachedResponseCounter, "No cached response counter") | ||
push(counter, CachedResponseCounter, "Cached response counter") | ||
push(average, AvgResponseTime, "Average Bidswitch response time") | ||
} |
Oops, something went wrong.