Skip to content

Commit

Permalink
support unified service tagging:
Browse files Browse the repository at this point in the history
  • Loading branch information
sarah-witt committed Jan 3, 2025
1 parent 7088354 commit d56c915
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 10 deletions.
10 changes: 10 additions & 0 deletions octopus_deploy/assets/configuration/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ files:
example: 30
type: integer
required: false
- name: unified_service_tagging
description: |
Tags metrics, logs, and events with the project name as the `service`, and octopus environment as `env`,
and release version as `version`.
For more information, see https://docs.datadoghq.com/getting_started/tagging/unified_service_tagging
value:
default: false
example: true
type: boolean
required: false
- template: instances/default
- template: instances/http
overrides:
Expand Down
10 changes: 10 additions & 0 deletions octopus_deploy/datadog_checks/octopus_deploy/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ def _process_projects(self, space_id, space_name, project_group_id, project_grou
f'project_id:{project_id}',
f'project_name:{project_name}',
]
if not self.config.disable_generic_tags and self.config.unified_service_tagging:
tags.append(f'service:{project_name}')
self.gauge("project.count", 1, tags=tags)
self._process_queued_and_running_tasks(space_id, space_name, project_id, project_name)
self._process_completed_tasks(space_id, space_name, project_id, project_name)
Expand Down Expand Up @@ -288,6 +290,8 @@ def _process_environments(self, space_id, space_name):
f"environment_id:{environment_id}",
f"environment_slug:{environment_slug}",
]
if not self.config.disable_generic_tags and self.config.unified_service_tagging:
tags.append(f'env:{environment_name}')
self.gauge("environment.count", 1, tags=tags)
self.gauge("environment.use_guided_failure", use_guided_failure, tags=tags)
self.gauge("environment.allow_dynamic_infrastructure", allow_dynamic_infrastructure, tags=tags)
Expand Down Expand Up @@ -370,6 +374,8 @@ def _process_tasks(self, space_id, space_name, project_name, tasks_json):
f'server_node:{server_node}',
]
)
if not self.config.disable_generic_tags and self.config.unified_service_tagging:
tags.append(f'service:{project_name}')
self.log.debug("Processing task id %s for project %s", task_id, project_name)
queued_time, executing_time, completed_time = self._calculate_task_times(task)
self.gauge("deployment.count", 1, tags=tags)
Expand Down Expand Up @@ -422,6 +428,10 @@ def _get_deployment_tags(self, space_id, deployment_id):
f'release_version:{release_version}',
f'environment_name:{environment_name}',
]
if not self.config.disable_generic_tags and self.config.unified_service_tagging:
tags.append(f'env:{environment_name}')
tags.append(f'version:{release_version}')

return environment_name, tags

def _collect_server_nodes_metrics(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,9 @@ def instance_tls_verify():
return True


def instance_unified_service_tagging():
return False


def instance_use_legacy_auth_encoding():
return True
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class InstanceConfig(BaseModel):
tls_protocols_allowed: Optional[tuple[str, ...]] = None
tls_use_host_header: Optional[bool] = None
tls_verify: Optional[bool] = None
unified_service_tagging: Optional[bool] = None
use_legacy_auth_encoding: Optional[bool] = None
username: Optional[str] = None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ instances:
#
# paginated_limit: 30

## @param unified_service_tagging - boolean - optional - default: true
## Tags metrics, logs, and events with the project name as the `service`, and octopus environment as `env`,
## and release version as `version`.
## For more information, see https://docs.datadoghq.com/getting_started/tagging/unified_service_tagging
#
# unified_service_tagging: true

## @param tags - list of strings - optional
## A list of tags to attach to every metric and service check emitted by this instance.
##
Expand Down
5 changes: 4 additions & 1 deletion octopus_deploy/hatch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ matrix.setup.env-vars = [
]

[envs.default]
e2e-env = false
e2e-env = false

[envs.default.env-vars]
DDEV_SKIP_GENERIC_TAGS_CHECK = "true"
37 changes: 29 additions & 8 deletions octopus_deploy/tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,46 @@
MOCKED_TIME1 = datetime.datetime.fromisoformat("2024-09-23T14:45:00.123+00:00")
MOCKED_TIME2 = MOCKED_TIME1 + datetime.timedelta(seconds=DEFAULT_COLLECTION_INTERVAL)

E2E_METRICS = [
"octopus_deploy.space.count",
"octopus_deploy.project_group.count",
"octopus_deploy.project.count",

DEPLOY_METRICS = [
"octopus_deploy.deployment.count",
"octopus_deploy.deployment.executing",
"octopus_deploy.deployment.queued",
"octopus_deploy.deployment.waiting",
"octopus_deploy.deployment.queued_time",
"octopus_deploy.deployment.executing_time",
"octopus_deploy.server_node.count",
"octopus_deploy.server_node.in_maintenance_mode",
"octopus_deploy.server_node.max_concurrent_tasks",
"octopus_deploy.deployment.completed_time",
]

SPACE_METRICS = [
"octopus_deploy.space.count",
]

PROJECT_GROUP_METRICS = [
"octopus_deploy.project_group.count",
]

PROJECT_METRICS = [
"octopus_deploy.project.count",
]

ENV_METRICS = [
"octopus_deploy.environment.allow_dynamic_infrastructure",
"octopus_deploy.environment.count",
"octopus_deploy.environment.use_guided_failure",
]

ALL_METRICS = ["octopus_deploy.deployment.completed_time"] + E2E_METRICS
SERVER_METRICS = [
"octopus_deploy.server_node.count",
"octopus_deploy.server_node.in_maintenance_mode",
"octopus_deploy.server_node.max_concurrent_tasks",
]

COMPLETED_METRICS = ["octopus_deploy.deployment.completed_time"]

ALL_METRICS = SERVER_METRICS + ENV_METRICS + PROJECT_METRICS + PROJECT_GROUP_METRICS + SPACE_METRICS + DEPLOY_METRICS

E2E_METRICS = list(set(ALL_METRICS) - set(COMPLETED_METRICS))

ALL_DEPLOYMENT_LOGS = [
{
Expand Down
74 changes: 73 additions & 1 deletion octopus_deploy/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@
from datadog_checks.dev.utils import get_metadata_metrics
from datadog_checks.octopus_deploy import OctopusDeployCheck

from .constants import ALL_DEPLOYMENT_LOGS, ALL_EVENTS, ALL_METRICS, MOCKED_TIME1, MOCKED_TIME2, ONLY_TEST_LOGS
from .constants import (
ALL_DEPLOYMENT_LOGS,
ALL_EVENTS,
ALL_METRICS,
COMPLETED_METRICS,
DEPLOY_METRICS,
ENV_METRICS,
MOCKED_TIME1,
MOCKED_TIME2,
ONLY_TEST_LOGS,
PROJECT_METRICS,
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -2257,3 +2268,64 @@ def test_paginated_limit_environments(
skip_take_args += [(list(args), skip, take)]

assert skip_take_args == expected_skip_take_args


@pytest.mark.parametrize(
('disable_generic_tags, unified_service_tagging, expect_service_tags'),
[
pytest.param(
True,
True,
False,
),
pytest.param(
True,
False,
False,
),
pytest.param(
False,
True,
True,
),
pytest.param(
False,
False,
False,
),
],
)
@pytest.mark.usefixtures('mock_http_get')
@mock.patch("datadog_checks.octopus_deploy.check.get_current_datetime")
def test_unified_service_tagging(
get_current_datetime,
dd_run_check,
aggregator,
disable_generic_tags,
unified_service_tagging,
expect_service_tags,
):
instance = {'octopus_endpoint': 'http://localhost:80'}
instance['disable_generic_tags'] = disable_generic_tags
instance['unified_service_tagging'] = unified_service_tagging
check = OctopusDeployCheck('octopus_deploy', {}, [instance])
get_current_datetime.return_value = MOCKED_TIME1
dd_run_check(check)

if expect_service_tags:
for metric in set(DEPLOY_METRICS) - set(COMPLETED_METRICS):
aggregator.assert_metric_has_tag(metric, 'service:my-project', count=1)
aggregator.assert_metric_has_tag(metric, 'env:staging', count=1)

for metric in PROJECT_METRICS:
aggregator.assert_metric_has_tag(metric, 'service:my-project', count=1)
aggregator.assert_metric_has_tag(metric, 'env:staging', count=0)

for metric in ENV_METRICS:
aggregator.assert_metric_has_tag(metric, 'service:my-project', count=0)
aggregator.assert_metric_has_tag(metric, 'env:staging', count=1)

else:
for metric in ALL_METRICS:
aggregator.assert_metric_has_tag(metric, 'service:my-project', count=0)
aggregator.assert_metric_has_tag(metric, 'env:staging', count=0)

0 comments on commit d56c915

Please sign in to comment.