Skip to content

Commit efba10a

Browse files
feat: Add timer/counter telemetry for CI process (#3081)
* code change 1 * code change 2 * code-change-3 * code change 4 * add-expose-flag * code change 5 * code change 6 * change-cd-telemetry-trigger-function * add-deployment-type * server-mode-change * remove-loop-import * add-wire-gen * Update AppService function name * remove finished on * ci-telemetry * add-flag-and-appName * add-flag-and-appName * add-app-name * improve-ci-metrics-struct * improve-ci-metrics-struct * improve-ci-metrics-struct * add-ci-pipeline * Add check to Cache Upload * Code-refactor
1 parent b8502f2 commit efba10a

File tree

7 files changed

+94
-5
lines changed

7 files changed

+94
-5
lines changed

Wire.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ func InitializeApp() (*App, error) {
410410
pubsub.NewApplicationStatusHandlerImpl,
411411
wire.Bind(new(pubsub.ApplicationStatusHandler), new(*pubsub.ApplicationStatusHandlerImpl)),
412412

413+
pubsub.GetCiEventConfig,
413414
pubsub.NewCiEventHandlerImpl,
414415
wire.Bind(new(pubsub.CiEventHandler), new(*pubsub.CiEventHandlerImpl)),
415416

api/router/pubsub/CiEventHandler.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,25 @@ package pubsub
2020
import (
2121
"encoding/json"
2222
"fmt"
23+
"github.com/caarlos0/env/v6"
2324
pubsub "github.com/devtron-labs/common-lib/pubsub-lib"
2425
"github.com/devtron-labs/devtron/internal/sql/repository"
2526
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
2627
"github.com/devtron-labs/devtron/pkg/pipeline"
28+
"github.com/devtron-labs/devtron/util"
2729
"go.uber.org/zap"
2830
)
2931

32+
type CiEventConfig struct {
33+
ExposeCiMetrics bool `env:"EXPOSE_CI_METRICS" envDefault:"false"`
34+
}
35+
36+
func GetCiEventConfig() (*CiEventConfig, error) {
37+
cfg := &CiEventConfig{}
38+
err := env.Parse(cfg)
39+
return cfg, err
40+
}
41+
3042
type CiEventHandler interface {
3143
Subscribe() error
3244
BuildCiArtifactRequest(event CiCompleteEvent) (*pipeline.CiArtifactWebhookRequest, error)
@@ -37,6 +49,7 @@ type CiEventHandlerImpl struct {
3749
logger *zap.SugaredLogger
3850
pubsubClient *pubsub.PubSubClientServiceImpl
3951
webhookService pipeline.WebhookService
52+
ciEventConfig *CiEventConfig
4053
}
4154

4255
type CiCompleteEvent struct {
@@ -49,13 +62,16 @@ type CiCompleteEvent struct {
4962
PipelineName string `json:"pipelineName"`
5063
DataSource string `json:"dataSource"`
5164
MaterialType string `json:"materialType"`
65+
Metrics util.CIMetrics `json:"metrics"`
66+
AppName string `json:"appName"`
5267
}
5368

54-
func NewCiEventHandlerImpl(logger *zap.SugaredLogger, pubsubClient *pubsub.PubSubClientServiceImpl, webhookService pipeline.WebhookService) *CiEventHandlerImpl {
69+
func NewCiEventHandlerImpl(logger *zap.SugaredLogger, pubsubClient *pubsub.PubSubClientServiceImpl, webhookService pipeline.WebhookService, ciEventConfig *CiEventConfig) *CiEventHandlerImpl {
5570
ciEventHandlerImpl := &CiEventHandlerImpl{
5671
logger: logger,
5772
pubsubClient: pubsubClient,
5873
webhookService: webhookService,
74+
ciEventConfig: ciEventConfig,
5975
}
6076
err := ciEventHandlerImpl.Subscribe()
6177
if err != nil {
@@ -71,6 +87,7 @@ func (impl *CiEventHandlerImpl) Subscribe() error {
7187
//defer msg.Ack()
7288
ciCompleteEvent := CiCompleteEvent{}
7389
err := json.Unmarshal([]byte(string(msg.Data)), &ciCompleteEvent)
90+
util.TriggerCIMetrics(ciCompleteEvent.Metrics, impl.ciEventConfig.ExposeCiMetrics, ciCompleteEvent.PipelineName, ciCompleteEvent.AppName)
7491
if err != nil {
7592
impl.logger.Error("error while unmarshalling json data", "error", err)
7693
return

api/router/pubsub/WorkflowStatusUpdateHandler.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
2525
"github.com/devtron-labs/devtron/api/bean"
2626
client "github.com/devtron-labs/devtron/client/events"
27-
//"github.com/devtron-labs/devtron/client/pubsub"
2827
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
2928
"github.com/devtron-labs/devtron/pkg/pipeline"
3029
util "github.com/devtron-labs/devtron/util/event"

internal/middleware/instrument.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,42 @@ var CdDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
3838
Help: "Duration of CD process",
3939
}, []string{"appName", "status", "envName", "deploymentType"})
4040

41+
var CiDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
42+
Name: "ci_duration_seconds",
43+
Help: "Duration of CI process",
44+
Buckets: prometheus.LinearBuckets(20, 20, 5),
45+
}, []string{"pipelineName", "appName"})
46+
47+
var CacheDownloadDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
48+
Name: "cache_download_duration_seconds",
49+
Help: "Duration of Cache Download process",
50+
Buckets: prometheus.LinearBuckets(20, 20, 5),
51+
}, []string{"pipelineName", "appName"})
52+
53+
var PreCiDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
54+
Name: "pre_ci_duration_seconds",
55+
Help: "Duration of Pre CI process",
56+
Buckets: prometheus.LinearBuckets(20, 20, 5),
57+
}, []string{"pipelineName", "appName"})
58+
59+
var BuildDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
60+
Name: "build_duration_seconds",
61+
Help: "Duration of Build process",
62+
Buckets: prometheus.LinearBuckets(20, 20, 5),
63+
}, []string{"pipelineName", "appName"})
64+
65+
var PostCiDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
66+
Name: "post_ci_duration_seconds",
67+
Help: "Duration of Post CI process",
68+
Buckets: prometheus.LinearBuckets(20, 20, 5),
69+
}, []string{"pipelineName", "appName"})
70+
71+
var CacheUploadDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
72+
Name: "cache_upload_duration_seconds",
73+
Help: "Duration of Cache Upload process",
74+
Buckets: prometheus.LinearBuckets(20, 20, 5),
75+
}, []string{"pipelineName", "appName"})
76+
4177
var requestCounter = promauto.NewCounterVec(
4278
prometheus.CounterOpts{
4379
Name: "orchestrator_http_requests_total",
@@ -60,7 +96,7 @@ var CdTriggerCounter = promauto.NewCounterVec(prometheus.CounterOpts{
6096

6197
var CiTriggerCounter = promauto.NewCounterVec(prometheus.CounterOpts{
6298
Name: "ci_trigger_counter",
63-
}, []string{"appName"})
99+
}, []string{"appName", "pipelineName"})
64100

65101
// prometheusMiddleware implements mux.MiddlewareFunc.
66102
func PrometheusMiddleware(next http.Handler) http.Handler {

pkg/pipeline/CiService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (impl *CiServiceImpl) TriggerCiPipeline(trigger Trigger) (int, error) {
157157
}
158158
impl.Logger.Debugw("ci triggered", "wf name ", createdWf.Name, " pipeline ", trigger.PipelineId)
159159

160-
middleware.CiTriggerCounter.WithLabelValues(pipeline.App.AppName).Inc()
160+
middleware.CiTriggerCounter.WithLabelValues(pipeline.App.AppName, pipeline.Name).Inc()
161161
go impl.WriteCITriggerEvent(trigger, pipeline, workflowRequest)
162162
return savedCiWf.Id, err
163163
}

util/helper.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ type CDMetrics struct {
4545
Time float64
4646
}
4747

48+
type CIMetrics struct {
49+
CacheDownDuration float64 `json:"cacheDownDuration"`
50+
PreCiDuration float64 `json:"preCiDuration"`
51+
BuildDuration float64 `json:"buildDuration"`
52+
PostCiDuration float64 `json:"postCiDuration"`
53+
CacheUpDuration float64 `json:"cacheUpDuration"`
54+
TotalDuration float64 `json:"totalDuration"`
55+
CacheDownStartTime time.Time `json:"cacheDownStartTime"`
56+
PreCiStartTime time.Time `json:"preCiStart"`
57+
BuildStartTime time.Time `json:"buildStartTime"`
58+
PostCiStartTime time.Time `json:"postCiStartTime"`
59+
CacheUpStartTime time.Time `json:"cacheUpStartTime"`
60+
TotalStartTime time.Time `json:"totalStartTime"`
61+
}
62+
4863
func ContainsString(list []string, element string) bool {
4964
if len(list) == 0 {
5065
return false
@@ -226,3 +241,20 @@ func TriggerCDMetrics(wfr CDMetrics, exposeCDMetrics bool) {
226241
middleware.CdDuration.WithLabelValues(wfr.AppName, wfr.Status, wfr.EnvironmentName, wfr.DeploymentType).Observe(wfr.Time)
227242
}
228243
}
244+
245+
func TriggerCIMetrics(Metrics CIMetrics, exposeCIMetrics bool, PipelineName string, AppName string) {
246+
if exposeCIMetrics {
247+
middleware.CacheDownloadDuration.WithLabelValues(PipelineName, AppName).Observe(Metrics.CacheDownDuration)
248+
middleware.CiDuration.WithLabelValues(PipelineName, AppName).Observe(Metrics.TotalDuration)
249+
if Metrics.CacheUpDuration != 0 {
250+
middleware.CacheUploadDuration.WithLabelValues(PipelineName, AppName).Observe(Metrics.CacheUpDuration)
251+
}
252+
if Metrics.PostCiDuration != 0 {
253+
middleware.PostCiDuration.WithLabelValues(PipelineName, AppName).Observe(Metrics.PostCiDuration)
254+
}
255+
if Metrics.PreCiDuration != 0 {
256+
middleware.PreCiDuration.WithLabelValues(PipelineName, AppName).Observe(Metrics.PreCiDuration)
257+
}
258+
middleware.BuildDuration.WithLabelValues(PipelineName, AppName).Observe(Metrics.BuildDuration)
259+
}
260+
}

wire_gen.go

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)