Skip to content

Commit c6cad6f

Browse files
committed
add azure_devops_api_request histogram metric (request tracing)
replaces old request counter metric Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent bc2c7ce commit c6cad6f

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

azure-devops-client/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import (
44
"errors"
55
"fmt"
66
resty "github.com/go-resty/resty/v2"
7+
"github.com/prometheus/client_golang/prometheus"
8+
"net/url"
9+
"strconv"
10+
"strings"
711
"sync/atomic"
812
)
913

@@ -34,6 +38,10 @@ type AzureDevopsClient struct {
3438
LimitDeploymentPerDefinition int64
3539
LimitReleaseDefinitionsPerProject int64
3640
LimitReleasesPerProject int64
41+
42+
prometheus struct {
43+
apiRequest *prometheus.HistogramVec
44+
}
3745
}
3846

3947
func NewAzureDevopsClient() *AzureDevopsClient {
@@ -56,6 +64,17 @@ func (c *AzureDevopsClient) Init() {
5664
c.LimitDeploymentPerDefinition = 100
5765
c.LimitReleaseDefinitionsPerProject = 100
5866
c.LimitReleasesPerProject = 100
67+
68+
c.prometheus.apiRequest = prometheus.NewHistogramVec(
69+
prometheus.HistogramOpts{
70+
Name: "azure_devops_api_request",
71+
Help: "AzureDevOps API requests",
72+
Buckets: []float64{.05, .1, .25, .5, 1, 2.5, 5, 10, 30},
73+
},
74+
[]string{"endpoint", "organization", "method", "statusCode"},
75+
)
76+
77+
prometheus.MustRegister(c.prometheus.apiRequest)
5978
}
6079

6180
func (c *AzureDevopsClient) SetConcurrency(v int64) {
@@ -143,6 +162,13 @@ func (c *AzureDevopsClient) restOnBeforeRequest(client *resty.Client, request *r
143162
}
144163

145164
func (c *AzureDevopsClient) restOnAfterResponse(client *resty.Client, response *resty.Response) (err error) {
165+
requestUrl, _ := url.Parse(response.Request.URL)
166+
c.prometheus.apiRequest.With(prometheus.Labels{
167+
"endpoint": requestUrl.Hostname(),
168+
"organization": *c.organization,
169+
"method": strings.ToLower(response.Request.Method),
170+
"statusCode": strconv.FormatInt(int64(response.StatusCode()), 10),
171+
}).Observe(response.Time().Seconds())
146172
return
147173
}
148174

metrics_general.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,9 @@ func (m *MetricsCollectorGeneral) Reset() {
3636
}
3737

3838
func (m *MetricsCollectorGeneral) Collect(ctx context.Context, logger *log.Entry, callback chan<- func()) {
39-
m.collectAzureDevopsClientStats(ctx, logger, callback)
4039
m.collectCollectorStats(ctx, logger, callback)
4140
}
4241

43-
func (m *MetricsCollectorGeneral) collectAzureDevopsClientStats(ctx context.Context, logger *log.Entry, callback chan<- func()) {
44-
statsMetrics := prometheusCommon.NewMetricsList()
45-
46-
statsMetrics.Add(prometheus.Labels{
47-
"name": "dev.azure.com",
48-
"type": "requests",
49-
}, AzureDevopsClient.GetRequestCount())
50-
51-
statsMetrics.Add(prometheus.Labels{
52-
"name": "dev.azure.com",
53-
"type": "concurrency",
54-
}, AzureDevopsClient.GetCurrentConcurrency())
55-
56-
callback <- func() {
57-
statsMetrics.GaugeSet(m.prometheus.stats)
58-
}
59-
}
60-
6142
func (m *MetricsCollectorGeneral) collectCollectorStats(ctx context.Context, logger *log.Entry, callback chan<- func()) {
6243
statsMetrics := prometheusCommon.NewMetricsList()
6344

0 commit comments

Comments
 (0)