Skip to content

Commit

Permalink
implemented prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
ps-spb committed Sep 12, 2024
1 parent 80d02d2 commit 251be90
Show file tree
Hide file tree
Showing 8 changed files with 621 additions and 7 deletions.
4 changes: 4 additions & 0 deletions cmd/moroz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/oklog/run"

"github.com/groob/moroz/logging"
"github.com/groob/moroz/metrics"
"github.com/groob/moroz/moroz"
"github.com/groob/moroz/santaconfig"
)
Expand Down Expand Up @@ -66,6 +67,9 @@ func main() {
logging.InitLogger(*flDebug, *flLogFmt)
logging.Logger.Log("msg", "Application started")

// init metrics service...
metrics.Init()

if _, err := os.Stat(*flTLSCert); *flUseTLS && os.IsNotExist(err) {
logging.Logger.Log("level", "info", "msg", openSSLBash)
logging.Logger.Log("level", "info", "msg", "you need to provide at least a 'global.yaml' configuration file in the configs folder. See the configs folder in the git repo for an example")
Expand Down
14 changes: 13 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ require (
github.com/kolide/kit v0.0.0-20240411131714-94dd1939cf50
github.com/oklog/run v1.1.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.1
gopkg.in/yaml.v2 v2.4.0
)

require github.com/go-logfmt/logfmt v0.6.0 // indirect
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.30.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
golang.org/x/sys v0.18.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
463 changes: 462 additions & 1 deletion go.sum

Large diffs are not rendered by default.

79 changes: 79 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
)

var (
// Counters for the Santa API routes
PreflightRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "moroz_santa_preflight_requests_total",
Help: "Total number of preflight requests.",
},
[]string{"method"},
)

RuleDownloadRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "moroz_santa_ruledownload_requests_total",
Help: "Total number of ruledownload requests.",
},
[]string{"method"},
)

EventUploadRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "moroz_santa_eventupload_requests_total",
Help: "Total number of eventupload requests.",
},
[]string{"method"},
)

PostflightRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "moroz_santa_postflight_requests_total",
Help: "Total number of postflight requests.",
},
[]string{"method"},
)

// Histogram for preflight request durations
PreflightRequestDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "moroz_santa_preflight_request_duration_seconds",
Help: "Duration of preflight requests in seconds.",
Buckets: prometheus.DefBuckets, // Default buckets for timing metrics
},
[]string{"status"}, // Labels: "success" or "error"
)

// Histogram for rule download request duration
RuleDownloadRequestDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "moroz_santa_ruledownload_request_duration_seconds",
Help: "Duration of ruledownload requests in seconds.",
Buckets: prometheus.DefBuckets, // Default buckets for timing metrics
},
[]string{"status"}, // Labels: "success" or "error"
)

EventUploadRequestDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "moroz_santa_eventupload_request_duration_seconds",
Help: "Duration of eventupload requests in seconds.",
Buckets: prometheus.DefBuckets, // Default buckets for timing metrics
},
[]string{"status"}, // Labels: success or error
)
)

func Init() {
// Register the counters with Prometheus
prometheus.MustRegister(PreflightRequests)
prometheus.MustRegister(RuleDownloadRequests)
prometheus.MustRegister(EventUploadRequests)
prometheus.MustRegister(PostflightRequests)
prometheus.MustRegister(RuleDownloadRequestDuration)
prometheus.MustRegister(EventUploadRequestDuration)
}
30 changes: 25 additions & 5 deletions moroz/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
httptransport "github.com/go-kit/kit/transport/http"
"github.com/go-kit/log"
"github.com/gorilla/mux"
"github.com/groob/moroz/metrics"

"github.com/prometheus/client_golang/prometheus/promhttp"
)

func AddHTTPRoutes(r *mux.Router, e Endpoints, logger log.Logger) {
Expand All @@ -25,36 +28,53 @@ func AddHTTPRoutes(r *mux.Router, e Endpoints, logger log.Logger) {
// POST /v1/santa/eventupload/:id upload event.
// POST /v1/santa/postflight/:id postflight request. Implemented as a no-op.

// Preflight Route
r.Methods("POST").Path("/v1/santa/preflight/{id}").Handler(httptransport.NewServer(
e.PreflightEndpoint,
decodePreflightRequest,
encodeResponse,
options...,
append(options, httptransport.ServerBefore(func(ctx context.Context, req *http.Request) context.Context {
metrics.PreflightRequests.WithLabelValues("POST").Inc() // Increment preflight requests counter
return ctx
}))...,
))

// Rule Download Route
r.Methods("POST").Path("/v1/santa/ruledownload/{id}").Handler(httptransport.NewServer(
e.RuleDownloadEndpoint,
decodeRuleRequest,
encodeResponse,
options...,
append(options, httptransport.ServerBefore(func(ctx context.Context, req *http.Request) context.Context {
metrics.RuleDownloadRequests.WithLabelValues("POST").Inc() // Increment rule download requests counter
return ctx
}))...,
))

// Event Upload Route
r.Methods("POST").Path("/v1/santa/eventupload/{id}").Handler(httptransport.NewServer(
e.EventUploadEndpoint,
decodeEventUpload,
encodeResponse,
options...,
append(options, httptransport.ServerBefore(func(ctx context.Context, req *http.Request) context.Context {
metrics.EventUploadRequests.WithLabelValues("POST").Inc() // Increment event upload requests counter
return ctx
}))...,
))

// Postflight Route (no-op)
r.Methods("POST").Path("/v1/santa/postflight/{id}").Handler(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {},
func(w http.ResponseWriter, r *http.Request) {
metrics.PostflightRequests.WithLabelValues("POST").Inc() // Increment postflight requests counter
},
))

// add healthz
// Health Check Route
r.Methods("GET").Path("/healthz").Handler(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {},
))

// Add Prometheus
r.Methods("GET").Path("/metrics").Handler(promhttp.Handler())
}

// errBadRoute is used for mux errors
Expand Down
14 changes: 14 additions & 0 deletions moroz/svc_preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"time"

"github.com/groob/moroz/metrics"

"github.com/go-kit/kit/endpoint"
"github.com/groob/moroz/santa"
)
Expand Down Expand Up @@ -96,6 +98,18 @@ func (mw logmw) Preflight(ctx context.Context, machineID string, p santa.Preflig
"err", err,
"took", time.Since(begin),
)

status := "success"
if err != nil {
status = "error"
}

// Increment the preflight request counter using the existing metric
metrics.PreflightRequests.WithLabelValues("POST").Inc()

// Observe the request duration using the existing time.Since(begin)
metrics.PreflightRequestDuration.WithLabelValues(status).Observe(time.Since(begin).Seconds())

}(time.Now())

pf, err = mw.next.Preflight(ctx, machineID, p)
Expand Down
12 changes: 12 additions & 0 deletions moroz/svc_rule_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/go-kit/kit/endpoint"
"github.com/groob/moroz/metrics"
"github.com/groob/moroz/santa"
)

Expand Down Expand Up @@ -63,6 +64,17 @@ func (mw logmw) RuleDownload(ctx context.Context, machineID string) (rules []san
"err", err,
"took", time.Since(begin),
)

// Determine status based on the error (nil means success)
status := "success"
if err != nil {
status = "error"
}
// Increment the rule download request counter
metrics.RuleDownloadRequests.WithLabelValues("POST").Inc()

// Observe the request duration using the existing time.Since(begin)
metrics.RuleDownloadRequestDuration.WithLabelValues(status).Observe(time.Since(begin).Seconds())
}(time.Now())

rules, err = mw.next.RuleDownload(ctx, machineID)
Expand Down
12 changes: 12 additions & 0 deletions moroz/svc_upload_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/go-kit/kit/endpoint"
"github.com/pkg/errors"

"github.com/groob/moroz/metrics"
"github.com/groob/moroz/santa"
)

Expand Down Expand Up @@ -104,6 +105,17 @@ func decodeEventUpload(ctx context.Context, r *http.Request) (interface{}, error

func (mw logmw) UploadEvent(ctx context.Context, machineID string, events []santa.EventPayload) (err error) {
defer func(begin time.Time) {
status := "success"
if err != nil {
status = "error"
}

// Increment the event upload request counter
metrics.EventUploadRequests.WithLabelValues("POST").Inc()

// Observe the request duration using time.Since(begin)
metrics.EventUploadRequestDuration.WithLabelValues(status).Observe(time.Since(begin).Seconds())

for _, ev := range events {
_ = mw.logger.Log(
"method", "UploadEvent",
Expand Down

0 comments on commit 251be90

Please sign in to comment.