Skip to content

Commit

Permalink
Use project name and project group name tags
Browse files Browse the repository at this point in the history
  • Loading branch information
sarah-witt committed Nov 21, 2024
1 parent ce05336 commit 10ad093
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 43 deletions.
31 changes: 18 additions & 13 deletions octopus_deploy/datadog_checks/octopus_deploy/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,21 @@ def _process_project_groups(self, space_id, project_groups_config):
self.log.debug("Project Groups: %s", project_groups)
for _, _, project_group, project_group_config in project_groups:
project_group_id = project_group.get("Id")
project_group_name = project_group.get("Name")
tags = self._base_tags + [
f'space_id:{space_id}',
f'project_group_id:{project_group_id}',
f'project_group_name:{project_group.get("Name")}',
f'project_group_name:{project_group_name}',
]
self.gauge("project_group.count", 1, tags=tags)
self._process_projects(
space_id, project_group_id, project_group_config.get("projects") if project_group_config else None
space_id,
project_group_id,
project_group_name,
project_group_config.get("projects") if project_group_config else None,
)

def _process_projects(self, space_id, project_group_id, projects_config):
def _process_projects(self, space_id, project_group_id, project_group_name, projects_config):
if projects_config:
self._init_projects_discovery(space_id, project_group_id, Projects(**projects_config))
projects = list(self._projects_discovery[space_id][project_group_id].get_items())
Expand All @@ -192,29 +196,30 @@ def _process_projects(self, space_id, project_group_id, projects_config):
self.log.debug("Projects: %s", projects)
for _, _, project, _ in projects:
project_id = project.get("Id")
project_name = project.get("Name")
tags = self._base_tags + [
f'space_id:{space_id}',
f'project_group_id:{project_group_id}',
f'project_group_name:{project_group_name}',
f'project_id:{project_id}',
f'project_name:{project.get("Name")}',
f'project_name:{project_name}',
]
self.gauge("project.count", 1, tags=tags)
self._process_queued_and_running_tasks(space_id, project_id)
self._process_completed_tasks(space_id, project_id)
self._process_queued_and_running_tasks(space_id, project_id, project_name)
self._process_completed_tasks(space_id, project_id, project_name)

def _process_queued_and_running_tasks(self, space_id, project_id):
def _process_queued_and_running_tasks(self, space_id, project_id, project_name):
params = {'project': project_id, 'states': ["Queued", "Executing"]}
response_json = self._process_endpoint(f"api/{space_id}/tasks", params)
self._process_tasks(space_id, project_id, response_json.get('Items', []))
self._process_tasks(space_id, project_name, response_json.get('Items', []))

def _process_completed_tasks(self, space_id, project_id):
def _process_completed_tasks(self, space_id, project_id, project_name):
params = {
'project': project_id,
'fromCompletedDate': self._from_completed_time,
'toCompletedDate': self._to_completed_time,
}
response_json = self._process_endpoint(f"api/{space_id}/tasks", params)
self._process_tasks(space_id, project_id, response_json.get('Items', []))
self._process_tasks(space_id, project_name, response_json.get('Items', []))

def _calculate_task_times(self, task):
task_queue_time = task.get("QueueTime")
Expand Down Expand Up @@ -243,11 +248,11 @@ def _calculate_task_times(self, task):
completed_time = -1
return queued_time, executing_time, completed_time

def _process_tasks(self, space_id, project_id, tasks_json):
def _process_tasks(self, space_id, project_name, tasks_json):
for task in tasks_json:
tags = self._base_tags + [
f'space_id:{space_id}',
f'project_id:{project_id}',
f'project_name:{project_name}',
f'task_id:{task.get("Id")}',
f'task_name:{task.get("Name")}',
f'task_state:{task.get("State")}',
Expand Down
70 changes: 40 additions & 30 deletions octopus_deploy/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test_projects(get_current_datetime, dd_run_check, aggregator):
tags=[
'project_id:Projects-1',
'project_name:test-api',
'project_group_id:ProjectGroups-1',
'project_group_name:Default Project Group',
'space_id:Spaces-1',
],
)
Expand All @@ -169,19 +169,24 @@ def test_projects(get_current_datetime, dd_run_check, aggregator):
tags=[
'project_id:Projects-2',
'project_name:my-project',
'project_group_id:ProjectGroups-1',
'project_group_name:Default Project Group',
'space_id:Spaces-1',
],
)
aggregator.assert_metric(
'octopus_deploy.project.count',
1,
tags=['project_id:Projects-3', 'project_name:test', 'project_group_id:ProjectGroups-1', 'space_id:Spaces-1'],
tags=[
'project_id:Projects-3',
'project_name:test',
'project_group_name:Default Project Group',
'space_id:Spaces-1',
],
)
aggregator.assert_metric(
'octopus_deploy.project.count',
1,
tags=['project_id:Projects-4', 'project_name:hi', 'project_group_id:ProjectGroups-2', 'space_id:Spaces-1'],
tags=['project_id:Projects-4', 'project_name:hi', 'project_group_name:test-group', 'space_id:Spaces-1'],
)


Expand All @@ -201,7 +206,7 @@ def test_queued_or_running_tasks(get_current_datetime, dd_run_check, aggregator)
'task_id:ServerTasks-118048',
'task_name:Deploy',
'task_state:Executing',
'project_id:Projects-2',
'project_name:my-project',
'space_id:Spaces-1',
],
)
Expand All @@ -212,7 +217,7 @@ def test_queued_or_running_tasks(get_current_datetime, dd_run_check, aggregator)
'task_id:ServerTasks-118048',
'task_name:Deploy',
'task_state:Executing',
'project_id:Projects-2',
'project_name:my-project',
'space_id:Spaces-1',
],
)
Expand All @@ -223,7 +228,7 @@ def test_queued_or_running_tasks(get_current_datetime, dd_run_check, aggregator)
'task_id:ServerTasks-118048',
'task_name:Deploy',
'task_state:Executing',
'project_id:Projects-2',
'project_name:my-project',
'space_id:Spaces-1',
],
)
Expand All @@ -235,7 +240,7 @@ def test_queued_or_running_tasks(get_current_datetime, dd_run_check, aggregator)
'task_id:ServerTasks-118048',
'task_name:Deploy',
'task_state:Executing',
'project_id:Projects-2',
'project_name:my-project',
'space_id:Spaces-1',
],
)
Expand All @@ -246,7 +251,7 @@ def test_queued_or_running_tasks(get_current_datetime, dd_run_check, aggregator)
'task_id:ServerTasks-118055',
'task_name:Deploy',
'task_state:Queued',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -257,7 +262,7 @@ def test_queued_or_running_tasks(get_current_datetime, dd_run_check, aggregator)
'task_id:ServerTasks-118055',
'task_name:Deploy',
'task_state:Queued',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -269,7 +274,7 @@ def test_queued_or_running_tasks(get_current_datetime, dd_run_check, aggregator)
'task_id:ServerTasks-118055',
'task_name:Deploy',
'task_state:Queued',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -281,7 +286,7 @@ def test_queued_or_running_tasks(get_current_datetime, dd_run_check, aggregator)
'task_id:ServerTasks-118055',
'task_name:Deploy',
'task_state:Queued',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -297,8 +302,8 @@ def test_completed_tasks(get_current_datetime, dd_run_check, aggregator):
dd_run_check(check)
metrics = aggregator.metrics('octopus_deploy.deployment.count')
for metric in metrics:
assert not ('project_id:Projects-1' in metric.tags and 'task_state:Success' in metric.tags)
assert not ('project_id:Projects-3' in metric.tags and 'task_state:Success' in metric.tags)
assert not ('project_name:test-api' in metric.tags and 'task_state:Success' in metric.tags)
assert not ('project_name:test' in metric.tags and 'task_state:Success' in metric.tags)

get_current_datetime.return_value = MOCKED_TIME2
dd_run_check(check)
Expand All @@ -310,7 +315,7 @@ def test_completed_tasks(get_current_datetime, dd_run_check, aggregator):
'task_id:ServerTasks-1847',
'task_name:Deploy',
'task_state:Failed',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -321,7 +326,7 @@ def test_completed_tasks(get_current_datetime, dd_run_check, aggregator):
'task_id:ServerTasks-1847',
'task_name:Deploy',
'task_state:Failed',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -332,7 +337,7 @@ def test_completed_tasks(get_current_datetime, dd_run_check, aggregator):
'task_id:ServerTasks-1847',
'task_name:Deploy',
'task_state:Failed',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -343,7 +348,7 @@ def test_completed_tasks(get_current_datetime, dd_run_check, aggregator):
'task_id:ServerTasks-1847',
'task_name:Deploy',
'task_state:Failed',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -354,7 +359,7 @@ def test_completed_tasks(get_current_datetime, dd_run_check, aggregator):
'task_id:ServerTasks-1846',
'task_name:Deploy',
'task_state:Success',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -365,7 +370,7 @@ def test_completed_tasks(get_current_datetime, dd_run_check, aggregator):
'task_id:ServerTasks-1846',
'task_name:Deploy',
'task_state:Success',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -376,7 +381,7 @@ def test_completed_tasks(get_current_datetime, dd_run_check, aggregator):
'task_id:ServerTasks-1846',
'task_name:Deploy',
'task_state:Success',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -387,7 +392,7 @@ def test_completed_tasks(get_current_datetime, dd_run_check, aggregator):
'task_id:ServerTasks-1846',
'task_name:Deploy',
'task_state:Success',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -397,7 +402,7 @@ def test_completed_tasks(get_current_datetime, dd_run_check, aggregator):
'task_id:ServerTasks-1845',
'task_name:Deploy',
'task_state:Success',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -408,7 +413,7 @@ def test_completed_tasks(get_current_datetime, dd_run_check, aggregator):
'task_id:ServerTasks-1845',
'task_name:Deploy',
'task_state:Success',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -419,7 +424,7 @@ def test_completed_tasks(get_current_datetime, dd_run_check, aggregator):
'task_id:ServerTasks-1845',
'task_name:Deploy',
'task_state:Success',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand All @@ -430,7 +435,7 @@ def test_completed_tasks(get_current_datetime, dd_run_check, aggregator):
'task_id:ServerTasks-1845',
'task_name:Deploy',
'task_state:Success',
'project_id:Projects-3',
'project_name:test',
'space_id:Spaces-1',
],
)
Expand Down Expand Up @@ -571,7 +576,7 @@ def test_discovery_default_projects(get_current_datetime, dd_run_check, aggregat
tags=[
'project_id:Projects-1',
'project_name:test-api',
'project_group_id:ProjectGroups-1',
'project_group_name:Default Project Group',
'space_id:Spaces-1',
],
)
Expand All @@ -580,19 +585,24 @@ def test_discovery_default_projects(get_current_datetime, dd_run_check, aggregat
tags=[
'project_id:Projects-2',
'project_name:my-project',
'project_group_id:ProjectGroups-1',
'project_group_name:Default Project Group',
'space_id:Spaces-1',
],
count=0,
)
aggregator.assert_metric(
'octopus_deploy.project.count',
tags=['project_id:Projects-3', 'project_name:test', 'project_group_id:ProjectGroups-1', 'space_id:Spaces-1'],
tags=[
'project_name:test',
'project_name:test',
'project_group_name:Default Project Group',
'space_id:Spaces-1',
],
count=0,
)
aggregator.assert_metric(
'octopus_deploy.project.count',
tags=['project_id:Projects-4', 'project_name:hi', 'project_group_id:ProjectGroups-2', 'space_id:Spaces-1'],
tags=['project_id:Projects-4', 'project_name:hi', 'project_group_name:test-group', 'space_id:Spaces-1'],
count=0,
)

Expand Down

0 comments on commit 10ad093

Please sign in to comment.