Skip to content

Commit

Permalink
fix: add 'receiver.mongodb.removeDatabaseResourceAttr' feature gate
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulali committed Nov 21, 2023
1 parent a0fa510 commit 60b8806
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .chloggen/mongodbreceiver-duplicate-attribute-24972.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ change_type: 'bug_fix'
component: mongodbreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Remove duplicate database name attribute, database name reported as a datapoint attribute and as a resource attribute, one of them should be removed
note: "add `receiver.mongodb.removeDatabaseResourceAttr` Alpha feature gate to remove duplicate database name attribute"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [24972]
Expand Down
12 changes: 12 additions & 0 deletions receiver/mongodbreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ The following metric are available with versions:
- `mongodb.index.access.count` >= 4.0

Details about the metrics produced by this receiver can be found in [metadata.yaml](./metadata.yaml)

## Feature gate configurations
See the [Collector feature gates](https://github.com/open-telemetry/opentelemetry-collector/blob/main/featuregate/README.md#collector-feature-gates) for an overview of feature gates in the collector.

**ALPHA**: `receiver.mongodb.removeDatabaseResourceAttr`

The feature gate `receiver.mongodb.removeDatabaseResourceAttr` once enabled will remove database name resource attribute,
because both resource and datapoint attributes are called database.

This feature gate will eventually be enabled by default, and eventually the old implementation will be removed. It aims
to give users time to migrate to the new implementation. The target release for this featuregate to be enabled by default
is 0.93.0.
6 changes: 6 additions & 0 deletions receiver/mongodbreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,9 @@ The amount of time that the server has been running.
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| ms | Sum | Int | Cumulative | true |
## Resource Attributes
| Name | Description | Values | Enabled |
| ---- | ----------- | ------ | ------- |
| database | The name of a database. | Any Str | false |
38 changes: 36 additions & 2 deletions receiver/mongodbreceiver/internal/metadata/generated_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions receiver/mongodbreceiver/internal/metadata/generated_resource.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ all_set:
enabled: true
mongodb.uptime:
enabled: true
resource_attributes:
database:
enabled: true
none_set:
metrics:
mongodb.cache.operations:
Expand Down Expand Up @@ -123,3 +126,6 @@ none_set:
enabled: false
mongodb.uptime:
enabled: false
resource_attributes:
database:
enabled: false
8 changes: 8 additions & 0 deletions receiver/mongodbreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ status:
codeowners:
active: [djaglowski, schmikei]

resource_attributes:
database:
description: The name of a database.
enabled: false
type: string
warnings:
if_configured: This resource_attribute is deprecated and will be removed soon.

attributes:
database:
description: The name of a database.
Expand Down
49 changes: 42 additions & 7 deletions receiver/mongodbreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-version"
"go.mongodb.org/mongo-driver/bson"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
Expand All @@ -21,23 +22,49 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbreceiver/internal/metadata"
)

var unknownVersion = func() *version.Version { return version.Must(version.NewVersion("0.0")) }
const (
readmeURL = "https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/receiver/mongodbreceiver/README.md"
removeDatabaseResourceAttrID = "receiver.mongodb.removeDatabaseResourceAttr"
)

var (
unknownVersion = func() *version.Version { return version.Must(version.NewVersion("0.0")) }

removeDatabaseResourceAttrFeatureGate = featuregate.GlobalRegistry().MustRegister(
removeDatabaseResourceAttrID,
featuregate.StageAlpha,
featuregate.WithRegisterDescription("Remove duplicate database name resource attribute"),
featuregate.WithRegisterReferenceURL("https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/24972"),
featuregate.WithRegisterFromVersion("v0.88.0"))
)

type mongodbScraper struct {
logger *zap.Logger
config *Config
client client
mongoVersion *version.Version
mb *metadata.MetricsBuilder

// removeDatabaseResourceAttr if enabled, will remove database resource attribute on database metrics
removeDatabaseResourceAttr bool
}

func newMongodbScraper(settings receiver.CreateSettings, config *Config) *mongodbScraper {
return &mongodbScraper{
logger: settings.Logger,
config: config,
mb: metadata.NewMetricsBuilder(config.MetricsBuilderConfig, settings),
mongoVersion: unknownVersion(),
ms := &mongodbScraper{
logger: settings.Logger,
config: config,
mb: metadata.NewMetricsBuilder(config.MetricsBuilderConfig, settings),
mongoVersion: unknownVersion(),
removeDatabaseResourceAttr: removeDatabaseResourceAttrFeatureGate.IsEnabled(),
}

if !ms.removeDatabaseResourceAttr {
settings.Logger.Warn(
fmt.Sprintf("Feature gate %s is not enabled. Please see the README for more information: %s", removeDatabaseResourceAttrID, readmeURL),
)
}

return ms
}

func (s *mongodbScraper) start(ctx context.Context, _ component.Host) error {
Expand Down Expand Up @@ -116,7 +143,15 @@ func (s *mongodbScraper) collectDatabase(ctx context.Context, now pcommon.Timest
return
}
s.recordNormalServerStats(now, serverStatus, databaseName, errs)
s.mb.EmitForResource()

emitWith := []metadata.ResourceMetricsOption{}
if !s.removeDatabaseResourceAttr {
rb := s.mb.NewResourceBuilder()
rb.SetDatabase(databaseName)
emitWith = append(emitWith, metadata.WithResource(rb.Emit()))
}

s.mb.EmitForResource(emitWith...)
}

func (s *mongodbScraper) collectAdminDatabase(ctx context.Context, now pcommon.Timestamp, errs *scrapererror.ScrapeErrors) {
Expand Down

0 comments on commit 60b8806

Please sign in to comment.