Skip to content

Commit 05465b8

Browse files
committed
matrices published
1 parent 3d5de0d commit 05465b8

File tree

4 files changed

+111
-26
lines changed

4 files changed

+111
-26
lines changed

chart-sync/App.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/prometheus/client_golang/prometheus/promhttp"
88
"go.uber.org/zap"
99
"net/http"
10+
"strconv"
1011
"time"
1112
)
1213

@@ -32,11 +33,18 @@ func NewApp(Logger *zap.SugaredLogger,
3233
func (app *App) Start() {
3334
// Set up the /metrics endpoint for Prometheus to scrape
3435
http.Handle("/metrics", promhttp.Handler())
36+
go func() {
37+
// Then modify the line to:
38+
err := http.ListenAndServe(":"+strconv.Itoa(app.configuration.PrometheusMatrixPort), nil)
39+
if err != nil {
40+
app.Logger.Errorw("error in starting prometheus server", "err", err)
41+
}
42+
}()
3543

3644
// Start the sync service
3745
_, err := app.syncService.Sync()
3846
// sleep for ShutDownInterval seconds to give time for prometheus to scrape the metrics
39-
time.Sleep(time.Duration(app.configuration.ShutDownInterval) * time.Second)
47+
time.Sleep(time.Duration(app.configuration.AppSyncJobShutDownInterval) * time.Second)
4048

4149
if err != nil {
4250
app.Logger.Errorw("err", "err", err)

chart-sync/internals/Configuration.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ type Configuration struct {
77
ChartProviderId string `env:"CHART_PROVIDER_ID" envDefault:"*"` // * is used to sync all chart providers; else CHART_PROVIDER_ID should contain chart_repo_id OR docker_artifact_store_id
88
IsOCIRegistry bool `env:"IS_OCI_REGISTRY" envDefault:"true"`
99
ParallelismLimitForTagProcessing int `env:"PARALLELISM_LIMIT_FOR_TAG_PROCESSING" envDefault:"0"`
10-
ShutDownInterval int `env:"SHUTDOWN_INTERVAL" envDefault:"120"`
10+
AppSyncJobShutDownInterval int `env:"APP_SYNC_SHUTDOWN_INTERVAL" envDefault:"60"`
11+
PrometheusMatrixPort int `env:"PROMETHEUS_MATRIX_PORT" envDefault:"8080"`
1112
}
1213

1314
func ParseConfiguration() (*Configuration, error) {

chart-sync/internals/instruments.go

+65-20
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,71 @@ import (
77

88
var constLabels = map[string]string{"app": "chart-sync"}
99

10-
var SyncOCIRepo = promauto.NewCounterVec(
11-
prometheus.CounterOpts{
12-
Name: "sync_oci_repo",
13-
Help: "no of update received in given chart and version",
14-
ConstLabels: constLabels,
15-
},
16-
[]string{})
17-
18-
var SyncRepo = promauto.NewCounterVec(
19-
prometheus.CounterOpts{
20-
Name: "sync_repo",
21-
Help: "no of update received in given chart and version",
22-
ConstLabels: constLabels,
23-
},
24-
[]string{})
10+
// Counter metrics
11+
var (
12+
SyncOCIRepo = promauto.NewCounterVec(
13+
prometheus.CounterOpts{
14+
Name: "sync_oci_repo",
15+
Help: "Number of OCI repository sync operations",
16+
ConstLabels: constLabels,
17+
},
18+
[]string{"repo_name"})
19+
20+
SyncRepo = promauto.NewCounterVec(
21+
prometheus.CounterOpts{
22+
Name: "sync_repo",
23+
Help: "Number of standard repository sync operations",
24+
ConstLabels: constLabels,
25+
},
26+
[]string{"repo_name"})
27+
28+
ChartVersionsProcessed = promauto.NewCounterVec(
29+
prometheus.CounterOpts{
30+
Name: "chart_versions_processed_total",
31+
Help: "Total number of chart versions processed successfully",
32+
ConstLabels: constLabels,
33+
},
34+
[]string{"repo_type", "chart_name"})
35+
36+
ChartVersionsFailedProcessing = promauto.NewCounterVec(
37+
prometheus.CounterOpts{
38+
Name: "chart_versions_failed_processing_total",
39+
Help: "Total number of chart versions that failed processing",
40+
ConstLabels: constLabels,
41+
},
42+
[]string{"repo_type", "chart_name", "error_type"})
43+
44+
AppStoresCreated = promauto.NewCounter(
45+
prometheus.CounterOpts{
46+
Name: "app_stores_created_total",
47+
Help: "Total number of app stores created during sync",
48+
ConstLabels: constLabels,
49+
})
50+
51+
AppVersionsCreated = promauto.NewCounter(
52+
prometheus.CounterOpts{
53+
Name: "app_versions_created_total",
54+
Help: "Total number of app versions created during sync",
55+
ConstLabels: constLabels,
56+
})
57+
58+
RepoSyncErrors = promauto.NewCounterVec(
59+
prometheus.CounterOpts{
60+
Name: "repo_sync_errors_total",
61+
Help: "Total number of repository sync errors",
62+
ConstLabels: constLabels,
63+
},
64+
[]string{"repo_type", "error_type"})
65+
)
2566

67+
// Histogram metrics
2668
var (
27-
httpDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
28-
Name: "http_duration_seconds",
29-
Help: "Duration of HTTP requests.",
30-
ConstLabels: constLabels,
31-
}, []string{"path", "method", "status"})
69+
RepoSyncDuration = promauto.NewHistogramVec(
70+
prometheus.HistogramOpts{
71+
Name: "repo_sync_duration_seconds",
72+
Help: "Time taken to sync an entire repository",
73+
ConstLabels: constLabels,
74+
Buckets: prometheus.DefBuckets,
75+
},
76+
[]string{"repo_type", "repo_name"})
3277
)

chart-sync/pkg/SyncService.go

+35-4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ func (impl *SyncServiceImpl) Sync() (interface{}, error) {
7171
ociRegistries []*sql.DockerArtifactStore
7272
ociRegistry *sql.DockerArtifactStore
7373
)
74+
// Track overall sync time
75+
start := time.Now()
76+
defer func() {
77+
internals.RepoSyncDuration.WithLabelValues("all", "all").Observe(time.Since(start).Seconds())
78+
}()
7479
if impl.configuration.ChartProviderId == "*" {
7580
ociRegistries, err = impl.dockerArtifactStoreRepository.FindAllChartProviders()
7681
if err != nil {
@@ -134,8 +139,14 @@ func extractChartRepoRepositoryList(repositoryList string) []string {
134139
}
135140

136141
func (impl *SyncServiceImpl) syncOCIRepo(ociRepo *sql.DockerArtifactStore) error {
137-
// prometheus event for OCI registry sync
138-
internals.SyncOCIRepo.WithLabelValues().Inc()
142+
// Track OCI repo sync time
143+
start := time.Now()
144+
defer func() {
145+
internals.RepoSyncDuration.WithLabelValues("oci", ociRepo.RegistryURL).Observe(time.Since(start).Seconds())
146+
}()
147+
148+
// prometheus event for OCI registry sync (already present)
149+
internals.SyncOCIRepo.WithLabelValues(ociRepo.RegistryURL).Inc()
139150

140151
applications, err := impl.appStoreRepository.FindByStoreId(ociRepo.Id)
141152
if err != nil {
@@ -239,6 +250,8 @@ func (impl *SyncServiceImpl) syncOCIRepo(ociRepo *sql.DockerArtifactStore) error
239250
impl.logger.Errorw("error in saving app", "app", app, "err", err)
240251
continue
241252
}
253+
// Increment app stores created counter
254+
internals.AppStoresCreated.Inc()
242255
} else {
243256
continue
244257
}
@@ -253,6 +266,7 @@ func (impl *SyncServiceImpl) syncOCIRepo(ociRepo *sql.DockerArtifactStore) error
253266
err = impl.updateOCIRegistryChartVersionsV2(client, id, chartVersions, ociRepo, chartName)
254267
}
255268
if err != nil {
269+
internals.RepoSyncErrors.WithLabelValues("oci", "process_error").Inc()
256270
impl.logger.Errorw("error in updating chart versions", "err", err, "appId", id)
257271
continue
258272
}
@@ -261,8 +275,14 @@ func (impl *SyncServiceImpl) syncOCIRepo(ociRepo *sql.DockerArtifactStore) error
261275
}
262276

263277
func (impl *SyncServiceImpl) syncRepo(repo *sql.ChartRepo) error {
264-
// prometheus event for registry sync
265-
internals.SyncRepo.WithLabelValues().Inc()
278+
// Track standard repo sync time
279+
start := time.Now()
280+
defer func() {
281+
internals.RepoSyncDuration.WithLabelValues("standard", repo.Name).Observe(time.Since(start).Seconds())
282+
}()
283+
284+
// prometheus event for registry sync (already present)
285+
internals.SyncRepo.WithLabelValues(repo.Name).Inc()
266286

267287
indexFile, err := impl.helmRepoManager.LoadIndexFile(repo)
268288
if err != nil {
@@ -294,13 +314,17 @@ func (impl *SyncServiceImpl) syncRepo(repo *sql.ChartRepo) error {
294314
impl.logger.Errorw("error in saving app", "app", app, "err", err)
295315
continue
296316
}
317+
// Increment app stores created counter
318+
internals.AppStoresCreated.Inc()
319+
297320
applicationId[name] = app.Id
298321
id = app.Id
299322
}
300323
//update entries if any id, chartVersions
301324
impl.logger.Infow("handling all versions of chart", "repoName", repo.Name, "chartName", name, "chartVersions", len(chartVersions))
302325
err := impl.updateChartVersions(id, &chartVersions, repo.Url, repo.Username, repo.Password, repo.AllowInsecureConnection)
303326
if err != nil {
327+
internals.RepoSyncErrors.WithLabelValues("standard", "process_error").Inc()
304328
impl.logger.Errorw("error in updating chart versions", "err", err, "appId", id)
305329
continue
306330
}
@@ -388,9 +412,12 @@ func (impl *SyncServiceImpl) updateChartVersions(appId int, chartVersions *repo.
388412
impl.logger.Infow("saving chart versions into DB", "versions", len(appVersions))
389413
err = impl.appStoreApplicationVersionRepository.Save(&appVersions)
390414
if err != nil {
415+
internals.RepoSyncErrors.WithLabelValues("standard", "db_save_error").Inc()
391416
impl.logger.Errorw("error in updating", "totalIn", len(*chartVersions), "totalOut", len(appVersions), "err", err)
392417
return err
393418
}
419+
// Count app versions created
420+
internals.AppVersionsCreated.Add(float64(len(appVersions)))
394421
// reset the array
395422
appVersions = nil
396423
}
@@ -430,10 +457,14 @@ func (impl *SyncServiceImpl) updateOCIRegistryChartVersions(client *registry.Cli
430457

431458
chartData, err := impl.helmRepoManager.OCIRepoValuesJson(client, ociRepo.RegistryURL, chartName, chartVersion)
432459
if err != nil {
460+
internals.ChartVersionsFailedProcessing.WithLabelValues("oci", chartName, "processing_error").Inc()
433461
impl.logger.Errorw("error in getting values yaml", "err", err)
434462
continue
435463
}
436464

465+
// Track successful processing
466+
internals.ChartVersionsProcessed.WithLabelValues("oci", chartName).Inc()
467+
437468
if !isAnyChartVersionFound {
438469
isAnyChartVersionFound = true
439470
}

0 commit comments

Comments
 (0)