-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from AntonRutkevich/parametrize-prometheus-metric
Add ability to parametrize prometheus metric name
- Loading branch information
Showing
4 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package templatestests | ||
|
||
// DO NOT EDIT! | ||
// This code is generated with http://github.com/hexdigest/gowrap tool | ||
// using ../templates/prometheus template | ||
|
||
//go:generate gowrap gen -p github.com/hexdigest/gowrap/templates_tests -i AnotherTestInterface -t ../templates/prometheus -o interface_with_prometheus_metric_name.go -v MetricName=custom_metric_name_seconds | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
// AnotherTestInterfaceWithPrometheus implements AnotherTestInterface interface with all methods wrapped | ||
// with Prometheus metrics | ||
type AnotherTestInterfaceWithPrometheus struct { | ||
base AnotherTestInterface | ||
instanceName string | ||
} | ||
|
||
var anothertestinterfaceDurationSummaryVec = promauto.NewSummaryVec( | ||
prometheus.SummaryOpts{ | ||
Name: "custom_metric_name_seconds", | ||
Help: "anothertestinterface runtime duration and result", | ||
MaxAge: time.Minute, | ||
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, | ||
}, | ||
[]string{"instance_name", "method", "result"}) | ||
|
||
// NewAnotherTestInterfaceWithPrometheus returns an instance of the AnotherTestInterface decorated with prometheus summary metric | ||
func NewAnotherTestInterfaceWithPrometheus(base AnotherTestInterface, instanceName string) AnotherTestInterfaceWithPrometheus { | ||
return AnotherTestInterfaceWithPrometheus{ | ||
base: base, | ||
instanceName: instanceName, | ||
} | ||
} | ||
|
||
// Channels implements AnotherTestInterface | ||
func (_d AnotherTestInterfaceWithPrometheus) Channels(chA chan bool, chB chan<- bool, chanC <-chan bool) { | ||
_since := time.Now() | ||
defer func() { | ||
result := "ok" | ||
anothertestinterfaceDurationSummaryVec.WithLabelValues(_d.instanceName, "Channels", result).Observe(time.Since(_since).Seconds()) | ||
}() | ||
_d.base.Channels(chA, chB, chanC) | ||
return | ||
} | ||
|
||
// F implements AnotherTestInterface | ||
func (_d AnotherTestInterfaceWithPrometheus) F(ctx context.Context, a1 string, a2 ...string) (result1 string, result2 string, err error) { | ||
_since := time.Now() | ||
defer func() { | ||
result := "ok" | ||
if err != nil { | ||
result = "error" | ||
} | ||
|
||
anothertestinterfaceDurationSummaryVec.WithLabelValues(_d.instanceName, "F", result).Observe(time.Since(_since).Seconds()) | ||
}() | ||
return _d.base.F(ctx, a1, a2...) | ||
} | ||
|
||
// NoError implements AnotherTestInterface | ||
func (_d AnotherTestInterfaceWithPrometheus) NoError(s1 string) (s2 string) { | ||
_since := time.Now() | ||
defer func() { | ||
result := "ok" | ||
anothertestinterfaceDurationSummaryVec.WithLabelValues(_d.instanceName, "NoError", result).Observe(time.Since(_since).Seconds()) | ||
}() | ||
return _d.base.NoError(s1) | ||
} | ||
|
||
// NoParamsOrResults implements AnotherTestInterface | ||
func (_d AnotherTestInterfaceWithPrometheus) NoParamsOrResults() { | ||
_since := time.Now() | ||
defer func() { | ||
result := "ok" | ||
anothertestinterfaceDurationSummaryVec.WithLabelValues(_d.instanceName, "NoParamsOrResults", result).Observe(time.Since(_since).Seconds()) | ||
}() | ||
_d.base.NoParamsOrResults() | ||
return | ||
} |
33 changes: 33 additions & 0 deletions
33
templates_tests/interface_with_prometheus_metric_name_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package templatestests | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTestInterfaceWithPrometheusMetricName_F(t *testing.T) { | ||
t.Run("no error", func(t *testing.T) { | ||
impl := &testImpl{r1: "1", r2: "2"} | ||
|
||
wrapped := NewAnotherTestInterfaceWithPrometheus(impl, "test") | ||
r1, r2, err := wrapped.F(context.Background(), "a1", "a2") | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, "1", r1) | ||
assert.Equal(t, "2", r2) | ||
}) | ||
|
||
t.Run("with error", func(t *testing.T) { | ||
impl := &testImpl{r1: "1", r2: "2", err: errors.New("unexpected error")} | ||
|
||
wrapped := NewAnotherTestInterfaceWithPrometheus(impl, "test") | ||
r1, r2, err := wrapped.F(context.Background(), "a1", "a2") | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, "1", r1) | ||
assert.Equal(t, "2", r2) | ||
}) | ||
} |