-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…-exporter Feature/gh 255 add openmetrics exporter
- Loading branch information
Showing
19 changed files
with
885 additions
and
5 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
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,102 @@ | ||
package metrics | ||
|
||
import ( | ||
"runtime" | ||
|
||
"github.com/bsm/openmetrics" | ||
) | ||
|
||
type MetricSet struct { | ||
m runtime.MemStats | ||
// Memstats | ||
MemSysBytes openmetrics.Gauge | ||
// WebSocket connectionws | ||
WSConnections openmetrics.Gauge | ||
WSConnectionCount openmetrics.Counter | ||
// WebSocket requests | ||
WSRequestsGet openmetrics.Counter | ||
WSRequestsSubscribe openmetrics.Counter | ||
WSRequestsUnsubscribe openmetrics.Counter | ||
WSRequestsCall openmetrics.Counter | ||
WSRequestsAuth openmetrics.Counter | ||
// Cache | ||
CacheResources openmetrics.Gauge | ||
CacheSubscriptions openmetrics.Gauge | ||
// HTTP requests | ||
HTTPRequests openmetrics.CounterFamily | ||
HTTPRequestsGet openmetrics.Counter | ||
HTTPRequestsPost openmetrics.Counter | ||
} | ||
|
||
// Scrape updates the metric set with info on current mem usage. | ||
func (m *MetricSet) Scrape() { | ||
runtime.ReadMemStats(&m.m) | ||
m.MemSysBytes.Set(float64(m.m.Sys)) | ||
} | ||
|
||
func (m *MetricSet) Register(reg *openmetrics.Registry, version string, protocolVersion string) { | ||
// Go info | ||
reg.Info(openmetrics.Desc{ | ||
Name: "go", | ||
Help: "Information about the Go environment.", | ||
Labels: []string{"version"}, | ||
}).With(runtime.Version()) | ||
|
||
// Resgate info | ||
reg.Info(openmetrics.Desc{ | ||
Name: "resgate", | ||
Help: "Information about resgate.", | ||
Labels: []string{"version", "protocol"}, | ||
}).With(version, protocolVersion) | ||
|
||
// Memory stats | ||
m.MemSysBytes = reg.Gauge(openmetrics.Desc{ | ||
Name: "go_memstats_sys_bytes", | ||
Help: "Number of bytes obtained from system.", | ||
}).With() | ||
m.MemSysBytes.Set(0) | ||
|
||
// WebSocket connections | ||
m.WSConnections = reg.Gauge(openmetrics.Desc{ | ||
Name: "resgate_ws_current_connections", | ||
Help: "Current established WebSocket connections.", | ||
}).With() | ||
m.WSConnections.Set(0) | ||
m.WSConnectionCount = reg.Counter(openmetrics.Desc{ | ||
Name: "resgate_ws_connections", | ||
Help: "Total established WebSocket connections.", | ||
}).With() | ||
|
||
// WebSocket requests | ||
wsRequests := reg.Counter(openmetrics.Desc{ | ||
Name: "resgate_ws_requests", | ||
Help: "Total WebSocket client requests.", | ||
Labels: []string{"method"}, | ||
}) | ||
m.WSRequestsGet = wsRequests.With("get") | ||
m.WSRequestsSubscribe = wsRequests.With("subscribe") | ||
m.WSRequestsUnsubscribe = wsRequests.With("unsubscribe") | ||
m.WSRequestsCall = wsRequests.With("call") | ||
m.WSRequestsAuth = wsRequests.With("auth") | ||
|
||
// HTTP requests | ||
m.HTTPRequests = reg.Counter(openmetrics.Desc{ | ||
Name: "resgate_http_requests", | ||
Help: "Total HTTP client requests.", | ||
Labels: []string{"method"}, | ||
}) | ||
m.HTTPRequestsGet = m.HTTPRequests.With("GET") | ||
m.HTTPRequestsPost = m.HTTPRequests.With("POST") | ||
|
||
// Cache | ||
m.CacheResources = reg.Gauge(openmetrics.Desc{ | ||
Name: "resgate_cache_resources", | ||
Help: "Current number of resources stored in the cache.", | ||
}).With() | ||
m.CacheResources.Set(0) | ||
m.CacheSubscriptions = reg.Gauge(openmetrics.Desc{ | ||
Name: "resgate_cache_subscriptions", | ||
Help: "Current number of subscriptions on cached resources.", | ||
}).With() | ||
m.CacheSubscriptions.Set(0) | ||
} |
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,102 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/bsm/openmetrics" | ||
"github.com/bsm/openmetrics/omhttp" | ||
"github.com/resgateio/resgate/server/metrics" | ||
) | ||
|
||
const MetricsPattern = "/metrics" | ||
|
||
func (s *Service) initMetricsServer() { | ||
if s.cfg.MetricsPort == 0 { | ||
return | ||
} | ||
s.metrics = &metrics.MetricSet{} | ||
} | ||
|
||
// MetricsHandler returns any metrics HTTP handler for testing purposes. | ||
func (s *Service) MetricsHandler() http.Handler { | ||
return s.metricsh | ||
} | ||
|
||
// startMetricsServer initializes the server and starts a goroutine with a prometheus metrics server | ||
func (s *Service) startMetricsServer() { | ||
if s.cfg.MetricsPort == 0 { | ||
return | ||
} | ||
|
||
reg := openmetrics.NewConsistentRegistry(func() time.Time { return time.Now() }) | ||
ms := s.metrics | ||
ms.Register(reg, Version, ProtocolVersion) | ||
|
||
h := omhttp.NewHandler(reg) | ||
s.metricsh = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ms.Scrape() | ||
h.ServeHTTP(w, r) | ||
}) | ||
|
||
// For testing | ||
if s.cfg.NoHTTP { | ||
return | ||
} | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle(MetricsPattern, s.metricsh) | ||
|
||
hln, err := net.Listen("tcp", s.cfg.metricsNetAddr) | ||
if err != nil { | ||
s.Logf("Metrics server can't listen on %s: %s", s.cfg.metricsNetAddr, err) | ||
return | ||
} | ||
|
||
metricsServer := &http.Server{ | ||
Handler: mux, | ||
} | ||
s.m = metricsServer | ||
|
||
s.Logf("Metrics endpoint listening on %s://%s%s", s.cfg.scheme, s.cfg.metricsNetAddr, MetricsPattern) | ||
|
||
go func() { | ||
var err error | ||
if s.cfg.TLS { | ||
err = s.m.ServeTLS(hln, s.cfg.TLSCert, s.cfg.TLSKey) | ||
} else { | ||
err = s.m.Serve(hln) | ||
} | ||
|
||
if err != nil { | ||
s.Stop(err) | ||
} | ||
}() | ||
|
||
} | ||
|
||
// stopMetricsServer stops the Metrics server | ||
func (s *Service) stopMetricsServer() { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
if s.m == nil { | ||
return | ||
} | ||
|
||
s.Debugf("Stopping Metrics server...") | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
s.m.Shutdown(ctx) | ||
s.m = nil | ||
|
||
if ctx.Err() == context.DeadlineExceeded { | ||
s.Errorf("Metrics server forcefully stopped after timeout") | ||
} else { | ||
s.Debugf("Metrics server gracefully stopped") | ||
} | ||
} |
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
Oops, something went wrong.