From 874b5b0328e4f74db1b3bd2e9dbb4be5d4e3986b Mon Sep 17 00:00:00 2001 From: Ryan Hoofard <42755382+rhoofard@users.noreply.github.com> Date: Thu, 7 Sep 2023 14:10:04 -0700 Subject: [PATCH] fix: fixed branch data bug and fixed diff metrics(#101) * fix: fix com count value in getCommitData * fix: update metadata metrics and change to separate behind and aheadby * fix: moved branch variables so they are reset for each repo * chore: fix linting issue and remove comments --------- Co-authored-by: Adriel Perkins Co-authored-by: rhoofard --- .../gitproviderreceiver/documentation.md | 23 +++- .../internal/metadata/generated_config.go | 10 +- .../metadata/generated_config_test.go | 6 +- .../internal/metadata/generated_metrics.go | 124 +++++++++++++----- .../metadata/generated_metrics_test.go | 42 ++++-- .../internal/metadata/testdata/config.yaml | 12 +- .../scraper/githubscraper/github_scraper.go | 34 +++-- .../gitproviderreceiver/metadata.yaml | 12 +- 8 files changed, 193 insertions(+), 70 deletions(-) diff --git a/pkg/receiver/gitproviderreceiver/documentation.md b/pkg/receiver/gitproviderreceiver/documentation.md index 2eeed070..bda8f74b 100644 --- a/pkg/receiver/gitproviderreceiver/documentation.md +++ b/pkg/receiver/gitproviderreceiver/documentation.md @@ -12,9 +12,9 @@ metrics: enabled: false ``` -### git.repository.branch.count +### git.repository.branch.commit.aheadby.count -Number of branches that exist in the repository +Number of commits the branch is ahead of the default branch | Unit | Metric Type | Value Type | | ---- | ----------- | ---------- | @@ -25,10 +25,11 @@ Number of branches that exist in the repository | Name | Description | Values | | ---- | ----------- | ------ | | repository.name | The full name of the Git repository | Any Str | +| branch.name | The name of the branch in a given repository | Any Str | -### git.repository.branch.diff +### git.repository.branch.commit.behindby.count -Difference in commits between the branch and main +Number of commits the branch is behind the default branch | Unit | Metric Type | Value Type | | ---- | ----------- | ---------- | @@ -41,6 +42,20 @@ Difference in commits between the branch and main | repository.name | The full name of the Git repository | Any Str | | branch.name | The name of the branch in a given repository | Any Str | +### git.repository.branch.count + +Number of branches that exist in the repository + +| Unit | Metric Type | Value Type | +| ---- | ----------- | ---------- | +| 1 | Gauge | Int | + +#### Attributes + +| Name | Description | Values | +| ---- | ----------- | ------ | +| repository.name | The full name of the Git repository | Any Str | + ### git.repository.branch.time Time the branch has existed diff --git a/pkg/receiver/gitproviderreceiver/internal/metadata/generated_config.go b/pkg/receiver/gitproviderreceiver/internal/metadata/generated_config.go index 021d7b7a..9de69a0c 100644 --- a/pkg/receiver/gitproviderreceiver/internal/metadata/generated_config.go +++ b/pkg/receiver/gitproviderreceiver/internal/metadata/generated_config.go @@ -25,8 +25,9 @@ func (ms *MetricConfig) Unmarshal(parser *confmap.Conf) error { // MetricsConfig provides config for gitprovider metrics. type MetricsConfig struct { + GitRepositoryBranchCommitAheadbyCount MetricConfig `mapstructure:"git.repository.branch.commit.aheadby.count"` + GitRepositoryBranchCommitBehindbyCount MetricConfig `mapstructure:"git.repository.branch.commit.behindby.count"` GitRepositoryBranchCount MetricConfig `mapstructure:"git.repository.branch.count"` - GitRepositoryBranchDiff MetricConfig `mapstructure:"git.repository.branch.diff"` GitRepositoryBranchTime MetricConfig `mapstructure:"git.repository.branch.time"` GitRepositoryContributorCount MetricConfig `mapstructure:"git.repository.contributor.count"` GitRepositoryCount MetricConfig `mapstructure:"git.repository.count"` @@ -39,10 +40,13 @@ type MetricsConfig struct { func DefaultMetricsConfig() MetricsConfig { return MetricsConfig{ - GitRepositoryBranchCount: MetricConfig{ + GitRepositoryBranchCommitAheadbyCount: MetricConfig{ + Enabled: true, + }, + GitRepositoryBranchCommitBehindbyCount: MetricConfig{ Enabled: true, }, - GitRepositoryBranchDiff: MetricConfig{ + GitRepositoryBranchCount: MetricConfig{ Enabled: true, }, GitRepositoryBranchTime: MetricConfig{ diff --git a/pkg/receiver/gitproviderreceiver/internal/metadata/generated_config_test.go b/pkg/receiver/gitproviderreceiver/internal/metadata/generated_config_test.go index b103ee9a..740678aa 100644 --- a/pkg/receiver/gitproviderreceiver/internal/metadata/generated_config_test.go +++ b/pkg/receiver/gitproviderreceiver/internal/metadata/generated_config_test.go @@ -26,8 +26,9 @@ func TestMetricsBuilderConfig(t *testing.T) { name: "all_set", want: MetricsBuilderConfig{ Metrics: MetricsConfig{ + GitRepositoryBranchCommitAheadbyCount: MetricConfig{Enabled: true}, + GitRepositoryBranchCommitBehindbyCount: MetricConfig{Enabled: true}, GitRepositoryBranchCount: MetricConfig{Enabled: true}, - GitRepositoryBranchDiff: MetricConfig{Enabled: true}, GitRepositoryBranchTime: MetricConfig{Enabled: true}, GitRepositoryContributorCount: MetricConfig{Enabled: true}, GitRepositoryCount: MetricConfig{Enabled: true}, @@ -47,8 +48,9 @@ func TestMetricsBuilderConfig(t *testing.T) { name: "none_set", want: MetricsBuilderConfig{ Metrics: MetricsConfig{ + GitRepositoryBranchCommitAheadbyCount: MetricConfig{Enabled: false}, + GitRepositoryBranchCommitBehindbyCount: MetricConfig{Enabled: false}, GitRepositoryBranchCount: MetricConfig{Enabled: false}, - GitRepositoryBranchDiff: MetricConfig{Enabled: false}, GitRepositoryBranchTime: MetricConfig{Enabled: false}, GitRepositoryContributorCount: MetricConfig{Enabled: false}, GitRepositoryCount: MetricConfig{Enabled: false}, diff --git a/pkg/receiver/gitproviderreceiver/internal/metadata/generated_metrics.go b/pkg/receiver/gitproviderreceiver/internal/metadata/generated_metrics.go index 347fc985..38aa97a2 100644 --- a/pkg/receiver/gitproviderreceiver/internal/metadata/generated_metrics.go +++ b/pkg/receiver/gitproviderreceiver/internal/metadata/generated_metrics.go @@ -12,22 +12,22 @@ import ( conventions "go.opentelemetry.io/collector/semconv/v1.9.0" ) -type metricGitRepositoryBranchCount struct { +type metricGitRepositoryBranchCommitAheadbyCount struct { data pmetric.Metric // data buffer for generated metric. config MetricConfig // metric config provided by user. capacity int // max observed number of data points added to the metric. } -// init fills git.repository.branch.count metric with initial data. -func (m *metricGitRepositoryBranchCount) init() { - m.data.SetName("git.repository.branch.count") - m.data.SetDescription("Number of branches that exist in the repository") +// init fills git.repository.branch.commit.aheadby.count metric with initial data. +func (m *metricGitRepositoryBranchCommitAheadbyCount) init() { + m.data.SetName("git.repository.branch.commit.aheadby.count") + m.data.SetDescription("Number of commits the branch is ahead of the default branch") m.data.SetUnit("1") m.data.SetEmptyGauge() m.data.Gauge().DataPoints().EnsureCapacity(m.capacity) } -func (m *metricGitRepositoryBranchCount) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, repositoryNameAttributeValue string) { +func (m *metricGitRepositoryBranchCommitAheadbyCount) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, repositoryNameAttributeValue string, branchNameAttributeValue string) { if !m.config.Enabled { return } @@ -36,17 +36,18 @@ func (m *metricGitRepositoryBranchCount) recordDataPoint(start pcommon.Timestamp dp.SetTimestamp(ts) dp.SetIntValue(val) dp.Attributes().PutStr("repository.name", repositoryNameAttributeValue) + dp.Attributes().PutStr("branch.name", branchNameAttributeValue) } // updateCapacity saves max length of data point slices that will be used for the slice capacity. -func (m *metricGitRepositoryBranchCount) updateCapacity() { +func (m *metricGitRepositoryBranchCommitAheadbyCount) updateCapacity() { if m.data.Gauge().DataPoints().Len() > m.capacity { m.capacity = m.data.Gauge().DataPoints().Len() } } // emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points. -func (m *metricGitRepositoryBranchCount) emit(metrics pmetric.MetricSlice) { +func (m *metricGitRepositoryBranchCommitAheadbyCount) emit(metrics pmetric.MetricSlice) { if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 { m.updateCapacity() m.data.MoveTo(metrics.AppendEmpty()) @@ -54,8 +55,8 @@ func (m *metricGitRepositoryBranchCount) emit(metrics pmetric.MetricSlice) { } } -func newMetricGitRepositoryBranchCount(cfg MetricConfig) metricGitRepositoryBranchCount { - m := metricGitRepositoryBranchCount{config: cfg} +func newMetricGitRepositoryBranchCommitAheadbyCount(cfg MetricConfig) metricGitRepositoryBranchCommitAheadbyCount { + m := metricGitRepositoryBranchCommitAheadbyCount{config: cfg} if cfg.Enabled { m.data = pmetric.NewMetric() m.init() @@ -63,22 +64,22 @@ func newMetricGitRepositoryBranchCount(cfg MetricConfig) metricGitRepositoryBran return m } -type metricGitRepositoryBranchDiff struct { +type metricGitRepositoryBranchCommitBehindbyCount struct { data pmetric.Metric // data buffer for generated metric. config MetricConfig // metric config provided by user. capacity int // max observed number of data points added to the metric. } -// init fills git.repository.branch.diff metric with initial data. -func (m *metricGitRepositoryBranchDiff) init() { - m.data.SetName("git.repository.branch.diff") - m.data.SetDescription("Difference in commits between the branch and main") +// init fills git.repository.branch.commit.behindby.count metric with initial data. +func (m *metricGitRepositoryBranchCommitBehindbyCount) init() { + m.data.SetName("git.repository.branch.commit.behindby.count") + m.data.SetDescription("Number of commits the branch is behind the default branch") m.data.SetUnit("1") m.data.SetEmptyGauge() m.data.Gauge().DataPoints().EnsureCapacity(m.capacity) } -func (m *metricGitRepositoryBranchDiff) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, repositoryNameAttributeValue string, branchNameAttributeValue string) { +func (m *metricGitRepositoryBranchCommitBehindbyCount) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, repositoryNameAttributeValue string, branchNameAttributeValue string) { if !m.config.Enabled { return } @@ -91,14 +92,65 @@ func (m *metricGitRepositoryBranchDiff) recordDataPoint(start pcommon.Timestamp, } // updateCapacity saves max length of data point slices that will be used for the slice capacity. -func (m *metricGitRepositoryBranchDiff) updateCapacity() { +func (m *metricGitRepositoryBranchCommitBehindbyCount) updateCapacity() { + if m.data.Gauge().DataPoints().Len() > m.capacity { + m.capacity = m.data.Gauge().DataPoints().Len() + } +} + +// emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points. +func (m *metricGitRepositoryBranchCommitBehindbyCount) emit(metrics pmetric.MetricSlice) { + if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 { + m.updateCapacity() + m.data.MoveTo(metrics.AppendEmpty()) + m.init() + } +} + +func newMetricGitRepositoryBranchCommitBehindbyCount(cfg MetricConfig) metricGitRepositoryBranchCommitBehindbyCount { + m := metricGitRepositoryBranchCommitBehindbyCount{config: cfg} + if cfg.Enabled { + m.data = pmetric.NewMetric() + m.init() + } + return m +} + +type metricGitRepositoryBranchCount struct { + data pmetric.Metric // data buffer for generated metric. + config MetricConfig // metric config provided by user. + capacity int // max observed number of data points added to the metric. +} + +// init fills git.repository.branch.count metric with initial data. +func (m *metricGitRepositoryBranchCount) init() { + m.data.SetName("git.repository.branch.count") + m.data.SetDescription("Number of branches that exist in the repository") + m.data.SetUnit("1") + m.data.SetEmptyGauge() + m.data.Gauge().DataPoints().EnsureCapacity(m.capacity) +} + +func (m *metricGitRepositoryBranchCount) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, repositoryNameAttributeValue string) { + if !m.config.Enabled { + return + } + dp := m.data.Gauge().DataPoints().AppendEmpty() + dp.SetStartTimestamp(start) + dp.SetTimestamp(ts) + dp.SetIntValue(val) + dp.Attributes().PutStr("repository.name", repositoryNameAttributeValue) +} + +// updateCapacity saves max length of data point slices that will be used for the slice capacity. +func (m *metricGitRepositoryBranchCount) updateCapacity() { if m.data.Gauge().DataPoints().Len() > m.capacity { m.capacity = m.data.Gauge().DataPoints().Len() } } // emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points. -func (m *metricGitRepositoryBranchDiff) emit(metrics pmetric.MetricSlice) { +func (m *metricGitRepositoryBranchCount) emit(metrics pmetric.MetricSlice) { if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 { m.updateCapacity() m.data.MoveTo(metrics.AppendEmpty()) @@ -106,8 +158,8 @@ func (m *metricGitRepositoryBranchDiff) emit(metrics pmetric.MetricSlice) { } } -func newMetricGitRepositoryBranchDiff(cfg MetricConfig) metricGitRepositoryBranchDiff { - m := metricGitRepositoryBranchDiff{config: cfg} +func newMetricGitRepositoryBranchCount(cfg MetricConfig) metricGitRepositoryBranchCount { + m := metricGitRepositoryBranchCount{config: cfg} if cfg.Enabled { m.data = pmetric.NewMetric() m.init() @@ -534,8 +586,9 @@ type MetricsBuilder struct { metricsCapacity int // maximum observed number of metrics per resource. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting. buildInfo component.BuildInfo // contains version information. + metricGitRepositoryBranchCommitAheadbyCount metricGitRepositoryBranchCommitAheadbyCount + metricGitRepositoryBranchCommitBehindbyCount metricGitRepositoryBranchCommitBehindbyCount metricGitRepositoryBranchCount metricGitRepositoryBranchCount - metricGitRepositoryBranchDiff metricGitRepositoryBranchDiff metricGitRepositoryBranchTime metricGitRepositoryBranchTime metricGitRepositoryContributorCount metricGitRepositoryContributorCount metricGitRepositoryCount metricGitRepositoryCount @@ -558,12 +611,13 @@ func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption { func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder { mb := &MetricsBuilder{ - config: mbc, - startTime: pcommon.NewTimestampFromTime(time.Now()), - metricsBuffer: pmetric.NewMetrics(), - buildInfo: settings.BuildInfo, + config: mbc, + startTime: pcommon.NewTimestampFromTime(time.Now()), + metricsBuffer: pmetric.NewMetrics(), + buildInfo: settings.BuildInfo, + metricGitRepositoryBranchCommitAheadbyCount: newMetricGitRepositoryBranchCommitAheadbyCount(mbc.Metrics.GitRepositoryBranchCommitAheadbyCount), + metricGitRepositoryBranchCommitBehindbyCount: newMetricGitRepositoryBranchCommitBehindbyCount(mbc.Metrics.GitRepositoryBranchCommitBehindbyCount), metricGitRepositoryBranchCount: newMetricGitRepositoryBranchCount(mbc.Metrics.GitRepositoryBranchCount), - metricGitRepositoryBranchDiff: newMetricGitRepositoryBranchDiff(mbc.Metrics.GitRepositoryBranchDiff), metricGitRepositoryBranchTime: newMetricGitRepositoryBranchTime(mbc.Metrics.GitRepositoryBranchTime), metricGitRepositoryContributorCount: newMetricGitRepositoryContributorCount(mbc.Metrics.GitRepositoryContributorCount), metricGitRepositoryCount: newMetricGitRepositoryCount(mbc.Metrics.GitRepositoryCount), @@ -634,8 +688,9 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) { ils.Scope().SetName("otelcol/gitproviderreceiver") ils.Scope().SetVersion(mb.buildInfo.Version) ils.Metrics().EnsureCapacity(mb.metricsCapacity) + mb.metricGitRepositoryBranchCommitAheadbyCount.emit(ils.Metrics()) + mb.metricGitRepositoryBranchCommitBehindbyCount.emit(ils.Metrics()) mb.metricGitRepositoryBranchCount.emit(ils.Metrics()) - mb.metricGitRepositoryBranchDiff.emit(ils.Metrics()) mb.metricGitRepositoryBranchTime.emit(ils.Metrics()) mb.metricGitRepositoryContributorCount.emit(ils.Metrics()) mb.metricGitRepositoryCount.emit(ils.Metrics()) @@ -664,16 +719,21 @@ func (mb *MetricsBuilder) Emit(rmo ...ResourceMetricsOption) pmetric.Metrics { return metrics } +// RecordGitRepositoryBranchCommitAheadbyCountDataPoint adds a data point to git.repository.branch.commit.aheadby.count metric. +func (mb *MetricsBuilder) RecordGitRepositoryBranchCommitAheadbyCountDataPoint(ts pcommon.Timestamp, val int64, repositoryNameAttributeValue string, branchNameAttributeValue string) { + mb.metricGitRepositoryBranchCommitAheadbyCount.recordDataPoint(mb.startTime, ts, val, repositoryNameAttributeValue, branchNameAttributeValue) +} + +// RecordGitRepositoryBranchCommitBehindbyCountDataPoint adds a data point to git.repository.branch.commit.behindby.count metric. +func (mb *MetricsBuilder) RecordGitRepositoryBranchCommitBehindbyCountDataPoint(ts pcommon.Timestamp, val int64, repositoryNameAttributeValue string, branchNameAttributeValue string) { + mb.metricGitRepositoryBranchCommitBehindbyCount.recordDataPoint(mb.startTime, ts, val, repositoryNameAttributeValue, branchNameAttributeValue) +} + // RecordGitRepositoryBranchCountDataPoint adds a data point to git.repository.branch.count metric. func (mb *MetricsBuilder) RecordGitRepositoryBranchCountDataPoint(ts pcommon.Timestamp, val int64, repositoryNameAttributeValue string) { mb.metricGitRepositoryBranchCount.recordDataPoint(mb.startTime, ts, val, repositoryNameAttributeValue) } -// RecordGitRepositoryBranchDiffDataPoint adds a data point to git.repository.branch.diff metric. -func (mb *MetricsBuilder) RecordGitRepositoryBranchDiffDataPoint(ts pcommon.Timestamp, val int64, repositoryNameAttributeValue string, branchNameAttributeValue string) { - mb.metricGitRepositoryBranchDiff.recordDataPoint(mb.startTime, ts, val, repositoryNameAttributeValue, branchNameAttributeValue) -} - // RecordGitRepositoryBranchTimeDataPoint adds a data point to git.repository.branch.time metric. func (mb *MetricsBuilder) RecordGitRepositoryBranchTimeDataPoint(ts pcommon.Timestamp, val int64, repositoryNameAttributeValue string, branchNameAttributeValue string) { mb.metricGitRepositoryBranchTime.recordDataPoint(mb.startTime, ts, val, repositoryNameAttributeValue, branchNameAttributeValue) diff --git a/pkg/receiver/gitproviderreceiver/internal/metadata/generated_metrics_test.go b/pkg/receiver/gitproviderreceiver/internal/metadata/generated_metrics_test.go index 4a2aed19..bfaba283 100644 --- a/pkg/receiver/gitproviderreceiver/internal/metadata/generated_metrics_test.go +++ b/pkg/receiver/gitproviderreceiver/internal/metadata/generated_metrics_test.go @@ -56,11 +56,15 @@ func TestMetricsBuilder(t *testing.T) { defaultMetricsCount++ allMetricsCount++ - mb.RecordGitRepositoryBranchCountDataPoint(ts, 1, "repository.name-val") + mb.RecordGitRepositoryBranchCommitAheadbyCountDataPoint(ts, 1, "repository.name-val", "branch.name-val") defaultMetricsCount++ allMetricsCount++ - mb.RecordGitRepositoryBranchDiffDataPoint(ts, 1, "repository.name-val", "branch.name-val") + mb.RecordGitRepositoryBranchCommitBehindbyCountDataPoint(ts, 1, "repository.name-val", "branch.name-val") + + defaultMetricsCount++ + allMetricsCount++ + mb.RecordGitRepositoryBranchCountDataPoint(ts, 1, "repository.name-val") defaultMetricsCount++ allMetricsCount++ @@ -118,12 +122,12 @@ func TestMetricsBuilder(t *testing.T) { validatedMetrics := make(map[string]bool) for i := 0; i < ms.Len(); i++ { switch ms.At(i).Name() { - case "git.repository.branch.count": - assert.False(t, validatedMetrics["git.repository.branch.count"], "Found a duplicate in the metrics slice: git.repository.branch.count") - validatedMetrics["git.repository.branch.count"] = true + case "git.repository.branch.commit.aheadby.count": + assert.False(t, validatedMetrics["git.repository.branch.commit.aheadby.count"], "Found a duplicate in the metrics slice: git.repository.branch.commit.aheadby.count") + validatedMetrics["git.repository.branch.commit.aheadby.count"] = true assert.Equal(t, pmetric.MetricTypeGauge, ms.At(i).Type()) assert.Equal(t, 1, ms.At(i).Gauge().DataPoints().Len()) - assert.Equal(t, "Number of branches that exist in the repository", ms.At(i).Description()) + assert.Equal(t, "Number of commits the branch is ahead of the default branch", ms.At(i).Description()) assert.Equal(t, "1", ms.At(i).Unit()) dp := ms.At(i).Gauge().DataPoints().At(0) assert.Equal(t, start, dp.StartTimestamp()) @@ -133,12 +137,15 @@ func TestMetricsBuilder(t *testing.T) { attrVal, ok := dp.Attributes().Get("repository.name") assert.True(t, ok) assert.EqualValues(t, "repository.name-val", attrVal.Str()) - case "git.repository.branch.diff": - assert.False(t, validatedMetrics["git.repository.branch.diff"], "Found a duplicate in the metrics slice: git.repository.branch.diff") - validatedMetrics["git.repository.branch.diff"] = true + attrVal, ok = dp.Attributes().Get("branch.name") + assert.True(t, ok) + assert.EqualValues(t, "branch.name-val", attrVal.Str()) + case "git.repository.branch.commit.behindby.count": + assert.False(t, validatedMetrics["git.repository.branch.commit.behindby.count"], "Found a duplicate in the metrics slice: git.repository.branch.commit.behindby.count") + validatedMetrics["git.repository.branch.commit.behindby.count"] = true assert.Equal(t, pmetric.MetricTypeGauge, ms.At(i).Type()) assert.Equal(t, 1, ms.At(i).Gauge().DataPoints().Len()) - assert.Equal(t, "Difference in commits between the branch and main", ms.At(i).Description()) + assert.Equal(t, "Number of commits the branch is behind the default branch", ms.At(i).Description()) assert.Equal(t, "1", ms.At(i).Unit()) dp := ms.At(i).Gauge().DataPoints().At(0) assert.Equal(t, start, dp.StartTimestamp()) @@ -151,6 +158,21 @@ func TestMetricsBuilder(t *testing.T) { attrVal, ok = dp.Attributes().Get("branch.name") assert.True(t, ok) assert.EqualValues(t, "branch.name-val", attrVal.Str()) + case "git.repository.branch.count": + assert.False(t, validatedMetrics["git.repository.branch.count"], "Found a duplicate in the metrics slice: git.repository.branch.count") + validatedMetrics["git.repository.branch.count"] = true + assert.Equal(t, pmetric.MetricTypeGauge, ms.At(i).Type()) + assert.Equal(t, 1, ms.At(i).Gauge().DataPoints().Len()) + assert.Equal(t, "Number of branches that exist in the repository", ms.At(i).Description()) + assert.Equal(t, "1", ms.At(i).Unit()) + dp := ms.At(i).Gauge().DataPoints().At(0) + assert.Equal(t, start, dp.StartTimestamp()) + assert.Equal(t, ts, dp.Timestamp()) + assert.Equal(t, pmetric.NumberDataPointValueTypeInt, dp.ValueType()) + assert.Equal(t, int64(1), dp.IntValue()) + attrVal, ok := dp.Attributes().Get("repository.name") + assert.True(t, ok) + assert.EqualValues(t, "repository.name-val", attrVal.Str()) case "git.repository.branch.time": assert.False(t, validatedMetrics["git.repository.branch.time"], "Found a duplicate in the metrics slice: git.repository.branch.time") validatedMetrics["git.repository.branch.time"] = true diff --git a/pkg/receiver/gitproviderreceiver/internal/metadata/testdata/config.yaml b/pkg/receiver/gitproviderreceiver/internal/metadata/testdata/config.yaml index 58bc9704..6ec78483 100644 --- a/pkg/receiver/gitproviderreceiver/internal/metadata/testdata/config.yaml +++ b/pkg/receiver/gitproviderreceiver/internal/metadata/testdata/config.yaml @@ -1,9 +1,11 @@ default: all_set: metrics: - git.repository.branch.count: + git.repository.branch.commit.aheadby.count: + enabled: true + git.repository.branch.commit.behindby.count: enabled: true - git.repository.branch.diff: + git.repository.branch.count: enabled: true git.repository.branch.time: enabled: true @@ -28,9 +30,11 @@ all_set: enabled: true none_set: metrics: - git.repository.branch.count: + git.repository.branch.commit.aheadby.count: + enabled: false + git.repository.branch.commit.behindby.count: enabled: false - git.repository.branch.diff: + git.repository.branch.count: enabled: false git.repository.branch.time: enabled: false diff --git a/pkg/receiver/gitproviderreceiver/internal/scraper/githubscraper/github_scraper.go b/pkg/receiver/gitproviderreceiver/internal/scraper/githubscraper/github_scraper.go index f9a4d7d7..24057813 100644 --- a/pkg/receiver/gitproviderreceiver/internal/scraper/githubscraper/github_scraper.go +++ b/pkg/receiver/gitproviderreceiver/internal/scraper/githubscraper/github_scraper.go @@ -142,18 +142,12 @@ func (ghs *githubScraper) scrape(ctx context.Context) (pmetric.Metrics, error) { } - // TODO: End of refactor to using genqlient - - // Slightly refactoring this and making it more nested during the refactor - // to maintain parady with the original code while using genqlient and - // not having to use the original query login interspection and types - var branchCursor *string - var branches []BranchNode - if _, ok := data.(*getRepoDataBySearchResponse); ok { for _, repo := range searchRepos { var name string var defaultBranch string + var branchCursor *string + var branches []BranchNode if n, ok := repo.Node.(*SearchNodeRepository); ok { name = n.Name @@ -210,8 +204,19 @@ func (ghs *githubScraper) scrape(ctx context.Context) (pmetric.Metrics, error) { } for _, branch := range branches { - branchDiff := add(branch.Compare.AheadBy, branch.Compare.BehindBy) - ghs.mb.RecordGitRepositoryBranchDiffDataPoint(now, int64(branchDiff), name, branch.Name) + if branch.Name == defaultBranch || branch.Compare.BehindBy == 0 { + continue + } + + ghs.logger.Sugar().Debugf( + "default branch behind by: %d\n %s branch behind by: %d in repo: %s", + branch.Compare.BehindBy, branch.Name, branch.Compare.AheadBy, name) + + // Yes, this looks weird. The aheadby metric is referring to the number of commits the branch is AHEAD OF the + // default branch, which in the context of the query is the behind by value. See the above below comment about + // BehindBy vs AheadBy. + ghs.mb.RecordGitRepositoryBranchCommitAheadbyCountDataPoint(now, int64(branch.Compare.BehindBy), name, branch.Name) + ghs.mb.RecordGitRepositoryBranchCommitBehindbyCountDataPoint(now, int64(branch.Compare.AheadBy), name, branch.Name) // We're using BehindBy here because we're comparing against the target // branch, which is the default branch. In essence the response is saying @@ -220,14 +225,17 @@ func (ghs *githubScraper) scrape(ctx context.Context) (pmetric.Metrics, error) { // the default branch. Doing it this way involves less queries because // we don't have to know the queried branch name ahead of time. cp := getNumPages(float64(100), float64(branch.Compare.BehindBy)) + comCount := 100 + var cc *string for i := 0; i < cp; i++ { - if branch.Name == defaultBranch || branch.Compare.BehindBy == 0 { - break + + if i == cp-1 { + comCount = branch.Compare.BehindBy % 100 } - c, err := getCommitData(ctx, genClient, name, ghs.cfg.GitHubOrg, 1, 100, cc, branch.Name) + c, err := getCommitData(ctx, genClient, name, ghs.cfg.GitHubOrg, 1, comCount, cc, branch.Name) if err != nil { ghs.logger.Sugar().Errorf("error getting commit data", zap.Error(err)) } diff --git a/pkg/receiver/gitproviderreceiver/metadata.yaml b/pkg/receiver/gitproviderreceiver/metadata.yaml index 71a1d6e0..d1f6ae1c 100644 --- a/pkg/receiver/gitproviderreceiver/metadata.yaml +++ b/pkg/receiver/gitproviderreceiver/metadata.yaml @@ -59,9 +59,17 @@ metrics: gauge: value_type: int attributes: [repository.name, branch.name] - git.repository.branch.diff: + # may need to be separate metrics + git.repository.branch.commit.aheadby.count: enabled: true - description: Difference in commits between the branch and main + description: Number of commits the branch is ahead of the default branch + unit: 1 + gauge: + value_type: int + attributes: [repository.name, branch.name] + git.repository.branch.commit.behindby.count: + enabled: true + description: Number of commits the branch is behind the default branch unit: 1 gauge: value_type: int