Skip to content

Commit

Permalink
feat: WithClientInfo added for client_id
Browse files Browse the repository at this point in the history
Signed-off-by: Eray Ates <[email protected]>
  • Loading branch information
rytsh committed Dec 14, 2023
1 parent 48c4aef commit 7fc13a7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
10 changes: 10 additions & 0 deletions clientoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type options struct {
Consumer consumer
ClientID string
InfoVersion string
KGOOptions []kgo.Opt
AutoTopicCreation bool
}
Expand All @@ -25,6 +26,15 @@ func WithClientID(clientID string) Option {
}
}

// WithClientInfo to set client_id in kafka server.
// Not usable with WithClientID option.
// - appname:version@hostname
func WithClientInfo(name, version string) Option {
return func(o *options) {
o.ClientID = name + ":" + version + "@" + idHostname
}
}

// WithAutoTopicCreation to enable auto topic creation for producer and consumer.
//
// Default is enabled.
Expand Down
8 changes: 6 additions & 2 deletions example/consume/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

var (
kafkaConfig = wkafka.Config{
Brokers: []string{"localhost:9095"},
Brokers: []string{"localhost:9092"},
}
consumeConfig = wkafka.ConsumeConfig{
Topics: []string{"test"},
Expand Down Expand Up @@ -67,7 +67,11 @@ func main() {

func run(ctx context.Context, _ *sync.WaitGroup) error {
p := Processor{}
client, err := wkafka.NewClient(ctx, kafkaConfig, wkafka.WithConsumerBatch(consumeConfig, p))
client, err := wkafka.NewClient(
ctx, kafkaConfig,
wkafka.WithConsumerBatch(consumeConfig, p),
wkafka.WithClientInfo("testapp", "v0.1.0"),
)
if err != nil {
return err
}
Expand Down
12 changes: 8 additions & 4 deletions id.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import (
"path/filepath"
)

var DefaultClientID string
var (
DefaultClientID string
idProgname string
idHostname string
)

func init() {
progname := filepath.Base(os.Args[0])
hostname, _ := os.Hostname()
idProgname = filepath.Base(os.Args[0])
idHostname, _ = os.Hostname()

DefaultClientID = fmt.Sprintf("%s@%s", progname, hostname)
DefaultClientID = fmt.Sprintf("%s@%s", idProgname, idHostname)
}

0 comments on commit 7fc13a7

Please sign in to comment.