Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#35 - Added support for stripping host from metric #39

Merged
merged 3 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type HandlerConfig struct {
Precision string
InsecureSkipVerify bool
CheckStatusMetric bool
StripHost bool
}

const (
Expand All @@ -32,6 +33,7 @@ const (
precision = "precision"
insecureSkipVerify = "insecure-skip-verify"
checkStatusMetric = "check-status-metric"
stripHost = "strip-host"
)

var (
Expand Down Expand Up @@ -103,6 +105,14 @@ var (
Usage: "if true, the check status result will be captured as a metric",
Value: &config.CheckStatusMetric,
},
{
Path: stripHost,
Argument: stripHost,
Shorthand: "",
Default: false,
Usage: "if true, we strip the host from the metric",
Value: &config.StripHost,
},
}
)

Expand Down Expand Up @@ -159,6 +169,12 @@ func sendMetrics(event *corev2.Event) error {

for _, point := range event.Metrics.Points {
var tagKey string

if config.StripHost && strings.HasPrefix(point.Name, event.Entity.Name) {
// Adding a char since we also want to strip the dot
point.Name = point.Name[len(event.Entity.Name) + 1:]
}

nameField := strings.Split(point.Name, ".")
name := nameField[0]
if len(nameField) > 1 {
Expand Down
31 changes: 31 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import (
"github.com/stretchr/testify/require"
)

var hostStrippingDataSet = []struct {
entityName string
expectedBody string
}{
{"sensu.company.com", `answer,foo=bar,sensu_entity_name=sensu.company.com value=42`},
{"answer.company.com", `answer,foo=bar,sensu_entity_name=answer.company.com value=42`},
}

func TestStatusMetrics(t *testing.T) {
assert := assert.New(t)
event := corev2.FixtureEvent("entity1", "check1")
Expand Down Expand Up @@ -53,6 +61,29 @@ func TestSendMetrics(t *testing.T) {
assert.NoError(err)
}

func TestSendMetricsHostStripping(t *testing.T) {
for _, tt := range hostStrippingDataSet {
assert := assert.New(t)
event := corev2.FixtureEvent(tt.entityName, "check1")
event.Check = nil
event.Metrics = corev2.FixtureMetrics()
event.Metrics.Points[0].Name = tt.entityName + ".answer"

var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
assert.Contains(string(body), tt.expectedBody)
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"ok": true}`))
require.NoError(t, err)
}))

config.Addr = apiStub.URL
config.StripHost = true
err := sendMetrics(event)
assert.NoError(err)
}
}

func TestSendAnnotation(t *testing.T) {
assert := assert.New(t)
event := corev2.FixtureEvent("entity1", "check1")
Expand Down