Skip to content

Commit

Permalink
Merge pull request #1 from Trendyol/endpoint-call-count-metrics
Browse files Browse the repository at this point in the history
metrics for endpoint call counts
  • Loading branch information
ahmetamasyali authored Feb 15, 2021
2 parents 598945a + 12eb302 commit 98d68a4
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
Expand Down Expand Up @@ -64,6 +65,12 @@ type Runner struct {
Service map[string]*Service `json:"service"`
Scenario map[string]*Scenario `json:"scenario"`
servers []*fasthttp.Server
Metrics Metrics
}

type Metrics struct {
endpointCallCounts map[string]int
sync.Mutex
}

type Service struct {
Expand Down Expand Up @@ -114,10 +121,12 @@ type Scenario struct {

type Method struct {
Scenario
runner *Runner
}

func New(path string) (*Runner, error) {
runner := &Runner{}
runner.Metrics.endpointCallCounts = map[string]int{}

file, err := ioutil.ReadFile(path)

Expand Down Expand Up @@ -210,12 +219,13 @@ func (g *Runner) runToService(service *Service, name string) bool {

if scenario, ok := g.Scenario[value.Scenario]; ok {

method := Method{*scenario}
method := Method{*scenario, g}

r.Handle(value.Method, path, method.Handler())
}

}
}
r.Handle(fasthttp.MethodGet, "/metrics", g.metricsHandler())

server := &fasthttp.Server{
Name: fmt.Sprint(service.Port),
Expand Down Expand Up @@ -311,6 +321,21 @@ func (g *Runner) ErrorHandler(ctx *fasthttp.RequestCtx, cause error) {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
}

func (g *Runner) metricsHandler() fasthttp.RequestHandler {

return func(ctx *fasthttp.RequestCtx) {
resp, err := json.Marshal(g.Metrics.endpointCallCounts)

if err == nil {
ctx.SetBody(resp)
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetContentType(runtime.ContentTypeJSON)
} else {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
}
}
}

func (m *Method) Handler() fasthttp.RequestHandler {

cnt := 0
Expand All @@ -323,6 +348,7 @@ func (m *Method) Handler() fasthttp.RequestHandler {
elapsed := time.Since(start) / time.Millisecond
logger.Info(fmt.Sprintf("[%d] Host: %s | Path: %s | Executed: %s | Elapsed time: %dms", cnt, string(ctx.Host()), string(ctx.Request.URI().Path()), name, elapsed))
cnt++
go m.runner.Metrics.incrementEndpointCallCount(string(ctx.Request.Header.Method()), string(ctx.Request.URI().Path()))
}(m.Name)

action, done := m.Execute()
Expand Down Expand Up @@ -455,3 +481,29 @@ func (a *Action) Execute(ctx *fasthttp.RequestCtx) error {

return nil
}


func (metrics *Metrics) incrementEndpointCallCount(httpMethod, url string){

if httpMethod == "" || url == "" {
return
}

metrics.Lock()
defer metrics.Unlock()

key := endpointCallCountKey(httpMethod, url)

metrics.endpointCallCounts[key]++
}

func (metrics *Metrics) GetEndpointCallCount(httpMethod, url string) int{
metrics.Lock()
defer metrics.Unlock()

return metrics.endpointCallCounts[endpointCallCountKey(httpMethod, url)]
}

func endpointCallCountKey(httpMethod, url string) string{
return fmt.Sprintf("%s-%s", httpMethod, url)
}

0 comments on commit 98d68a4

Please sign in to comment.