• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mvisonneau / gitlab-ci-pipelines-exporter / 21040431532

15 Jan 2026 05:32PM UTC coverage: 61.785% (+0.03%) from 61.751%
21040431532

push

github

web-flow
fix(deps): update module github.com/sirupsen/logrus to v1.9.4 (#1033)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

3620 of 5859 relevant lines covered (61.79%)

3.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.85
/pkg/controller/environments.go
1
package controller
2

3
import (
4
        "context"
5

6
        log "github.com/sirupsen/logrus"
7

8
        "github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/schemas"
9
)
10

11
// PullEnvironmentsFromProject ..
12
func (c *Controller) PullEnvironmentsFromProject(ctx context.Context, p schemas.Project) (err error) {
1✔
13
        var envs schemas.Environments
1✔
14

1✔
15
        envs, err = c.Gitlab.GetProjectEnvironments(ctx, p)
1✔
16
        if err != nil {
1✔
17
                return
×
18
        }
×
19

20
        for k := range envs {
2✔
21
                var exists bool
1✔
22

1✔
23
                exists, err = c.Store.EnvironmentExists(ctx, k)
1✔
24
                if err != nil {
1✔
25
                        return
×
26
                }
×
27

28
                if !exists {
2✔
29
                        env := envs[k]
1✔
30
                        if err = c.UpdateEnvironment(ctx, &env); err != nil {
1✔
31
                                return
×
32
                        }
×
33

34
                        log.WithFields(log.Fields{
1✔
35
                                "project-name":     env.ProjectName,
1✔
36
                                "environment-id":   env.ID,
1✔
37
                                "environment-name": env.Name,
1✔
38
                        }).Info("discovered new environment")
1✔
39

1✔
40
                        c.ScheduleTask(ctx, schemas.TaskTypePullEnvironmentMetrics, string(env.Key()), env)
1✔
41
                }
42
        }
43

44
        return
1✔
45
}
46

47
// UpdateEnvironment ..
48
func (c *Controller) UpdateEnvironment(ctx context.Context, env *schemas.Environment) error {
5✔
49
        pulledEnv, err := c.Gitlab.GetEnvironment(ctx, env.ProjectName, env.ID)
5✔
50
        if err != nil {
7✔
51
                return err
2✔
52
        }
2✔
53

54
        env.Available = pulledEnv.Available
2✔
55
        env.ExternalURL = pulledEnv.ExternalURL
2✔
56
        env.LatestDeployment = pulledEnv.LatestDeployment
2✔
57

2✔
58
        return c.Store.SetEnvironment(ctx, *env)
2✔
59
}
60

61
// PullEnvironmentMetrics ..
62
func (c *Controller) PullEnvironmentMetrics(ctx context.Context, env schemas.Environment) (err error) {
4✔
63
        // At scale, the scheduled environment may be behind the actual state being stored
4✔
64
        // to avoid issues, we refresh it from the store before manipulating it
4✔
65
        if err := c.Store.GetEnvironment(ctx, &env); err != nil {
4✔
66
                return err
×
67
        }
×
68

69
        // Save the existing deployment ID before we updated environment from the API
70
        deploymentJobID := env.LatestDeployment.JobID
4✔
71

4✔
72
        if err = c.UpdateEnvironment(ctx, &env); err != nil {
6✔
73
                return
2✔
74
        }
2✔
75

76
        var (
1✔
77
                infoLabels = env.InformationLabelsValues()
1✔
78
                commitDate float64
1✔
79
        )
1✔
80

1✔
81
        switch env.LatestDeployment.RefKind {
1✔
82
        case schemas.RefKindBranch:
1✔
83
                infoLabels["latest_commit_short_id"], commitDate, err = c.Gitlab.GetBranchLatestCommit(ctx, env.ProjectName, env.LatestDeployment.RefName)
1✔
84
        case schemas.RefKindTag:
×
85
                // TODO: Review how to manage this in a nicier fashion
×
86
                infoLabels["latest_commit_short_id"], commitDate, err = c.Gitlab.GetProjectMostRecentTagCommit(ctx, env.ProjectName, ".*")
×
87
        default:
×
88
                infoLabels["latest_commit_short_id"] = env.LatestDeployment.CommitShortID
×
89
                commitDate = env.LatestDeployment.Timestamp
×
90
        }
91

92
        if err != nil {
1✔
93
                return err
×
94
        }
×
95

96
        var (
1✔
97
                envBehindDurationSeconds float64
1✔
98
                envBehindCommitCount     float64
1✔
99
        )
1✔
100

1✔
101
        behindCommitsCountMetric := schemas.Metric{
1✔
102
                Kind:   schemas.MetricKindEnvironmentBehindCommitsCount,
1✔
103
                Labels: env.DefaultLabelsValues(),
1✔
104
        }
1✔
105

1✔
106
        // To reduce the amount of compare requests being made, we check if the labels are unchanged since
1✔
107
        // the latest emission of the information metric
1✔
108
        if infoLabels["latest_commit_short_id"] != infoLabels["current_commit_short_id"] {
1✔
109
                infoMetric := schemas.Metric{
×
110
                        Kind:   schemas.MetricKindEnvironmentInformation,
×
111
                        Labels: env.DefaultLabelsValues(),
×
112
                }
×
113

×
114
                var commitCount int
×
115

×
116
                if err = c.Store.GetMetric(ctx, &infoMetric); err != nil {
×
117
                        return err
×
118
                }
×
119

120
                if infoMetric.Labels["latest_commit_short_id"] != infoLabels["latest_commit_short_id"] ||
×
121
                        infoMetric.Labels["current_commit_short_id"] != infoLabels["current_commit_short_id"] {
×
122
                        commitCount, err = c.Gitlab.GetCommitCountBetweenRefs(ctx, env.ProjectName, infoLabels["current_commit_short_id"], infoLabels["latest_commit_short_id"])
×
123
                        if err != nil {
×
124
                                return err
×
125
                        }
×
126

127
                        envBehindCommitCount = float64(commitCount)
×
128
                } else {
×
129
                        // TODO: Find a more efficient way
×
130
                        if err = c.Store.GetMetric(ctx, &behindCommitsCountMetric); err != nil {
×
131
                                return err
×
132
                        }
×
133

134
                        envBehindCommitCount = behindCommitsCountMetric.Value
×
135
                }
136
        }
137

138
        storeSetMetric(ctx, c.Store, schemas.Metric{
1✔
139
                Kind:   schemas.MetricKindEnvironmentBehindCommitsCount,
1✔
140
                Labels: env.DefaultLabelsValues(),
1✔
141
                Value:  envBehindCommitCount,
1✔
142
        })
1✔
143

1✔
144
        if commitDate-env.LatestDeployment.Timestamp > 0 {
1✔
145
                envBehindDurationSeconds = commitDate - env.LatestDeployment.Timestamp
×
146
        }
×
147

148
        envDeploymentCount := schemas.Metric{
1✔
149
                Kind:   schemas.MetricKindEnvironmentDeploymentCount,
1✔
150
                Labels: env.DefaultLabelsValues(),
1✔
151
        }
1✔
152

1✔
153
        storeGetMetric(ctx, c.Store, &envDeploymentCount)
1✔
154

1✔
155
        if env.LatestDeployment.JobID > deploymentJobID {
2✔
156
                envDeploymentCount.Value++
1✔
157
        }
1✔
158

159
        storeSetMetric(ctx, c.Store, envDeploymentCount)
1✔
160

1✔
161
        storeSetMetric(ctx, c.Store, schemas.Metric{
1✔
162
                Kind:   schemas.MetricKindEnvironmentBehindDurationSeconds,
1✔
163
                Labels: env.DefaultLabelsValues(),
1✔
164
                Value:  envBehindDurationSeconds,
1✔
165
        })
1✔
166

1✔
167
        storeSetMetric(ctx, c.Store, schemas.Metric{
1✔
168
                Kind:   schemas.MetricKindEnvironmentDeploymentDurationSeconds,
1✔
169
                Labels: env.DefaultLabelsValues(),
1✔
170
                Value:  env.LatestDeployment.DurationSeconds,
1✔
171
        })
1✔
172

1✔
173
        storeSetMetric(ctx, c.Store, schemas.Metric{
1✔
174
                Kind:   schemas.MetricKindEnvironmentDeploymentJobID,
1✔
175
                Labels: env.DefaultLabelsValues(),
1✔
176
                Value:  float64(env.LatestDeployment.JobID),
1✔
177
        })
1✔
178

1✔
179
        emitStatusMetric(
1✔
180
                ctx,
1✔
181
                c.Store,
1✔
182
                schemas.MetricKindEnvironmentDeploymentStatus,
1✔
183
                env.DefaultLabelsValues(),
1✔
184
                statusesList[:],
1✔
185
                env.LatestDeployment.Status,
1✔
186
                env.OutputSparseStatusMetrics,
1✔
187
        )
1✔
188

1✔
189
        storeSetMetric(ctx, c.Store, schemas.Metric{
1✔
190
                Kind:   schemas.MetricKindEnvironmentDeploymentTimestamp,
1✔
191
                Labels: env.DefaultLabelsValues(),
1✔
192
                Value:  env.LatestDeployment.Timestamp,
1✔
193
        })
1✔
194

1✔
195
        storeSetMetric(ctx, c.Store, schemas.Metric{
1✔
196
                Kind:   schemas.MetricKindEnvironmentInformation,
1✔
197
                Labels: infoLabels,
1✔
198
                Value:  1,
1✔
199
        })
1✔
200

1✔
201
        return nil
1✔
202
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc