confluentinc / confluent-kafka-go

Confluent's Apache Kafka Golang client
Apache License 2.0
4.62k stars 657 forks source link

Provide a way to convert ErrorCode enum name to a string #516

Open james-johnston-thumbtack opened 4 years ago

james-johnston-thumbtack commented 4 years ago

Description

The kafka.ErrorCode type only provides a String function that converts it to a human-readable string description of the error. But I would like to get the actual error code constant name. The motivation is to use it as a low-cardinality and concise string for monitoring purposes (storing as InfluxDB tag) and thus be able to view/aggregate by various error codes in Grafana. (While the current implementation of ErrorCode.String may also have low cardinality, it is longer than I want for my purposes.) For example:

var code kafka.ErrorCode = kafka.ErrInvalidRequest
fmt.Println(code.Name())

would print either ErrInvalidRequest or INVALID_REQUEST (derived from RD_KAFKA_RESP_ERR_INVALID_REQUEST). I don't really have a preference on which one.

This could be accomplished by calling either rd_kafka_err2name to get the INVALID_REQUEST string. Or alternatively, code generate a Name function that contains a giant "switch" statement that returns the appropriate string (e.g. similar to the hand-written func (p Pill) String() string example at https://blog.golang.org/generate )

edenhill commented 4 years ago

code.Name() -> err2name() seems to be the easiest alternative.

Suggest using the same names as in generated_errors.go, see the generator for how they are constructed.