Skip to content

Commit aad8e42

Browse files
committed
Fetch builds per time
- Fetch also builds per time (like releases) - Add env LIMIT_BUILD_HISTORY_DURATION - Add env LIMIT_RELEASE_HISTORY_DURATION (renamed from LIMIT_RELEASE_DURATION) - Add env LIMIT_BUILDS_PER_PROJECT
1 parent 56a3164 commit aad8e42

File tree

8 files changed

+29
-17
lines changed

8 files changed

+29
-17
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ Normally no configuration is needed but can be customized using environment vari
3535
| `AZURE_DEVOPS_APIVERSION` | fixed version | API version used to query for Azure DevOps |
3636
| `REQUEST_CONCURRENCY` | `10` | API request concurrency (number of calls at the same time) |
3737
| `REQUEST_RETRIES` | `3` | API request retries in case of failure |
38+
| `LIMIT_BUILDS_PER_PROJECT` | `100` | Fetched builds per project |
3839
| `LIMIT_BUILDS_PER_DEFINITION` | `10` | Fetched builds per definition |
3940
| `LIMIT_RELEASES_PER_PROJECT` | `100` | Fetched releases per project |
4041
| `LIMIT_RELEASES_PER_DEFINITION` | `100` | Fetched releases per definition |
4142
| `LIMIT_DEPLOYMENTS_PER_DEFINITION` | `100` | Fetched deployments per definition |
4243
| `LIMIT_RELEASEDEFINITION_PER_PROJECT` | `100` | Fetched builds per definition |
43-
| `LIMIT_RELEASE_DURATION` | `48h` | Time (time.Duration) how long the exporter should look back for releases |
44+
| `LIMIT_BUILD_HISTORY_DURATION` | `48h` | Time (time.Duration) how long the exporter should look back for builds |
45+
| `LIMIT_RELEASE_HISTORY_DURATION` | `48h` | Time (time.Duration) how long the exporter should look back for releases |
4446

4547

4648
Metrics

azure-devops-client/build.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,12 @@ func (c *AzureDevopsClient) ListBuildHistory(project string, minTime time.Time)
140140
c.concurrencyLock()
141141

142142
url := fmt.Sprintf(
143-
"%v/_apis/build/builds?api-version=%v&minTime=%s",
143+
"%v/_apis/build/builds?api-version=%v&minTime=%s&$top=%v",
144144
url.QueryEscape(project),
145145
url.QueryEscape(c.ApiVersion),
146146
url.QueryEscape(minTime.Format(time.RFC3339)),
147+
url.QueryEscape(int64ToString(c.LimitBuildsPerProject)),
148+
147149
)
148150
response, err := c.rest().R().Get(url)
149151
if err := c.checkResponse(response, err); err != nil {

azure-devops-client/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type AzureDevopsClient struct {
2525
RequestCount uint64
2626
RequestRetries int
2727

28+
LimitBuildsPerProject int64
2829
LimitBuildsPerDefinition int64
2930
LimitReleasesPerDefinition int64
3031
LimitDeploymentPerDefinition int64
@@ -46,6 +47,7 @@ func (c *AzureDevopsClient) Init() {
4647
c.SetRetries(3)
4748
c.SetConcurrency(10)
4849

50+
c.LimitBuildsPerProject = 100
4951
c.LimitBuildsPerDefinition = 10
5052
c.LimitReleasesPerDefinition = 100
5153
c.LimitDeploymentPerDefinition = 100

azure-devops-client/release.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,16 @@ func (r *Release) QueueDuration() time.Duration {
148148
return r.StartTime.Sub(r.QueueTime)
149149
}
150150

151-
func (c *AzureDevopsClient) ListLatestReleases(project string, minTime time.Time) (list ReleaseList, error error) {
151+
func (c *AzureDevopsClient) ListReleases(project string, releaseDefinitionId int64) (list ReleaseList, error error) {
152152
defer c.concurrencyUnlock()
153153
c.concurrencyLock()
154154

155155
url := fmt.Sprintf(
156-
"%v/_apis/release/releases?api-version=%v&isDeleted=false&$expand=94&minCreatedTime=%s&$top=%v",
156+
"%v/_apis/release/releases?api-version=%v&isDeleted=false&$expand=94&definitionId=%s&$top=%v",
157157
url.QueryEscape(project),
158158
url.QueryEscape(c.ApiVersion),
159-
url.QueryEscape(minTime.Format(time.RFC3339)),
160-
url.QueryEscape(int64ToString(c.LimitReleasesPerProject)),
159+
url.QueryEscape(int64ToString(releaseDefinitionId)),
160+
url.QueryEscape(int64ToString(c.LimitReleasesPerDefinition)),
161161
)
162162
response, err := c.restVsrm().R().Get(url)
163163
if err := c.checkResponse(response, err); err != nil {
@@ -174,16 +174,16 @@ func (c *AzureDevopsClient) ListLatestReleases(project string, minTime time.Time
174174
return
175175
}
176176

177-
func (c *AzureDevopsClient) ListReleases(project string, releaseDefinitionId int64) (list ReleaseList, error error) {
177+
func (c *AzureDevopsClient) ListReleaseHistory(project string, minTime time.Time) (list ReleaseList, error error) {
178178
defer c.concurrencyUnlock()
179179
c.concurrencyLock()
180180

181181
url := fmt.Sprintf(
182-
"%v/_apis/release/releases?api-version=%v&isDeleted=false&$expand=94&definitionId=%s&$top=%v",
182+
"%v/_apis/release/releases?api-version=%v&isDeleted=false&$expand=94&minCreatedTime=%s&$top=%v",
183183
url.QueryEscape(project),
184184
url.QueryEscape(c.ApiVersion),
185-
url.QueryEscape(int64ToString(releaseDefinitionId)),
186-
url.QueryEscape(int64ToString(c.LimitReleasesPerDefinition)),
185+
url.QueryEscape(minTime.Format(time.RFC3339)),
186+
url.QueryEscape(int64ToString(c.LimitReleasesPerProject)),
187187
)
188188
response, err := c.restVsrm().R().Get(url)
189189
if err := c.checkResponse(response, err); err != nil {

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ var opts struct {
7070
RequestConcurrencyLimit int64 `long:"request.concurrency" env:"REQUEST_CONCURRENCY" description:"Number of concurrent requests against dev.azure.com" default:"10"`
7171
RequestRetries int `long:"request.retries" env:"REQUEST_RETRIES" description:"Number of retried requests against dev.azure.com" default:"3"`
7272

73+
LimitBuildsPerProject int64 `long:"limit.builds-per-project" env:"LIMIT_BUILDS_PER_PROJECT" description:"Limit builds per project" default:"100"`
7374
LimitBuildsPerDefinition int64 `long:"limit.builds-per-definition" env:"LIMIT_BUILDS_PER_DEFINITION" description:"Limit builds per definition" default:"10"`
7475
LimitReleasesPerProject int64 `long:"limit.releases-per-project" env:"LIMIT_RELEASES_PER_PROJECT" description:"Limit releases per project" default:"100"`
7576
LimitReleasesPerDefinition int64 `long:"limit.releases-per-definition" env:"LIMIT_RELEASES_PER_DEFINITION" description:"Limit releases per definition" default:"100"`
7677
LimitDeploymentPerDefinition int64 `long:"limit.deployments-per-definition" env:"LIMIT_DEPLOYMENTS_PER_DEFINITION" description:"Limit deployments per definition" default:"100"`
7778
LimitReleaseDefinitionsPerProject int64 `long:"limit.releasedefinitions-per-project" env:"LIMIT_RELEASEDEFINITION_PER_PROJECT" description:"Limit builds per definition" default:"100"`
78-
LimitReleaseDuration time.Duration `long:"limit.release-duration" env:"LIMIT_RELEASE_DURATION" description:"Time (time.Duration) how long the exporter should look back for releases" default:"48h"`
79+
LimitBuildHistoryDuration time.Duration `long:"limit.build-history-duration" env:"LIMIT_BUILD_HISTORY_DURATION" description:"Time (time.Duration) how long the exporter should look back for builds" default:"48h"`
80+
LimitReleaseHistoryDuration time.Duration `long:"limit.release-history-duration" env:"LIMIT_RELEASE_HISTORY_DURATION" description:"Time (time.Duration) how long the exporter should look back for releases" default:"48h"`
7981
}
8082

8183
func main() {
@@ -201,6 +203,7 @@ func initAzureDevOpsConnection() {
201203
AzureDevopsClient.SetRetries(opts.RequestRetries)
202204
AzureDevopsClient.SetUserAgent(fmt.Sprintf("azure-devops-exporter/%v", Version))
203205

206+
AzureDevopsClient.LimitBuildsPerProject = opts.LimitBuildsPerProject
204207
AzureDevopsClient.LimitBuildsPerDefinition = opts.LimitBuildsPerDefinition
205208
AzureDevopsClient.LimitReleasesPerDefinition = opts.LimitReleasesPerDefinition
206209
AzureDevopsClient.LimitDeploymentPerDefinition = opts.LimitDeploymentPerDefinition

metrics_build.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"github.com/prometheus/client_golang/prometheus"
66
devopsClient "github.com/webdevops/azure-devops-exporter/azure-devops-client"
7+
"time"
78
)
89

910
type MetricsCollectorBuild struct {
@@ -116,9 +117,11 @@ func (m *MetricsCollectorBuild) collectDefinition(ctx context.Context, callback
116117
}
117118

118119
func (m *MetricsCollectorBuild) collectBuilds(ctx context.Context, callback chan<- func(), project devopsClient.Project) {
119-
list, err := AzureDevopsClient.ListBuilds(project.Id)
120+
minTime := time.Now().Add(-opts.LimitBuildHistoryDuration)
121+
122+
list, err := AzureDevopsClient.ListBuildHistory(project.Id, minTime)
120123
if err != nil {
121-
Logger.Errorf("project[%v]call[ListBuilds]: %v", project.Name, err)
124+
Logger.Errorf("project[%v]call[ListBuildHistory]: %v", project.Name, err)
122125
return
123126
}
124127

metrics_release.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,11 @@ func (m *MetricsCollectorRelease) Collect(ctx context.Context, callback chan<- f
205205

206206
// --------------------------------------
207207
// Releases
208-
minTime := time.Now().Add(-time.Duration(opts.LimitReleaseDuration))
208+
minTime := time.Now().Add(-opts.LimitReleaseHistoryDuration)
209209

210-
releaseList, err := AzureDevopsClient.ListLatestReleases(project.Id, minTime)
210+
releaseList, err := AzureDevopsClient.ListReleaseHistory(project.Id, minTime)
211211
if err != nil {
212-
Logger.Errorf("project[%v]call[ListLatestReleases]: %v", project.Name, err)
212+
Logger.Errorf("project[%v]call[ListReleaseHistory]: %v", project.Name, err)
213213
return
214214
}
215215

metrics_stats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (m *MetricsCollectorStats) Collect(ctx context.Context, callback chan<- fun
144144
func (m *MetricsCollectorStats) CollectReleases(ctx context.Context, callback chan<- func(), project devopsClient.Project) {
145145
minTime := *m.CollectorReference.collectionLastTime
146146

147-
releaseList, err := AzureDevopsClient.ListLatestReleases(project.Id, minTime)
147+
releaseList, err := AzureDevopsClient.ListReleaseHistory(project.Id, minTime)
148148
if err != nil {
149149
Logger.Errorf("project[%v]call[ListLatestReleases]: %v", project.Name, err)
150150
return

0 commit comments

Comments
 (0)