Skip to content

Commit

Permalink
feat(fxcore): Updated core http server metrics configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox committed Mar 6, 2024
1 parent f620768 commit 81dbe98
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
7 changes: 3 additions & 4 deletions fxcore/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/labstack/echo/v4"

Expand Down Expand Up @@ -209,7 +208,7 @@ func withMiddlewares(coreServer *echo.Echo, p FxCoreParam) *echo.Echo {

var buckets []float64
if bucketsConfig := p.Config.GetString("modules.core.server.metrics.buckets"); bucketsConfig != "" {
for _, s := range strings.Split(strings.ReplaceAll(bucketsConfig, " ", ""), ",") {
for _, s := range Split(bucketsConfig) {
f, err := strconv.ParseFloat(s, 64)
if err == nil {
buckets = append(buckets, f)
Expand All @@ -219,8 +218,8 @@ func withMiddlewares(coreServer *echo.Echo, p FxCoreParam) *echo.Echo {

metricsMiddlewareConfig := httpservermiddleware.RequestMetricsMiddlewareConfig{
Registry: p.MetricsRegistry,
Namespace: strings.ReplaceAll(namespace, "-", "_"),
Subsystem: strings.ReplaceAll(subsystem, "-", "_"),
Namespace: Sanitize(namespace),
Subsystem: Sanitize(subsystem),
Buckets: buckets,
NormalizeRequestPath: p.Config.GetBool("modules.core.server.metrics.normalize.request_path"),
NormalizeResponseStatus: p.Config.GetBool("modules.core.server.metrics.normalize.response_status"),
Expand Down
16 changes: 16 additions & 0 deletions fxcore/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fxcore

import "strings"

// Sanitize transforms a given string to not contain spaces or dashes, and to be in lower case.
func Sanitize(str string) string {
san := strings.ReplaceAll(str, " ", "_")
san = strings.ReplaceAll(san, "-", "_")

return strings.ToLower(san)
}

// Split trims and splits a provided string by comma.
func Split(str string) []string {
return strings.Split(strings.ReplaceAll(str, " ", ""), ",")
}
27 changes: 27 additions & 0 deletions fxcore/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fxcore_test

import (
"testing"

"github.com/ankorstore/yokai/fxcore"
"github.com/stretchr/testify/assert"
)

func TestSanitize(t *testing.T) {
t.Parallel()

assert.Equal(t, "foo_bar", fxcore.Sanitize("foo-bar"))
assert.Equal(t, "foo_bar", fxcore.Sanitize("foo bar"))
assert.Equal(t, "foo_bar", fxcore.Sanitize("Foo-Bar"))
assert.Equal(t, "foo_bar", fxcore.Sanitize("Foo Bar"))
}

func TestSplit(t *testing.T) {
t.Parallel()

assert.Equal(t, []string{"1", "2", "3"}, fxcore.Split("1,2,3"))
assert.Equal(t, []string{"1", "2", "3"}, fxcore.Split(" 1,2,3"))
assert.Equal(t, []string{"1", "2", "3"}, fxcore.Split("1,2,3 "))
assert.Equal(t, []string{"1", "2", "3"}, fxcore.Split("1, 2, 3"))
assert.Equal(t, []string{"1", "2", "3"}, fxcore.Split(" 1, 2, 3 "))
}

0 comments on commit 81dbe98

Please sign in to comment.