Skip to content

add env vars support for dogstatsd host, port and entity id#363

Merged
ahmed-mez merged 5 commits intoDataDog:masterfrom
ahmed-mez:master
Mar 21, 2019
Merged

add env vars support for dogstatsd host, port and entity id#363
ahmed-mez merged 5 commits intoDataDog:masterfrom
ahmed-mez:master

Conversation

@ahmed-mez
Copy link
Copy Markdown
Contributor

@ahmed-mez ahmed-mez commented Mar 15, 2019

Add environment variables support for Client configuration

Support of 3 environment variables for Client configuration:

"DD_AGENT_HOST" used to provide the Agent hostname.
"DD_DOGSTATSD_PORT" used to provide the DogStatsD port.
"DD_ENTITY_ID" used to provide an identifier to the Client.

If Client constructor's arguments are not set, prioritize "DD_AGENT_HOST" and "DD_DOGSTATSD_PORT" over default values.
Add "DD_ENTITY_ID" to tags with the tag name dd.internal.entity_id.

@ahmed-mez ahmed-mez requested review from dabcoder, ofek and yannmh and removed request for dabcoder March 15, 2019 12:27
Copy link
Copy Markdown
Contributor

@ofek ofek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

statsd.host = None
statsd.port = None
else:
# Check host and port env vars, override arguments if env vars exist
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please give arguments precedence as it says in the PR description.

log.warning("Port number provided in DD_DOGSTATSD_PORT env var is not an integer: \
%s, using %s as port number", DD_DOGSTATSD_PORT, port)
pass

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may have to set the default arguments to None to detect actual args. @yannmh wdyt?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so should I set the default arguments to None ? @ofek @yannmh

Copy link
Copy Markdown

@yannmh yannmh Mar 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion on this. If we decide to keep the default args, I'd name the default argument, so it becomes

        # Check host and port env vars
        DD_AGENT_HOST = os.environ.get('DD_AGENT_HOST', '')
        if DD_AGENT_HOST and host == DEFAULT_HOST:
            host = DD_AGENT_HOST

Other notes:

  1. is it a "conventional" to name Python variables that store env variable with UPPERCASE?
  2. os.environ.get('DD_AGENT_HOST', '') -> os.environ.get('DD_AGENT_HOST')

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the review :)

I think I'm going to keep default args and name them as you suggested

regarding the other notes:

  1. it is not conventional to name Python variables that store env var with UPPERCASE, so I'm going to change it lowercase 👍
  2. 👍

statsd_port = int(DD_DOGSTATSD_PORT)
except ValueError:
pass

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need these. The "already instantiated" statsd client, would have had those values set in the constructor below.

if constant_tags is None:
constant_tags = []
self.constant_tags = constant_tags + env_tags
ENTITY_ID_TAG_NAME = "dd.internal.entity_id"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this constant should be moved outside of the constructor

except ValueError:
log.warning("Port number provided in DD_DOGSTATSD_PORT env var is not an integer: \
%s, using %s as port number", DD_DOGSTATSD_PORT, port)
pass
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need the 'pass'

os.environ['DD_DOGSTATSD_PORT'] = '4321'
initialize()
t.assert_equal(statsd.host, "myenvvarhost")
t.assert_equal(statsd.port, 4321)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make this a separate test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course 👍

statsd.socket = FakeSocket()
statsd.gauge('gt', 123.4)
t.assert_equal('gt:123.4|g|#country:canada,red,country:china,age:45,blue,dd.internal.entity_id:04652bb7-19b7-11e9-9cc6-42010a9c016d', statsd.socket.recv())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Copy Markdown

@yannmh yannmh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a couple more comments.

@ahmed-mez
Copy link
Copy Markdown
Contributor Author

ahmed-mez commented Mar 21, 2019

Added a couple more comments.

@yannmh thanks, can you please see the changes I've made?

let me know if you have any other comment :)

@ahmed-mez ahmed-mez closed this Mar 21, 2019
@ahmed-mez ahmed-mez reopened this Mar 21, 2019
@yannmh
Copy link
Copy Markdown

yannmh commented Mar 21, 2019

Looks good to me.

@ahmed-mez ahmed-mez requested a review from ofek March 21, 2019 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants