-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Rename confusing metric to reference_api
- Loading branch information
Showing
8 changed files
with
83 additions
and
91 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
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package metrics | ||
|
||
const ( | ||
namespace = "sl_exporter" | ||
namespace = "sl_exporter" | ||
|
||
// Subsystems | ||
staticSubsystem = "static" | ||
cosmosSubsystem = "cosmos" | ||
cosmosValSubsystem = cosmosSubsystem + "_val" | ||
refAPISubsystem = "reference_api" | ||
) |
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,35 @@ | ||
package metrics | ||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
// ReferenceAPI records metrics for external http calls. | ||
type ReferenceAPI struct { | ||
errorCounter *prometheus.CounterVec | ||
// TODO(nix): Count requests and histogram of latency. | ||
} | ||
|
||
func NewHTTPRequest() *ReferenceAPI { | ||
return &ReferenceAPI{ | ||
errorCounter: prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: prometheus.BuildFQName(namespace, refAPISubsystem, "error_total"), | ||
Help: "Number of errors encountered while making external calls to an API to gather reference data.", | ||
}, | ||
[]string{"host", "reason"}, | ||
), | ||
} | ||
} | ||
|
||
func (c ReferenceAPI) IncAPIError(host url.URL, reason string) { | ||
c.errorCounter.WithLabelValues(host.Hostname(), reason).Inc() | ||
} | ||
|
||
func (c ReferenceAPI) Metrics() []prometheus.Collector { | ||
return []prometheus.Collector{ | ||
c.errorCounter, | ||
} | ||
} |
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,29 @@ | ||
package metrics | ||
|
||
import ( | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestReferenceAPI_IncAPIError(t *testing.T) { | ||
t.Parallel() | ||
|
||
reg := prometheus.NewRegistry() | ||
metrics := NewHTTPRequest() | ||
reg.MustRegister(metrics.Metrics()[0]) | ||
|
||
u, err := url.Parse("http://test.example/should/not/be/used") | ||
require.NoError(t, err) | ||
|
||
metrics.IncAPIError(*u, "timeout") | ||
|
||
h := metricsHandler(reg) | ||
r := httptest.NewRecorder() | ||
h.ServeHTTP(r, stubRequest) | ||
|
||
require.Contains(t, r.Body.String(), `sl_exporter_reference_api_error_total{host="test.example",reason="timeout"} 1`) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.