-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
221 additions
and
4 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 |
---|---|---|
|
@@ -18,6 +18,7 @@ server/src/coverage.txt | |
build | ||
server/src/main | ||
server/src/server | ||
server/src/tmp | ||
|
||
# misc | ||
.DS_Store | ||
|
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,6 @@ | ||
apiVersion: 1 | ||
datasources: | ||
- name: Main | ||
type: prometheus | ||
url: http://prometheus:9090 | ||
isDefault: true |
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,15 @@ | ||
global: | ||
scrape_interval: 15s # By default, scrape targets every 15 seconds. | ||
|
||
scrape_configs: | ||
- job_name: 'scrumlr-api' | ||
|
||
# Override the global default and scrape targets from this job every 5 seconds. | ||
scrape_interval: 5s | ||
scrape_timeout: 5s | ||
metrics_path: /metrics | ||
scheme: http | ||
follow_redirects: true | ||
|
||
static_configs: | ||
- targets: ['host.docker.internal:8080'] |
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,79 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/go-chi/chi/middleware" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"scrumlr.io/server/logger" | ||
) | ||
|
||
var ( | ||
endpointLatency = prometheus.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Name: "http_request_duration_seconds", | ||
Help: "HTTP request latency per endpoint.", | ||
Buckets: []float64{0.1, 0.2, 0.5, 1, 3, 5, 10}, | ||
}, | ||
[]string{"method", "route", "status"}, | ||
) | ||
requestCounter = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "http_request_counter", | ||
Help: "Counter of HTTP requests", | ||
}, []string{"method", "route", "status"}, | ||
) | ||
) | ||
|
||
func (s *Server) initMetrics() { | ||
log := logger.FromContext(context.Background()) | ||
err := s.customMetrics.RegisterCounterVec(requestCounter) | ||
if err != nil { | ||
log.Errorw("unable to register counter for custom metrics", err) | ||
} | ||
err = s.customMetrics.RegisterHistogramVec(endpointLatency) | ||
if err != nil { | ||
log.Errorw("unable to register histogram for custom metrics", err) | ||
} | ||
} | ||
|
||
func metricCounterMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor) | ||
defer func() { | ||
urlPath := generalizeURLPath(r.URL.Path) | ||
requestCounter.WithLabelValues(r.Method, urlPath, strconv.Itoa(ww.Status())).Inc() | ||
}() | ||
next.ServeHTTP(ww, r) | ||
|
||
}) | ||
} | ||
|
||
func metricMeasureLatencyMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
start := time.Now() | ||
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor) | ||
defer func() { | ||
duration := time.Since(start).Seconds() | ||
urlPath := generalizeURLPath(r.URL.Path) | ||
endpointLatency.WithLabelValues(r.Method, urlPath, strconv.Itoa(ww.Status())).Observe(duration) | ||
}() | ||
next.ServeHTTP(ww, r) | ||
|
||
}) | ||
} | ||
|
||
// Replaces UUID-like segments with a wildcard placeholder | ||
func generalizeURLPath(path string) string { | ||
segments := strings.Split(path, "/") | ||
for i, segment := range segments { | ||
if len(segment) == 36 && strings.Contains(segment, "-") { | ||
segments[i] = "*" | ||
} | ||
} | ||
return strings.Join(segments, "/") | ||
} |
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
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,34 @@ | ||
package custom_metrics | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/collectors" | ||
"scrumlr.io/server/services" | ||
) | ||
|
||
type CustomMetricService struct { | ||
registry prometheus.Registry | ||
} | ||
|
||
func NewCustomMetricService(enabled bool) services.CustomMetrics { | ||
if enabled { | ||
m := &CustomMetricService{} | ||
m.registry = *prometheus.NewRegistry() | ||
m.registry.Unregister(collectors.NewGoCollector()) // Unregister the default bloat of metrics | ||
return m | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (cms *CustomMetricService) RegisterHistogramVec(histo *prometheus.HistogramVec) error { | ||
return cms.registry.Register(histo) | ||
} | ||
|
||
func (cms *CustomMetricService) RegisterCounterVec(ctr *prometheus.CounterVec) error { | ||
return cms.registry.Register(ctr) | ||
} | ||
|
||
func (cms *CustomMetricService) Registry() *prometheus.Registry { | ||
return &cms.registry | ||
} |
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