Skip to content

Commit 107e5f3

Browse files
authored
Add kafkametricsreceiver initial structure (#2550)
Adds the factory, config and receiver for new (kafkametricsreceiver) component. The new kafkametricsreceiver will primarily report metrics on consumer_group lag from kafka (using the internal __consumer_offsets topic). It will also include metrics on number of brokers, topics, and offsets of topics. Testing: The current code is a scaffolding that cannot be e2e tested, appropriate unit tests were added. Documentation: README explaining usage of new receiver component is included.
1 parent 0d64efb commit 107e5f3

File tree

11 files changed

+1897
-0
lines changed

11 files changed

+1897
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ../../Makefile.Common
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Kafka Metrics Receiver
2+
3+
Kafka metrics receiver collects kafka metrics (brokers, topics, partitions, consumer groups) from kafka server,
4+
converting into otlp.
5+
6+
## Getting Started
7+
8+
Required settings (no defaults):
9+
10+
- `protocol_version`: Kafka protocol version
11+
- `scrapers`: any combination of the following scrapers can be enabled.
12+
- `topics`
13+
- `consumers`
14+
- `brokers`
15+
16+
Optional Settings (with defaults):
17+
18+
- `brokers` (default = localhost:9092): the list of brokers to read from.
19+
- `topic_match` (default = *): regex pattern of topics to filter for metrics collection.
20+
- `group_match` (default = *): regex pattern of consumer groups to filter on for metrics.
21+
- `client_id` (default = otel-metrics-receiver): consumer client id
22+
- `collection_interval` (default = 1m): frequency of metric collection/scraping.
23+
- `auth` (default none)
24+
- `plain_text`
25+
- `username`: The username to use.
26+
- `password`: The password to use
27+
- `tls`
28+
- `ca_file`: path to the CA cert. For a client this verifies the server certificate. Should only be used
29+
if `insecure` is set to true.
30+
- `cert_file`: path to the TLS cert to use for TLS required connections. Should only be used if `insecure` is
31+
set to true.
32+
- `key_file`: path to the TLS key to use for TLS required connections. Should only be used if `insecure` is set
33+
to true.
34+
- `insecure` (default = false): Disable verifying the server's certificate chain and host
35+
name (`InsecureSkipVerify` in the tls config)
36+
- `server_name_override`: ServerName indicates the name of the server requested by the client in order to
37+
support virtual hosting.
38+
- `kerberos`
39+
- `service_name`: Kerberos service name
40+
- `realm`: Kerberos realm
41+
- `use_keytab`: Use of keytab instead of password, if this is true, keytab file will be used instead of
42+
password
43+
- `username`: The Kerberos username used for authenticate with KDC
44+
- `password`: The Kerberos password used for authenticate with KDC
45+
- `config_file`: Path to Kerberos configuration. i.e /etc/krb5.conf
46+
- `keytab_file`: Path to keytab file. i.e /etc/security/kafka.keytab
47+
48+
## Examples:
49+
50+
Basic configuration with all scrapers:
51+
52+
```yaml
53+
receivers:
54+
kafkametrics:
55+
protocol_version: 2.0.0
56+
scrapers:
57+
- brokers
58+
- topics
59+
- consumers
60+
```
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package kafkametricsreceiver
16+
17+
import (
18+
"go.opentelemetry.io/collector/exporter/kafkaexporter"
19+
"go.opentelemetry.io/collector/receiver/scraperhelper"
20+
)
21+
22+
// Config represents user settings for kafkametrics receiver
23+
type Config struct {
24+
scraperhelper.ScraperControllerSettings `mapstructure:",squash"`
25+
26+
// The list of kafka brokers (default localhost:9092)
27+
Brokers []string `mapstructure:"brokers"`
28+
29+
// ProtocolVersion Kafka protocol version
30+
ProtocolVersion string `mapstructure:"protocol_version"`
31+
32+
// TopicMatch topics to collect metrics on
33+
TopicMatch string `mapstructure:"topic_match"`
34+
35+
// GroupMatch consumer groups to collect on
36+
GroupMatch string `mapstructure:"group_match"`
37+
38+
// Authentication data
39+
Authentication kafkaexporter.Authentication `mapstructure:"auth"`
40+
41+
// Scrapers defines which metric data points to be captured from kafka
42+
Scrapers []string `mapstructure:"scrapers"`
43+
44+
// ClientID is the id associated with the consumer that reads from topics in kafka.
45+
ClientID string `mapstructure:"client_id"`
46+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package kafkametricsreceiver
16+
17+
import (
18+
"path"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/require"
23+
"go.opentelemetry.io/collector/component/componenttest"
24+
"go.opentelemetry.io/collector/config/configtest"
25+
"go.opentelemetry.io/collector/config/configtls"
26+
"go.opentelemetry.io/collector/exporter/kafkaexporter"
27+
"go.opentelemetry.io/collector/receiver/scraperhelper"
28+
)
29+
30+
func TestLoadConfig(t *testing.T) {
31+
factories, err := componenttest.ExampleComponents()
32+
assert.NoError(t, err)
33+
34+
factory := NewFactory()
35+
factories.Receivers[typeStr] = factory
36+
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
37+
require.NoError(t, err)
38+
require.Equal(t, 1, len(cfg.Receivers))
39+
40+
r := cfg.Receivers[typeStr].(*Config)
41+
assert.Equal(t, &Config{
42+
ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(typeStr),
43+
Brokers: []string{"10.10.10.10:9092"},
44+
ProtocolVersion: "2.0.0",
45+
TopicMatch: "test_*",
46+
GroupMatch: "test_*",
47+
Authentication: kafkaexporter.Authentication{
48+
TLS: &configtls.TLSClientSetting{
49+
TLSSetting: configtls.TLSSetting{
50+
CAFile: "ca.pem",
51+
CertFile: "cert.pem",
52+
KeyFile: "key.pem",
53+
},
54+
},
55+
},
56+
ClientID: defaultClientID,
57+
Scrapers: []string{"brokers", "topics", "consumers"},
58+
}, r)
59+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package kafkametricsreceiver
16+
17+
import (
18+
"context"
19+
20+
"go.opentelemetry.io/collector/component"
21+
"go.opentelemetry.io/collector/config/configmodels"
22+
"go.opentelemetry.io/collector/consumer"
23+
"go.opentelemetry.io/collector/receiver/receiverhelper"
24+
"go.opentelemetry.io/collector/receiver/scraperhelper"
25+
)
26+
27+
const (
28+
typeStr = "kafkametrics"
29+
defaultBroker = "localhost:9092"
30+
defaultGroupMatch = ".*"
31+
defaultTopicMatch = ".*"
32+
defaultClientID = "otel-metrics-receiver"
33+
)
34+
35+
// NewFactory creates kafkametrics receiver factory.
36+
func NewFactory() component.ReceiverFactory {
37+
return receiverhelper.NewFactory(
38+
typeStr,
39+
createDefaultConfig,
40+
receiverhelper.WithMetrics(createMetricsReceiver))
41+
}
42+
43+
func createDefaultConfig() configmodels.Receiver {
44+
return &Config{
45+
ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(typeStr),
46+
Brokers: []string{defaultBroker},
47+
GroupMatch: defaultGroupMatch,
48+
TopicMatch: defaultTopicMatch,
49+
ClientID: defaultClientID,
50+
}
51+
}
52+
53+
func createMetricsReceiver(
54+
ctx context.Context,
55+
params component.ReceiverCreateParams,
56+
cfg configmodels.Receiver,
57+
nextConsumer consumer.MetricsConsumer) (component.MetricsReceiver, error) {
58+
c := cfg.(*Config)
59+
r, err := newMetricsReceiver(ctx, *c, params, nextConsumer)
60+
if err != nil {
61+
return nil, err
62+
}
63+
return r, nil
64+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package kafkametricsreceiver
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
"go.opentelemetry.io/collector/component"
23+
"go.opentelemetry.io/collector/config/configcheck"
24+
"go.opentelemetry.io/collector/consumer"
25+
)
26+
27+
func TestCreateDefaultConfig(t *testing.T) {
28+
factory := NewFactory()
29+
cfg := factory.CreateDefaultConfig()
30+
assert.NotNil(t, cfg, "default config not created")
31+
assert.NoError(t, configcheck.ValidateConfig(cfg))
32+
}
33+
34+
func TestCreateMetricsReceiver_errors(t *testing.T) {
35+
factory := NewFactory()
36+
cfg := factory.CreateDefaultConfig().(*Config)
37+
cfg.Brokers = []string{"invalid:9092"}
38+
cfg.ProtocolVersion = "2.0.0"
39+
cfg.Scrapers = []string{"topics"}
40+
r, err := createMetricsReceiver(context.Background(), component.ReceiverCreateParams{}, cfg, nil)
41+
assert.Error(t, err)
42+
assert.Nil(t, r)
43+
}
44+
45+
func TestCreateMetricsReceiver(t *testing.T) {
46+
prev := newMetricsReceiver
47+
newMetricsReceiver = func(ctx context.Context, config Config, params component.ReceiverCreateParams, consumer consumer.MetricsConsumer) (component.MetricsReceiver, error) {
48+
return nil, nil
49+
}
50+
factory := NewFactory()
51+
cfg := factory.CreateDefaultConfig().(*Config)
52+
cfg.Brokers = []string{"invalid:9092"}
53+
cfg.ProtocolVersion = "2.0.0"
54+
cfg.Scrapers = []string{"topics"}
55+
_, err := createMetricsReceiver(context.Background(), component.ReceiverCreateParams{}, cfg, nil)
56+
newMetricsReceiver = prev
57+
assert.Nil(t, err)
58+
}

receiver/kafkametricsreceiver/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkametricsreceiver
2+
3+
go 1.14
4+
5+
require (
6+
github.com/stretchr/testify v1.7.0
7+
go.opentelemetry.io/collector v0.21.0
8+
)

0 commit comments

Comments
 (0)