-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1010 from openmeterio/feat-kafka-config
feat: add Kafka client configs
- Loading branch information
Showing
7 changed files
with
315 additions
and
5 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
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,67 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/confluentinc/confluent-kafka-go/v2/kafka" | ||
"github.com/stretchr/testify/assert" | ||
|
||
pkgkafka "github.com/openmeterio/openmeter/pkg/kafka" | ||
) | ||
|
||
func TestKafkaIngestConfiguration(t *testing.T) { | ||
|
||
tests := []struct { | ||
Name string | ||
|
||
KafkaConfig KafkaIngestConfiguration | ||
ExpectedKafkaConfigMap kafka.ConfigMap | ||
}{ | ||
{ | ||
Name: "All", | ||
KafkaConfig: KafkaIngestConfiguration{ | ||
Broker: "127.0.0.1:29092", | ||
SecurityProtocol: "SASL_SSL", | ||
SaslMechanisms: "PLAIN", | ||
SaslUsername: "user", | ||
SaslPassword: "pass", | ||
StatsInterval: pkgkafka.TimeDurationMilliSeconds(10 * time.Second), | ||
BrokerAddressFamily: "v6", | ||
SocketKeepAliveEnabled: true, | ||
TopicMetadataRefreshInterval: pkgkafka.TimeDurationMilliSeconds(time.Minute), | ||
}, | ||
ExpectedKafkaConfigMap: kafka.ConfigMap{ | ||
"bootstrap.servers": "127.0.0.1:29092", | ||
"broker.address.family": pkgkafka.BrokerAddressFamilyIPv6, | ||
"go.logs.channel.enable": true, | ||
"metadata.max.age.ms": pkgkafka.TimeDurationMilliSeconds(3 * time.Minute), | ||
"sasl.mechanism": "PLAIN", | ||
"sasl.password": "pass", | ||
"sasl.username": "user", | ||
"security.protocol": "SASL_SSL", | ||
"socket.keepalive.enable": true, | ||
"statistics.interval.ms": pkgkafka.TimeDurationMilliSeconds(10 * time.Second), | ||
"topic.metadata.refresh.interval.ms": pkgkafka.TimeDurationMilliSeconds(time.Minute), | ||
}, | ||
}, | ||
{ | ||
Name: "Basic", | ||
KafkaConfig: KafkaIngestConfiguration{ | ||
Broker: "127.0.0.1:29092", | ||
}, | ||
ExpectedKafkaConfigMap: kafka.ConfigMap{ | ||
"bootstrap.servers": "127.0.0.1:29092", | ||
"broker.address.family": pkgkafka.BrokerAddressFamilyIPv4, | ||
"go.logs.channel.enable": true, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
config := test.KafkaConfig.CreateKafkaConfig() | ||
assert.Equal(t, test.ExpectedKafkaConfigMap, config) | ||
}) | ||
} | ||
} |
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,76 @@ | ||
package kafka | ||
|
||
import ( | ||
"encoding" | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type configValue interface { | ||
fmt.Stringer | ||
encoding.TextUnmarshaler | ||
json.Unmarshaler | ||
} | ||
|
||
const ( | ||
BrokerAddressFamilyAny BrokerAddressFamily = "any" | ||
BrokerAddressFamilyIPv4 BrokerAddressFamily = "v4" | ||
BrokerAddressFamilyIPv6 BrokerAddressFamily = "v6" | ||
) | ||
|
||
var _ configValue = (*BrokerAddressFamily)(nil) | ||
|
||
type BrokerAddressFamily string | ||
|
||
func (s *BrokerAddressFamily) UnmarshalText(text []byte) error { | ||
switch strings.ToLower(strings.TrimSpace(string(text))) { | ||
case "v4": | ||
*s = BrokerAddressFamilyIPv4 | ||
case "v6": | ||
*s = BrokerAddressFamilyIPv6 | ||
case "any": | ||
*s = BrokerAddressFamilyAny | ||
default: | ||
return fmt.Errorf("invalid value broker family address: %s", text) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *BrokerAddressFamily) UnmarshalJSON(data []byte) error { | ||
return s.UnmarshalText(data) | ||
} | ||
|
||
func (s BrokerAddressFamily) String() string { | ||
return string(s) | ||
} | ||
|
||
var _ configValue = (*TimeDurationMilliSeconds)(nil) | ||
|
||
type TimeDurationMilliSeconds time.Duration | ||
|
||
func (d *TimeDurationMilliSeconds) UnmarshalText(text []byte) error { | ||
v, err := time.ParseDuration(strings.TrimSpace(string(text))) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse time duration: %w", err) | ||
} | ||
|
||
*d = TimeDurationMilliSeconds(v) | ||
|
||
return nil | ||
} | ||
|
||
func (d *TimeDurationMilliSeconds) UnmarshalJSON(data []byte) error { | ||
return d.UnmarshalText(data) | ||
} | ||
|
||
func (d TimeDurationMilliSeconds) Duration() time.Duration { | ||
return time.Duration(d) | ||
} | ||
|
||
func (d TimeDurationMilliSeconds) String() string { | ||
return strconv.Itoa(int(time.Duration(d).Milliseconds())) | ||
} |
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,117 @@ | ||
package kafka | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBrokerAddressFamily(t *testing.T) { | ||
|
||
tests := []struct { | ||
Name string | ||
|
||
Value string | ||
ExpectedError error | ||
ExplectedValue BrokerAddressFamily | ||
}{ | ||
{ | ||
Name: "Any", | ||
Value: "any", | ||
ExpectedError: nil, | ||
ExplectedValue: BrokerAddressFamilyAny, | ||
}, | ||
{ | ||
Name: "IPv4", | ||
Value: "v4", | ||
ExpectedError: nil, | ||
ExplectedValue: BrokerAddressFamilyIPv4, | ||
}, | ||
{ | ||
Name: "IPv6", | ||
Value: "v6", | ||
ExpectedError: nil, | ||
ExplectedValue: BrokerAddressFamilyIPv6, | ||
}, | ||
{ | ||
Name: "Invalid", | ||
Value: "invalid", | ||
ExpectedError: errors.New("invalid value broker family address: invalid"), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
var family BrokerAddressFamily | ||
|
||
err := family.UnmarshalText([]byte(test.Value)) | ||
assert.Equal(t, test.ExpectedError, err) | ||
if err == nil { | ||
assert.Equal(t, test.ExplectedValue, family) | ||
} | ||
|
||
err = family.UnmarshalJSON([]byte(test.Value)) | ||
assert.Equal(t, test.ExpectedError, err) | ||
if err == nil { | ||
assert.Equal(t, test.ExplectedValue, family) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestTimeDurationMilliSeconds(t *testing.T) { | ||
|
||
tests := []struct { | ||
Name string | ||
|
||
Value string | ||
ExpectedError error | ||
ExplectedValue TimeDurationMilliSeconds | ||
ExpectedString string | ||
ExpectedDuration time.Duration | ||
}{ | ||
{ | ||
Name: "Duration", | ||
Value: "6s", | ||
ExpectedError: nil, | ||
ExplectedValue: TimeDurationMilliSeconds(6 * time.Second), | ||
ExpectedString: "6000", | ||
ExpectedDuration: 6 * time.Second, | ||
}, | ||
{ | ||
Name: "Invalid", | ||
Value: "10000", | ||
ExpectedError: fmt.Errorf("failed to parse time duration: %w", errors.New("time: missing unit in duration \"10000\"")), | ||
}, | ||
{ | ||
Name: "Invalid", | ||
Value: "invalid", | ||
ExpectedError: fmt.Errorf("failed to parse time duration: %w", errors.New("time: invalid duration \"invalid\"")), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
var timeMs TimeDurationMilliSeconds | ||
|
||
err := timeMs.UnmarshalText([]byte(test.Value)) | ||
assert.Equal(t, test.ExpectedError, err) | ||
if err == nil { | ||
assert.Equal(t, test.ExplectedValue, timeMs) | ||
assert.Equal(t, test.ExpectedString, timeMs.String()) | ||
assert.Equal(t, test.ExpectedDuration, timeMs.Duration()) | ||
} | ||
|
||
err = timeMs.UnmarshalJSON([]byte(test.Value)) | ||
assert.Equal(t, test.ExpectedError, err) | ||
if err == nil { | ||
assert.Equal(t, test.ExplectedValue, timeMs) | ||
assert.Equal(t, test.ExpectedString, timeMs.String()) | ||
assert.Equal(t, test.ExpectedDuration, timeMs.Duration()) | ||
} | ||
}) | ||
} | ||
} |