Skip to content

Commit

Permalink
[receiver/couchdbreceiver] Fix couchdbreceiver not allowing user metr…
Browse files Browse the repository at this point in the history
…ics configuration (open-telemetry#9728)

* fix issue where receiver/couchdbreceiver did not allow user specified metrics

* include issue in changelog entry

* fix import order
  • Loading branch information
schmikei authored and djaglowski committed May 10, 2022
1 parent c68688b commit 1aec8bd
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- `opencensusreceiver`: Do not report fatal error if err is server closed (#9559).
- `sqlserverreceiver`: Fix the receiver to have integer types on metrics where applicable (#9601)
- `prometheusreceiver`: Fix the memory issue introduced in the 0.49.0 release (#9718)
- `couchdbreceiver`: Fix issue where the receiver would not respect custom metric settings (#9598)
- `nginxreceiver`: Include nginxreceiver in components (#9572)

## v0.50.0
Expand Down
7 changes: 5 additions & 2 deletions receiver/couchdbreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/multierr"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/couchdbreceiver/internal/metadata"
)

const defaultEndpoint = "http://localhost:5984"
Expand All @@ -39,8 +41,9 @@ var (
type Config struct {
scraperhelper.ScraperControllerSettings `mapstructure:",squash"`
confighttp.HTTPClientSettings `mapstructure:",squash"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Metrics metadata.MetricsSettings `mapstructure:"metrics"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
}

// Validate validates missing and invalid configuration fields.
Expand Down
4 changes: 4 additions & 0 deletions receiver/couchdbreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ import (
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver/scraperhelper"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/couchdbreceiver/internal/metadata"
)

const (
typeStr = "couchdb"
)

// NewFactory creates the couchdbreceiver factory
func NewFactory() component.ReceiverFactory {
return component.NewReceiverFactory(
typeStr,
Expand All @@ -39,6 +42,7 @@ func NewFactory() component.ReceiverFactory {

func createDefaultConfig() config.Receiver {
return &Config{
Metrics: metadata.DefaultMetricsSettings(),
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
CollectionInterval: 1 * time.Minute,
Expand Down
2 changes: 1 addition & 1 deletion receiver/couchdbreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func newCouchdbScraper(settings component.TelemetrySettings, config *Config) *co
return &couchdbScraper{
settings: settings,
config: config,
mb: metadata.NewMetricsBuilder(metadata.DefaultMetricsSettings()),
mb: metadata.NewMetricsBuilder(config.Metrics),
}
}

Expand Down
31 changes: 31 additions & 0 deletions receiver/couchdbreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/confighttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/couchdbreceiver/internal/metadata"
)

func TestScrape(t *testing.T) {
Expand Down Expand Up @@ -132,6 +134,35 @@ func TestStart(t *testing.T) {
})
}

func TestMetricSettings(t *testing.T) {
mockClient := new(MockClient)
mockClient.On("GetStats", "_local").Return(getStats("response_2.31.json"))
cfg := &Config{
HTTPClientSettings: confighttp.HTTPClientSettings{},
Metrics: metadata.MetricsSettings{
CouchdbAverageRequestTime: metadata.MetricSettings{Enabled: false},
CouchdbDatabaseOpen: metadata.MetricSettings{Enabled: false},
CouchdbDatabaseOperations: metadata.MetricSettings{Enabled: true},
CouchdbFileDescriptorOpen: metadata.MetricSettings{Enabled: false},
CouchdbHttpdBulkRequests: metadata.MetricSettings{Enabled: false},
CouchdbHttpdRequests: metadata.MetricSettings{Enabled: false},
CouchdbHttpdResponses: metadata.MetricSettings{Enabled: false},
CouchdbHttpdViews: metadata.MetricSettings{Enabled: false},
},
}
scraper := newCouchdbScraper(componenttest.NewNopTelemetrySettings(), cfg)
scraper.client = mockClient

metrics, err := scraper.scrape(context.Background())
require.NoError(t, err)

expected, err := golden.ReadMetrics(filepath.Join("testdata", "scraper", "only_db_ops.json"))
require.NoError(t, err)

require.NoError(t, scrapertest.CompareMetrics(expected, metrics))
require.Equal(t, metrics.MetricCount(), 1)
}

func getStats(filename string) (map[string]interface{}, error) {
var stats map[string]interface{}

Expand Down
62 changes: 62 additions & 0 deletions receiver/couchdbreceiver/testdata/scraper/only_db_ops.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"resourceMetrics": [
{
"resource": {
"attributes": [
{
"key": "couchdb.node.name",
"value": {
"stringValue": ""
}
}
]
},
"scopeMetrics": [
{
"scope": {
"name": "otelcol/couchdbreceiver"
},
"metrics": [
{
"name": "couchdb.database.operations",
"description": "The number of database operations.",
"unit": "{operations}",
"sum": {
"dataPoints": [
{
"attributes": [
{
"key": "operation",
"value": {
"stringValue": "reads"
}
}
],
"startTimeUnixNano": "1651674345801839000",
"timeUnixNano": "1651674345801933000",
"asInt": "38"
},
{
"attributes": [
{
"key": "operation",
"value": {
"stringValue": "writes"
}
}
],
"startTimeUnixNano": "1651674345801839000",
"timeUnixNano": "1651674345801933000",
"asInt": "39"
}
],
"aggregationTemporality": "AGGREGATION_TEMPORALITY_CUMULATIVE",
"isMonotonic": true
}
}
]
}
]
}
]
}

0 comments on commit 1aec8bd

Please sign in to comment.