Skip to content

Commit

Permalink
✨ Add env tags to all events/metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
kamaradclimber committed Jun 10, 2024
1 parent aac7ae4 commit 674b990
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Installation is done through hacs (pending integration in default repository, th
## Configuration

Add a new integration "datadog-agentless". You'll be asked to give an API key and the site (datadoghq.com or datadoghq.eu for instance).
You'll also need to configure an environment, this will be passed as the `env` tag on all events/metrics. Suggested value is `prod`.

## Usage

Expand Down
11 changes: 9 additions & 2 deletions custom_components/datadog_agentless/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def full_event_listener(creds: dict, event: Event[EventStateChangedData]):
if value is None:
return

tags = [f"entity:{new_state.entity_id}", "service:home-assistant", f"version:{HAVERSION}"]
tags = [f"entity:{new_state.entity_id}", "service:home-assistant", f"version:{HAVERSION}", f"env:{creds['env']}"]
unit = None
if "friendly_name" in new_state.attributes:
tags.append(f"friendly_name:{new_state.attributes['friendly_name']}")
Expand Down Expand Up @@ -200,7 +200,7 @@ def full_all_event_listener(creds: dict, event: Event):
# in addition we expect events about "metrics" to change more often than the others
return

tags = ["service:home-assistant", f"version:{HAVERSION}", f"event_type:{event.event_type}"]
tags = ["service:home-assistant", f"version:{HAVERSION}", f"event_type:{event.event_type}", f"env:{creds['env']}"]
if event.event_type == homeassistant.const.EVENT_STATE_CHANGED:
text=json.dumps(orjson.loads(orjson.dumps(event.json_fragment)))
else:
Expand Down Expand Up @@ -269,3 +269,10 @@ def generate_message(event: Event) -> str:
else:
_LOGGER.warn(f"Unhandled event type {event.event_type}, raise an issue on https://github.com/kamaradclimber/datadog-integration-ha/issues")
return ""

async def async_migrate_entry(hass, config_entry: ConfigEntry):
if config_entry.version == 1:
_LOGGER.warn("config entry is version 1, migrating to version 2")
new = {**config_entry.data}
new["env"] = "prod"
hass.config_entries.async_update_entry(config_entry, data=new, version=2)
3 changes: 2 additions & 1 deletion custom_components/datadog_agentless/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


class SetupConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
VERSION = 2

def __init__(self):
"""Initialize"""
Expand Down Expand Up @@ -58,5 +58,6 @@ async def async_step_user(self, user_input: Optional[dict[str, Any]] = None):
step_id="user", data_schema=vol.Schema({
vol.Required("api_key"): str,
vol.Required("site"): str,
vol.Required("env"): str,
})
)
1 change: 1 addition & 0 deletions custom_components/datadog_agentless/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"step": {
"user": {
"data": {
"env": "Environment of this instance (dev/prod)",
"api_key": "API Key",
"site": "Datadog Site"
},
Expand Down

0 comments on commit 674b990

Please sign in to comment.