@@ -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
3947func 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
6180func (c * AzureDevopsClient ) SetConcurrency (v int64 ) {
@@ -143,6 +162,13 @@ func (c *AzureDevopsClient) restOnBeforeRequest(client *resty.Client, request *r
143162}
144163
145164func (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
0 commit comments