Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
feat(metrics): initial errcode metric
Browse files Browse the repository at this point in the history
Adds an errcode metric to track the number of errcodes generated by
OSM.

Signed-off-by: jaellio <[email protected]>
  • Loading branch information
jaellio committed Aug 4, 2021
1 parent d796f9a commit 87b65f4
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/osm-controller/osm-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ func startMetricsStore() {
metricsstore.DefaultMetricsStore.ProxyBroadcastEventCount,
metricsstore.DefaultMetricsStore.CertIssuedCount,
metricsstore.DefaultMetricsStore.CertIssuedTime,
metricsstore.DefaultMetricsStore.ErrCodeCounter,
)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/catalog/egress.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (mc *MeshCatalog) GetEgressTrafficPolicy(serviceIdentity identity.ServiceId
// Deduplicate the list of TrafficMatch objects
trafficMatches, err = trafficpolicy.DeduplicateTrafficMatches(trafficMatches)
if err != nil {
log.Error().Err(err).Str(errcode.Kind, errcode.ErrDedupEgressTrafficMatches.String()).
log.Error().Err(err).Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrDedupEgressTrafficMatches)).
Msgf("Error deduplicating egress traffic matches for service identity %s", serviceIdentity)
return nil, err
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/errcode/errcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"

"github.com/pkg/errors"

"github.com/openservicemesh/osm/pkg/metricsstore"
)

// ErrCode defines the type to represent error codes
Expand Down Expand Up @@ -373,6 +375,13 @@ func (e ErrCode) String() string {
return fmt.Sprintf("E%d", e)
}

// GetErrCodeWithMetric increments the ErrCodeCounter metric for the given error code
// Returns the error code as a string
func GetErrCodeWithMetric(e ErrCode) string {
metricsstore.DefaultMetricsStore.ErrCodeCounter.WithLabelValues(e.String()).Inc()
return e.String()
}

// FromStr returns the ErrCode representation for the given error code string
// Ex. E1000 is converted to ErrInvalidCLIArgument
func FromStr(e string) (ErrCode, error) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/errcode/errcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ func TestString(t *testing.T) {
assert.Equal(ErrInvalidCLIArgument.String(), "E1000")
}

func TestGetErrorCodeWithMetric(t *testing.T) {
assert := tassert.New(t)
assert.Equal(GetErrCodeWithMetric(ErrInvalidCLIArgument), "E1000")
}

func TestFromStr(t *testing.T) {
testCases := []struct {
name string
Expand Down
19 changes: 19 additions & 0 deletions pkg/metricsstore/metricsstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ type MetricsStore struct {
// CertXdsIssuedCounter the histogram to track the time to issue a certificates
CertIssuedTime *prometheus.HistogramVec

/*
* ErrCode metrics
*/
// ErrCodeCounter is the metric counter for the number of errcodes generated by OSM
ErrCodeCounter *prometheus.CounterVec

/*
* MetricsStore internals should be defined below --------------
*/
Expand Down Expand Up @@ -158,6 +164,19 @@ func init() {
Help: "Histogram to track time spent to issue xds certificate",
},
[]string{})

/*
* ErrCode metrics
*/
defaultMetricsStore.ErrCodeCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metricsRootNamespace,
Subsystem: "error",
Name: "err_code_count",
Help: "Respesents the number of errcodes generated by OSM",
},
[]string{"err_code"},
)
defaultMetricsStore.registry = prometheus.NewRegistry()
}

Expand Down
28 changes: 28 additions & 0 deletions pkg/metricsstore/metricsstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ func setup() {
DefaultMetricsStore.Start(
DefaultMetricsStore.K8sAPIEventCounter,
DefaultMetricsStore.ProxyConnectCount,
DefaultMetricsStore.ErrCodeCounter,
)
}

func teardown() {
DefaultMetricsStore.Stop(
DefaultMetricsStore.K8sAPIEventCounter,
DefaultMetricsStore.ProxyConnectCount,
DefaultMetricsStore.ErrCodeCounter,
)
}

Expand Down Expand Up @@ -82,4 +84,30 @@ osm_proxy_connect_count %d
`, proxiesConnected-proxiesDisconnected)
assert.Contains(rr.Body.String(), expectedResp)
})

t.Run("ErrcodeCounter", func(t *testing.T) {
assert := tassert.New(t)

errcodeCount := 3

for i := 1; i <= errcodeCount; i++ {
DefaultMetricsStore.ErrCodeCounter.WithLabelValues("E1100").Inc()

handler := DefaultMetricsStore.Handler()

req, err := http.NewRequest("GET", "/metrics", nil)
assert.Nil(err)

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)

assert.Equal(http.StatusOK, rr.Code)

expectedResp := fmt.Sprintf(`# HELP osm_error_err_code_count Respesents the number of errcodes generated by OSM
# TYPE osm_error_err_code_count counter
osm_error_err_code_count{err_code="E1100"} %d
`, i /* api event count */)
assert.Contains(rr.Body.String(), expectedResp)
}
})
}

0 comments on commit 87b65f4

Please sign in to comment.