Skip to content

Commit be229c3

Browse files
ScottGuymerSDxKeeper
authored andcommitted
First pass at adding queue length
1 parent daf064c commit be229c3

File tree

3 files changed

+81
-20
lines changed

3 files changed

+81
-20
lines changed

azure-devops-client/agentpool.go

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,27 @@ type AgentPoolAgent struct {
6363
Status string
6464
Version string
6565
CreatedOn time.Time
66+
AssignedRequest JobRequest
6667

67-
AssignedRequest struct {
68-
RequestId int64
69-
Demands []string
70-
QueueTime time.Time
71-
AssignTime time.Time
72-
ReceiveTime time.Time
73-
LockedUntil time.Time
74-
ServiceOwner string
75-
HostId string
76-
ScopeId string
77-
PlanType string
78-
PlanId string
79-
JobId string
80-
Definition struct {
81-
Id int64
82-
Name string
83-
Links Links `json:"_links"`
84-
}
68+
}
69+
70+
type JobRequest struct {
71+
RequestId int64
72+
Demands []string
73+
QueueTime time.Time
74+
AssignTime *time.Time
75+
ReceiveTime time.Time
76+
LockedUntil time.Time
77+
ServiceOwner string
78+
HostId string
79+
ScopeId string
80+
PlanType string
81+
PlanId string
82+
JobId string
83+
Definition struct {
84+
Id int64
85+
Name string
86+
Links Links `json:"_links"`
8587
}
8688
}
8789

@@ -107,3 +109,32 @@ func (c *AzureDevopsClient) ListAgentPoolAgents(agentPoolId int64) (list AgentPo
107109

108110
return
109111
}
112+
113+
114+
type AgentPoolJobList struct {
115+
Count int `json:"count"`
116+
List []JobRequest `json:"value"`
117+
}
118+
119+
func (c *AzureDevopsClient) ListAgentPoolJobs(agentPoolId int64) (list AgentPoolJobList, error error) {
120+
defer c.concurrencyUnlock()
121+
c.concurrencyLock()
122+
123+
url := fmt.Sprintf(
124+
"/_apis/distributedtask/pools/%v/jobrequests",
125+
fmt.Sprintf("%d", agentPoolId),
126+
)
127+
response, err := c.rest().R().Get(url)
128+
if err := c.checkResponse(response, err); err != nil {
129+
error = err
130+
return
131+
}
132+
133+
err = json.Unmarshal(response.Body(), &list)
134+
if err != nil {
135+
error = err
136+
return
137+
}
138+
139+
return
140+
}

collector_structs.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ func (m *MetricCollectorList) AddBool(labels prometheus.Labels, state bool) {
7171
m.list = append(m.list, MetricCollectorRow{labels: labels, value: value})
7272
}
7373

74-
7574
func (m *MetricCollectorList) GaugeSet(gauge *prometheus.GaugeVec) {
7675
for _, metric := range m.list {
7776
gauge.With(metric.labels).Set(metric.value)

metrics_agentpool.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type MetricsCollectorAgentPool struct {
1515
agentPoolAgent *prometheus.GaugeVec
1616
agentPoolAgentStatus *prometheus.GaugeVec
1717
agentPoolAgentJob *prometheus.GaugeVec
18+
agentPoolQueueLength *prometheus.GaugeVec
1819
}
1920
}
2021

@@ -102,6 +103,7 @@ func (m *MetricsCollectorAgentPool) Reset() {
102103
m.prometheus.agentPoolAgent.Reset()
103104
m.prometheus.agentPoolAgentStatus.Reset()
104105
m.prometheus.agentPoolAgentJob.Reset()
106+
m.prometheus.agentPoolQueueLength.Reset()
105107
}
106108

107109
func (m *MetricsCollectorAgentPool) Collect(ctx context.Context, callback chan<- func()) {
@@ -111,6 +113,7 @@ func (m *MetricsCollectorAgentPool) Collect(ctx context.Context, callback chan<-
111113

112114
for _, agentPoolId := range m.CollectorReference.AgentPoolIdList {
113115
m.collectAgentQueues(ctx, callback, agentPoolId)
116+
m.collectAgentPoolJobs(ctx, callback, agentPoolId)
114117
}
115118
}
116119

@@ -185,7 +188,7 @@ func (m *MetricsCollectorAgentPool) collectAgentQueues(ctx context.Context, call
185188
"definitionName": agentPoolAgent.AssignedRequest.Definition.Name,
186189
"scopeID": agentPoolAgent.AssignedRequest.ScopeId,
187190
}
188-
agentPoolAgentJobMetric.Add(jobLabels, timeToFloat64(agentPoolAgent.AssignedRequest.AssignTime))
191+
agentPoolAgentJobMetric.Add(jobLabels, timeToFloat64(*agentPoolAgent.AssignedRequest.AssignTime))
189192
}
190193
}
191194

@@ -195,3 +198,31 @@ func (m *MetricsCollectorAgentPool) collectAgentQueues(ctx context.Context, call
195198
agentPoolAgentJobMetric.GaugeSet(m.prometheus.agentPoolAgentJob)
196199
}
197200
}
201+
202+
func (m *MetricsCollectorAgentPool) collectAgentPoolJobs(ctx context.Context, callback chan<- func(), agentPoolId int64) {
203+
list, err := AzureDevopsClient.ListAgentPoolJobs(agentPoolId)
204+
if err != nil {
205+
Logger.Errorf("agentpool[%v]call[ListAgentJobs]: %v", agentPoolId, err)
206+
return
207+
}
208+
209+
agentPoolQueueLength := NewMetricCollectorList()
210+
211+
notStartedJobCount := 0
212+
213+
for _, agentPoolJob := range list.List {
214+
if agentPoolJob.AssignTime == nil {
215+
notStartedJobCount++
216+
}
217+
}
218+
219+
infoLabels := prometheus.Labels{
220+
"agentPoolID": int64ToString(agentPoolId),
221+
}
222+
223+
agentPoolQueueLength.Add(infoLabels, 1)
224+
225+
callback <- func() {
226+
agentPoolQueueLength.GaugeSet(m.prometheus.agentPoolAgent)
227+
}
228+
}

0 commit comments

Comments
 (0)