Skip to content

Using Enum for metric names inconsistently converts to underlying str representation #955

Description

@afm-umbra

Describe the bug

Using Enum for metric names in the various statsd methods inconsistently converts to underlying str representation

To Reproduce

class MyServiceEvents(str, Enum):
    MODEL_UPDATE = "my-service.model-update"

from datadog import statsd

# Shows up in DataDog as event name `MyServiceEvents.MODEL_UPDATE
statsd.increment(MyServiceEvents.MODEL_UPDATE, ...)

# Shows up in DataDog as event name `my-service.model-update`
statsd.event(title=MyServiceEvents.MODEL_UPDATE, ...)

Expected behavior

Expect all statsd functions to exhibit consistent behavior, one way or the other.

Screenshots

N/A

Environment and Versions (please complete the following information):

datadog=0.25.1

Additional context

statsd.increment, .gauge, .foo go through the metrics reporting chain via _report -> _serialize_metric, which creates a metric packet like:

"%s%s:%s|%s%s%s%s%s%s%s" % (
            (self.namespace + ".") if self.namespace else "",
            metric,
            value,
            ...

so if metric is a string enum there, you're going to get the string enum representation

events go through DogStatsd._escape_event_content(title), which is title.replace("\n", "\\n"). title.replace(...) is inherited from str, so you get a str back, e.g.

class Foo(str, Enum):
    x = "x"
    abcde = "abcde"

Foo.abcde.replace("abc", "123")
'123de'

even if you replace a substring that's not present, you still change the type from the enum to a string

Foo.abcde.replace("lol", "ohno")
'abcde'

Foo.abcde
<Foo.abcde: 'abcde'>

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugBug related issue

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions