-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add kafka topics observer implementation (#38060)
#### Description Add implementation and tests for kafka topics observer #### Link to tracking issue New component #37665 #### Testing Unit tests
- Loading branch information
1 parent
2039671
commit ad684d6
Showing
16 changed files
with
501 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: new_component | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: kafkatopicsobserver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: "Adding implementation and tests of the component's logic." | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [37665] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [user] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package kafkatopicsobserver | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/confmap" | ||
"go.opentelemetry.io/collector/confmap/confmaptest" | ||
"go.opentelemetry.io/collector/confmap/xconfmap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer/kafkatopicsobserver/internal/metadata" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/kafka" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
id component.ID | ||
expected component.Config | ||
expectedError string | ||
}{ | ||
{ | ||
id: component.NewID(metadata.Type), | ||
expected: NewFactory().CreateDefaultConfig(), | ||
expectedError: "protocol_version must be specified; topic_regex must be specified", | ||
}, | ||
{ | ||
id: component.NewIDWithName(metadata.Type, "all_settings"), | ||
expected: &Config{ | ||
ProtocolVersion: "3.7.0", | ||
Brokers: []string{"1.2.3.4:9092", "2.3.4.5:9092"}, | ||
TopicRegex: "^topic[0-9]$", | ||
TopicsSyncInterval: 5 * time.Second, | ||
ResolveCanonicalBootstrapServersOnly: false, | ||
SessionTimeout: 30 * time.Second, | ||
HeartbeatInterval: 20 * time.Second, | ||
Authentication: kafka.Authentication{ | ||
PlainText: &kafka.PlainTextConfig{ | ||
Username: "fooUser", | ||
Password: "fooPassword", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.id.String(), func(t *testing.T) { | ||
cfg := loadConfig(t, tt.id) | ||
if tt.expectedError != "" { | ||
assert.EqualError(t, xconfmap.Validate(cfg), tt.expectedError) | ||
} else { | ||
assert.NoError(t, xconfmap.Validate(cfg)) | ||
} | ||
assert.Equal(t, tt.expected, cfg) | ||
}) | ||
} | ||
} | ||
|
||
func TestValidateConfig(t *testing.T) { | ||
cfg := &Config{ | ||
Brokers: []string{}, | ||
ProtocolVersion: "3.7.0", | ||
TopicRegex: "^test[0-9]$", | ||
} | ||
assert.Equal(t, "brokers list must be specified; topics_sync_interval must be greater than 0; session_timeout must be greater than 0; heartbeat_interval must be greater than 0", xconfmap.Validate(cfg).Error()) | ||
|
||
cfg = &Config{ | ||
Brokers: []string{"1.2.3.4:9092"}, | ||
ProtocolVersion: "", | ||
TopicRegex: "^topic[0-9]$", | ||
TopicsSyncInterval: 1 * time.Second, | ||
SessionTimeout: 1 * time.Second, | ||
HeartbeatInterval: 1 * time.Second, | ||
} | ||
assert.Equal(t, "protocol_version must be specified", xconfmap.Validate(cfg).Error()) | ||
|
||
cfg = &Config{ | ||
Brokers: []string{"1.2.3.4:9092"}, | ||
ProtocolVersion: "3.7.0", | ||
TopicRegex: "", | ||
TopicsSyncInterval: 1 * time.Second, | ||
SessionTimeout: 1 * time.Second, | ||
HeartbeatInterval: 1 * time.Second, | ||
} | ||
assert.Equal(t, "topic_regex must be specified", xconfmap.Validate(cfg).Error()) | ||
|
||
cfg = &Config{ | ||
Brokers: []string{"1.2.3.4:9092"}, | ||
ProtocolVersion: "3.7.0", | ||
TopicRegex: "^topic[0-9]$", | ||
TopicsSyncInterval: 0 * time.Second, | ||
SessionTimeout: 1 * time.Second, | ||
HeartbeatInterval: 1 * time.Second, | ||
} | ||
assert.Equal(t, "topics_sync_interval must be greater than 0", xconfmap.Validate(cfg).Error()) | ||
|
||
cfg = &Config{ | ||
Brokers: []string{"1.2.3.4:9092"}, | ||
ProtocolVersion: "3.7.0", | ||
TopicRegex: "^topic[0-9]$", | ||
TopicsSyncInterval: 1 * time.Second, | ||
SessionTimeout: 0 * time.Second, | ||
HeartbeatInterval: 1 * time.Second, | ||
} | ||
assert.Equal(t, "session_timeout must be greater than 0", xconfmap.Validate(cfg).Error()) | ||
|
||
cfg = &Config{ | ||
Brokers: []string{"1.2.3.4:9092"}, | ||
ProtocolVersion: "3.7.0", | ||
TopicRegex: "^topic[0-9]$", | ||
TopicsSyncInterval: 1 * time.Second, | ||
SessionTimeout: 1 * time.Second, | ||
HeartbeatInterval: 0 * time.Second, | ||
} | ||
assert.Equal(t, "heartbeat_interval must be greater than 0", xconfmap.Validate(cfg).Error()) | ||
|
||
cfg = &Config{ | ||
Brokers: []string{"1.2.3.4:9092"}, | ||
ProtocolVersion: "3.7.0", | ||
TopicRegex: "^topic[0-9]$", | ||
TopicsSyncInterval: 1 * time.Second, | ||
SessionTimeout: 1 * time.Second, | ||
HeartbeatInterval: 1 * time.Second, | ||
} | ||
assert.NoError(t, xconfmap.Validate(cfg)) | ||
} | ||
|
||
func loadConf(tb testing.TB, path string, id component.ID) *confmap.Conf { | ||
cm, err := confmaptest.LoadConf(filepath.Join("testdata", path)) | ||
require.NoError(tb, err) | ||
sub, err := cm.Sub(id.String()) | ||
require.NoError(tb, err) | ||
return sub | ||
} | ||
|
||
func loadConfig(tb testing.TB, id component.ID) *Config { | ||
factory := NewFactory() | ||
cfg := factory.CreateDefaultConfig() | ||
sub := loadConf(tb, "config.yaml", id) | ||
require.NoError(tb, sub.Unmarshal(cfg)) | ||
return cfg.(*Config) | ||
} |
Oops, something went wrong.