Skip to content

Commit

Permalink
feat: add city in instance name
Browse files Browse the repository at this point in the history
  • Loading branch information
WangYihang committed May 7, 2024
1 parent a915dcf commit e6595c0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
25 changes: 25 additions & 0 deletions pkg/utils/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package utils

import "strings"

func Sanitize(s string) string {
builder := strings.Builder{}
for _, c := range s {
if 'a' <= c && c <= 'z' {
builder.WriteRune(c)
continue
}
if 'A' <= c && c <= 'Z' {
builder.WriteRune(c - 'A' + 'a')
continue
}
if '0' <= c && c <= '9' {
builder.WriteRune(c)
continue
}
if c == ' ' || c == '-' {
builder.WriteString("-")
}
}
return builder.String()
}
31 changes: 31 additions & 0 deletions pkg/utils/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utils_test

import (
"fmt"
"testing"

"github.com/WangYihang/gojob/pkg/utils"
)

func ExampleSanitize() {
sanitized := utils.Sanitize("New York")
fmt.Println(sanitized)
// Output: new-york
}

func TestSanitize(t *testing.T) {
testcases := []struct {
s string
want string
}{
{s: "New York", want: "new-york"},
{s: "New-York", want: "new-york"},
{s: "NewYork", want: "newyork"},
}
for _, testcase := range testcases {
got := utils.Sanitize(testcase.s)
if got != testcase.want {
t.Errorf("Sanitize(%#v), want: %#v, expected: %#v", testcase.s, testcase.want, got)
}
}
}
8 changes: 5 additions & 3 deletions prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package gojob
import (
"fmt"
"log/slog"
"strings"
"sync"

"github.com/WangYihang/gojob/pkg/runner"
"github.com/WangYihang/gojob/pkg/utils"
"github.com/WangYihang/gojob/pkg/version"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
Expand Down Expand Up @@ -71,11 +71,13 @@ func (g *customMetricsRegistry) Gather() ([]*io_prometheus_client.MetricFamily,

return metricFamilies, err
}

func prometheusPusher(url, job string, statusChan <-chan Status, wg *sync.WaitGroup) {
instance := fmt.Sprintf(
"gojob-%s-%s-%s",
"gojob-%s-%s-%s-%s",
version.Version,
strings.ToLower(runner.Runner.Country),
utils.Sanitize(runner.Runner.Country),
utils.Sanitize(runner.Runner.City),
runner.Runner.IP,
)
registry := NewRegistryWithLabels(map[string]string{
Expand Down

0 comments on commit e6595c0

Please sign in to comment.