Skip to content

Commit

Permalink
Change featuregate usages to the new Register API (#16257)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Nov 11, 2022
1 parent 59b487b commit 9166107
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 86 deletions.
14 changes: 5 additions & 9 deletions exporter/datadogexporter/internal/metadata/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,12 @@ const (
HostnamePreviewFeatureGate = "exporter.datadog.hostname.preview"
)

var (
HostnamePreviewGate = featuregate.Gate{
ID: HostnamePreviewFeatureGate,
Description: "Use the 'preview' hostname resolution rules, which are consistent with Datadog cloud integration hostname resolution rules, and set 'host_metadata::hostname_source' to 'config_or_system' by default.",
Enabled: true,
}
)

func init() {
featuregate.GetRegistry().MustRegister(HostnamePreviewGate)
featuregate.GetRegistry().MustRegisterID(
HostnamePreviewFeatureGate,
featuregate.StageBeta,
featuregate.WithRegisterDescription("Use the 'preview' hostname resolution rules, which are consistent with Datadog cloud integration hostname resolution rules, and set 'host_metadata::hostname_source' to 'config_or_system' by default."),
)
}

func buildPreviewProvider(set component.TelemetrySettings, configHostname string) (source.Provider, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func TestMetadataFromAttributes(t *testing.T) {
for _, testInstance := range tests {
t.Run(testInstance.name, func(t *testing.T) {
registry := featuregate.NewRegistry()
registry.MustRegister(HostnamePreviewGate)
registry.MustRegisterID(HostnamePreviewFeatureGate, featuregate.StageBeta)
require.NoError(t, registry.Apply(map[string]bool{HostnamePreviewFeatureGate: testInstance.usePreviewHostnameLogic}))
metadata := metadataFromAttributesWithRegistry(registry, testInstance.attrs)
assert.Equal(t, testInstance.expected.InternalHostname, metadata.InternalHostname)
Expand Down
2 changes: 1 addition & 1 deletion exporter/datadogexporter/traces_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestTracesSource(t *testing.T) {
assert := assert.New(t)
params := componenttest.NewNopExporterCreateSettings()
reg := featuregate.NewRegistry()
reg.MustRegister(metadata.HostnamePreviewGate)
reg.MustRegisterID(metadata.HostnamePreviewFeatureGate, featuregate.StageBeta)
assert.NoError(reg.Apply(map[string]bool{
metadata.HostnamePreviewFeatureGate: true,
}))
Expand Down
10 changes: 5 additions & 5 deletions exporter/googlecloudexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ const (
)

func init() {
featuregate.GetRegistry().MustRegister(featuregate.Gate{
ID: pdataExporterFeatureGate,
Description: "When enabled, the googlecloud exporter translates pdata directly to google cloud monitoring's types, rather than first translating to opencensus.",
Enabled: true,
})
featuregate.GetRegistry().MustRegisterID(
pdataExporterFeatureGate,
featuregate.StageBeta,
featuregate.WithRegisterDescription("When enabled, the googlecloud exporter translates pdata directly to google cloud monitoring's types, rather than first translating to opencensus."),
)
}

// NewFactory creates a factory for the googlecloud exporter
Expand Down
14 changes: 7 additions & 7 deletions pkg/translator/prometheus/normalize_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
"go.opentelemetry.io/collector/featuregate"
)

var dropSanitizationGate = featuregate.Gate{
ID: "pkg.translator.prometheus.PermissiveLabelSanitization",
Enabled: false,
Description: "Controls whether to change labels starting with '_' to 'key_'",
}
const dropSanitizationGate = "pkg.translator.prometheus.PermissiveLabelSanitization"

func init() {
featuregate.GetRegistry().MustRegister(dropSanitizationGate)
featuregate.GetRegistry().MustRegisterID(
dropSanitizationGate,
featuregate.StageAlpha,
featuregate.WithRegisterDescription("Controls whether to change labels starting with '_' to 'key_'."),
)
}

// Normalizes the specified label to follow Prometheus label names standard
Expand All @@ -51,7 +51,7 @@ func NormalizeLabel(label string) string {
// If label starts with a number, prepend with "key_"
if unicode.IsDigit(rune(label[0])) {
label = "key_" + label
} else if strings.HasPrefix(label, "_") && !strings.HasPrefix(label, "__") && !featuregate.GetRegistry().IsEnabled(dropSanitizationGate.GetID()) {
} else if strings.HasPrefix(label, "_") && !strings.HasPrefix(label, "__") && !featuregate.GetRegistry().IsEnabled(dropSanitizationGate) {
label = "key" + label
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/translator/prometheus/normalize_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func TestSanitize(t *testing.T) {

defer testutil.SetFeatureGateForTest(t, dropSanitizationGate.GetID(), false)()
defer testutil.SetFeatureGateForTest(t, dropSanitizationGate, false)()

require.Equal(t, "", NormalizeLabel(""), "")
require.Equal(t, "key_test", NormalizeLabel("_test"))
Expand All @@ -36,7 +36,7 @@ func TestSanitize(t *testing.T) {

func TestSanitizeDropSanitization(t *testing.T) {

defer testutil.SetFeatureGateForTest(t, dropSanitizationGate.GetID(), true)()
defer testutil.SetFeatureGateForTest(t, dropSanitizationGate, true)()

require.Equal(t, "", NormalizeLabel(""))
require.Equal(t, "_test", NormalizeLabel("_test"))
Expand Down
14 changes: 7 additions & 7 deletions pkg/translator/prometheus/normalize_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ var perUnitMap = map[string]string{
"y": "year",
}

var normalizeNameGate = featuregate.Gate{
ID: "pkg.translator.prometheus.NormalizeName",
Enabled: false,
Description: "Controls whether metrics names are automatically normalized to follow Prometheus naming convention",
}
const normalizeNameGateID = "pkg.translator.prometheus.NormalizeName"

func init() {
// Register the feature gates
featuregate.GetRegistry().MustRegister(normalizeNameGate)
featuregate.GetRegistry().MustRegisterID(
normalizeNameGateID,
featuregate.StageAlpha,
featuregate.WithRegisterDescription("Controls whether metrics names are automatically normalized to follow Prometheus naming convention"),
)
}

// Build a Prometheus-compliant metric name for the specified metric
Expand All @@ -105,7 +105,7 @@ func BuildPromCompliantName(metric pmetric.Metric, namespace string) string {
var metricName string

// Full normalization following standard Prometheus naming conventions
if featuregate.GetRegistry().IsEnabled(normalizeNameGate.GetID()) {
if featuregate.GetRegistry().IsEnabled(normalizeNameGateID) {
return normalizeName(metric, namespace)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/translator/prometheus/normalize_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestRemoveItem(t *testing.T) {

func TestBuildPromCompliantNameWithNormalize(t *testing.T) {

defer testutil.SetFeatureGateForTest(t, normalizeNameGate.GetID(), true)()
defer testutil.SetFeatureGateForTest(t, normalizeNameGateID, true)()
require.Equal(t, "system_io_bytes_total", BuildPromCompliantName(createCounter("system.io", "By"), ""))
require.Equal(t, "system_network_io_bytes_total", BuildPromCompliantName(createCounter("network.io", "By"), "system"))
require.Equal(t, "_3_14_digits", BuildPromCompliantName(createGauge("3.14 digits", ""), ""))
Expand All @@ -202,7 +202,7 @@ func TestBuildPromCompliantNameWithNormalize(t *testing.T) {

func TestBuildPromCompliantNameWithoutNormalize(t *testing.T) {

defer testutil.SetFeatureGateForTest(t, normalizeNameGate.GetID(), false)()
defer testutil.SetFeatureGateForTest(t, normalizeNameGateID, false)()
require.Equal(t, "system_io", BuildPromCompliantName(createCounter("system.io", "By"), ""))
require.Equal(t, "system_network_io", BuildPromCompliantName(createCounter("network.io", "By"), "system"))
require.Equal(t, "system_network_I_O", BuildPromCompliantName(createCounter("network (I/O)", "By"), "system"))
Expand Down
8 changes: 1 addition & 7 deletions processor/cumulativetodeltaprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@ import (

const enableHistogramSupportGateID = "processor.cumulativetodeltaprocessor.EnableHistogramSupport"

var enableHistogramSupportGate = featuregate.Gate{
ID: enableHistogramSupportGateID,
Enabled: true,
Description: "Enables histogram conversion support",
}

func init() {
featuregate.GetRegistry().MustRegister(enableHistogramSupportGate)
featuregate.GetRegistry().MustRegisterID(enableHistogramSupportGateID, featuregate.StageBeta, featuregate.WithRegisterDescription("Enables histogram conversion support"))
}

type cumulativeToDeltaProcessor struct {
Expand Down
27 changes: 10 additions & 17 deletions receiver/apachereceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,19 @@ const (
readmeURL = "https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/receiver/apachereceiver/README.md"
EmitServerNameAsResourceAttribute = "receiver.apache.emitServerNameAsResourceAttribute"
EmitPortAsResourceAttribute = "receiver.apache.emitPortAsResourceAttribute"
featureGateWarning = "Feature gate %s is not enabled. Please see the README.md file of apache receiver for more information."
)

var (
emitServerNameAsResourceAttribute = featuregate.Gate{
ID: EmitServerNameAsResourceAttribute,
Enabled: false,
Description: "When enabled, the name of the server will be sent as an apache.server.name resource attribute " +
"instead of a metric-level server_name attribute.",
}
emitPortAsResourceAttribute = featuregate.Gate{
ID: EmitPortAsResourceAttribute,
Enabled: false,
Description: "When enabled, the port of the server will be sent as an apache.server.name resource attribute.",
}
)

func init() {
featuregate.GetRegistry().MustRegister(emitServerNameAsResourceAttribute)
featuregate.GetRegistry().MustRegister(emitPortAsResourceAttribute)
featuregate.GetRegistry().MustRegisterID(
EmitServerNameAsResourceAttribute,
featuregate.StageAlpha,
featuregate.WithRegisterDescription("When enabled, the name of the server will be sent as an apache.server.name resource attribute instead of a metric-level server_name attribute."),
)
featuregate.GetRegistry().MustRegisterID(
EmitPortAsResourceAttribute,
featuregate.StageAlpha,
featuregate.WithRegisterDescription("When enabled, the port of the server will be sent as an apache.server.name resource attribute."),
)
}

type apacheScraper struct {
Expand Down
10 changes: 5 additions & 5 deletions receiver/dockerstatsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ const (
)

func init() {
featuregate.GetRegistry().MustRegister(featuregate.Gate{
ID: useScraperV2ID,
Description: "When enabled, the receiver will use the function ScrapeV2 to collect metrics. This allows each metric to be turned off/on via config. The new metrics are slightly different to the legacy implementation.",
Enabled: false,
})
featuregate.GetRegistry().MustRegisterID(
useScraperV2ID,
featuregate.StageAlpha,
featuregate.WithRegisterDescription("When enabled, the receiver will use the function ScrapeV2 to collect metrics. This allows each metric to be turned off/on via config. The new metrics are slightly different to the legacy implementation."),
)
}

func NewFactory() component.ReceiverFactory {
Expand Down
40 changes: 17 additions & 23 deletions receiver/postgresqlreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,27 @@ import (
)

const (
emitMetricsWithResourceAttributesFeatureGateID = "receiver.postgresql.emitMetricsWithResourceAttributes"
emitMetricsWithResourceAttributesFeatureGateID = "receiver.postgresql.emitMetricsWithResourceAttributes"
emitMetricsWithoutResourceAttributesDescription = "Postgresql metrics are transitioning from being reported with identifying metric attributes " +
"to being identified via resource attributes in order to fit the OpenTelemetry specification. This feature " +
"gate controls emitting the old metrics without resource attributes. For more details, see: " +
"https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/receiver/postgresqlreceiver/README.md#feature-gate-configurations"
emitMetricsWithoutResourceAttributesFeatureGateID = "receiver.postgresql.emitMetricsWithoutResourceAttributes"
)

var (
emitMetricsWithoutResourceAttributes = featuregate.Gate{
ID: emitMetricsWithoutResourceAttributesFeatureGateID,
Enabled: false,
Description: "Postgresql metrics are transitioning from being reported with identifying metric attributes " +
"to being identified via resource attributes in order to fit the OpenTelemetry specification. This feature " +
"gate controls emitting the old metrics without resource attributes. For more details, see: " +
"https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/receiver/postgresqlreceiver/README.md#feature-gate-configurations",
}

emitMetricsWithResourceAttributes = featuregate.Gate{
ID: emitMetricsWithResourceAttributesFeatureGateID,
Enabled: true,
Description: "Postgresql metrics are transitioning from being reported with identifying metric attributes " +
"to being identified via resource attributes in order to fit the OpenTelemetry specification. This feature " +
"gate controls emitting the new metrics with resource attributes. For more details, see: " +
"https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/receiver/postgresqlreceiver/README.md#feature-gate-configurations",
}
emitMetricsWithResourceAttributesDescription = "Postgresql metrics are transitioning from being reported with identifying metric attributes " +
"to being identified via resource attributes in order to fit the OpenTelemetry specification. This feature " +
"gate controls emitting the new metrics with resource attributes. For more details, see: " +
"https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/receiver/postgresqlreceiver/README.md#feature-gate-configurations"
)

func init() {
featuregate.GetRegistry().MustRegister(emitMetricsWithoutResourceAttributes)
featuregate.GetRegistry().MustRegister(emitMetricsWithResourceAttributes)
featuregate.GetRegistry().MustRegisterID(
emitMetricsWithoutResourceAttributesFeatureGateID,
featuregate.StageAlpha,
featuregate.WithRegisterDescription(emitMetricsWithoutResourceAttributesDescription))
featuregate.GetRegistry().MustRegisterID(
emitMetricsWithResourceAttributesFeatureGateID,
featuregate.StageBeta,
featuregate.WithRegisterDescription(emitMetricsWithResourceAttributesDescription))
}

type postgreSQLScraper struct {
Expand Down

0 comments on commit 9166107

Please sign in to comment.