Skip to content

Commit

Permalink
Memcached Receiver Skeleton (#1414)
Browse files Browse the repository at this point in the history
This is the start of a basic memcached receiver.  It scrapes some basic
metrics from a memcached instance and sends them along.  The primary
test of the metric generation logic is in the integration test due to
the relative opacity of the memcached protocol and the lack of any
preexisting mock (the integration test uses a real memcached instance
via Docker containers).

There are only five basic metrics to start with but this can be easily
expanded with this framework.
  • Loading branch information
benkeith-splunk committed Nov 10, 2020
1 parent 6e84f52 commit 8662fd6
Show file tree
Hide file tree
Showing 14 changed files with 2,199 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/dockerstatsreceiver v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/memcachedreceiver v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusexecreceiver v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/receivercreator v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver v0.0.0-00010101000000-000000000000
Expand Down Expand Up @@ -157,6 +158,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/processor/grou

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/stanzareceiver => ./receiver/stanzareceiver/

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/memcachedreceiver => ./receiver/memcachedreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sprocessor => ./processor/k8sprocessor/

replace github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor => ./processor/resourcedetectionprocessor/
Expand Down
1 change: 1 addition & 0 deletions receiver/memcachedreceiver/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
39 changes: 39 additions & 0 deletions receiver/memcachedreceiver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Memcached Receiver

This receiver can fetch stats from a Memcached instance using the [stats
command](https://github.com/memcached/memcached/wiki/Commands#statistics). A
detailed description of all the stats available is at
https://github.com/memcached/memcached/blob/master/doc/protocol.txt#L1159.

> :construction: This receiver is currently in **BETA**.
## Details

## Configuration

> :information_source: This receiver is in beta and configuration fields are subject to change.
The following settings are required:

- `endpoint` (default: `localhost:11211`): The hostname/IP address and port of the memcached instance

The following settings are optional:

- `collection_interval` (default = `10s`): This receiver runs on an interval.
Each time it runs, it queries memcached, creates metrics, and sends them to the
next consumer. The `collection_interval` configuration option tells this
receiver the duration between runs. This value must be a string readable by
Golang's `ParseDuration` function (example: `1h30m`). Valid time units are
`ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`.

Example:

```yaml
receivers:
memcached:
endpoint: "localhost:11211"
collection_interval: 10s
```

The full list of settings exposed for this receiver are documented [here](./config.go)
with detailed sample configurations [here](./testdata/config.yaml).
32 changes: 32 additions & 0 deletions receiver/memcachedreceiver/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2020, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package memcachedreceiver

import (
"time"

"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/receiver/receiverhelper"
)

type config struct {
configmodels.ReceiverSettings `mapstructure:",squash"`
receiverhelper.ScraperControllerSettings `mapstructure:",squash"`
confignet.TCPAddr `mapstructure:",squash"`

// Timeout for the memcache stats request
Timeout time.Duration `mapstructure:"timeout"`
}
72 changes: 72 additions & 0 deletions receiver/memcachedreceiver/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2020, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package memcachedreceiver

//go:generate mdatagen metadata.yaml

import (
"context"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver/receiverhelper"
)

const (
typeStr = "memcached"
)

// NewFactory creates a factory for memcached receiver.
func NewFactory() component.ReceiverFactory {
return receiverhelper.NewFactory(
typeStr,
createDefaultConfig,
receiverhelper.WithMetrics(createMetricsReceiver))
}

func createDefaultConfig() configmodels.Receiver {
return &config{
ReceiverSettings: configmodels.ReceiverSettings{
TypeVal: typeStr,
NameVal: typeStr,
},
ScraperControllerSettings: receiverhelper.ScraperControllerSettings{
CollectionInterval: 10 * time.Second,
},
Timeout: 10 * time.Second,
TCPAddr: confignet.TCPAddr{
Endpoint: "localhost:11211",
},
}
}

func createMetricsReceiver(
ctx context.Context,
params component.ReceiverCreateParams,
rConf configmodels.Receiver,
consumer consumer.MetricsConsumer,
) (component.MetricsReceiver, error) {
cfg := rConf.(*config)

scraper := newMemcachedScraper(params.Logger, cfg)

return receiverhelper.NewScraperControllerReceiver(
&cfg.ScraperControllerSettings, consumer,
receiverhelper.AddResourceMetricsScraper(scraper),
)
}
56 changes: 56 additions & 0 deletions receiver/memcachedreceiver/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2020, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package memcachedreceiver

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configcheck"
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.opentelemetry.io/collector/testbed/testbed"
"go.uber.org/zap"
)

func TestType(t *testing.T) {
factory := NewFactory()
ft := factory.Type()
require.EqualValues(t, "memcached", ft)
}

func TestValidConfig(t *testing.T) {
factory := NewFactory()
err := configcheck.ValidateConfig(factory.CreateDefaultConfig())
require.NoError(t, err)
}

func TestCreateMetricsReceiver(t *testing.T) {
factory := NewFactory()
metricsReceiver, err := factory.CreateMetricsReceiver(
context.Background(),
component.ReceiverCreateParams{Logger: zap.NewNop()},
&config{
ScraperControllerSettings: receiverhelper.ScraperControllerSettings{
CollectionInterval: 10 * time.Second,
},
},
&testbed.MockMetricConsumer{},
)
require.NoError(t, err)
require.NotNil(t, metricsReceiver)
}
18 changes: 18 additions & 0 deletions receiver/memcachedreceiver/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/memcachedreceiver

go 1.14

require (
github.com/census-instrumentation/opencensus-proto v0.3.0
github.com/davecgh/go-spew v1.1.1
github.com/go-redis/redis/v7 v7.4.0
github.com/grobie/gomemcache v0.0.0-20180201122607-1f779c573665
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.0.0-00010101000000-000000000000
github.com/stretchr/testify v1.6.1
go.opentelemetry.io/collector v0.13.1-0.20201027215027-6ae66159741d
go.uber.org/zap v1.16.0
google.golang.org/api v0.32.0
google.golang.org/protobuf v1.25.0
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common
Loading

0 comments on commit 8662fd6

Please sign in to comment.