Skip to content

Commit

Permalink
Add version metadata (#5603)
Browse files Browse the repository at this point in the history
* Collect version info for squid

* Simplify regex and add patches to tests

* Update unit tests and change to submit_version

* Remove uneeded version variable

* Remove hardcoded version

* Add comment explaining squid tests

* Add patch to test

* Update squid/datadog_checks/squid/squid.py

Co-Authored-By: Christine Chen <ChristineTChen@users.noreply.github.com>

* Remove log

Co-authored-by: Christine Chen <ChristineTChen@users.noreply.github.com>
  • Loading branch information
sarah-witt and ChristineTChen authored Feb 4, 2020
1 parent b6c991b commit a2f8671
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 19 deletions.
25 changes: 24 additions & 1 deletion squid/datadog_checks/squid/squid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)

import re

import requests
from six import iteritems

Expand Down Expand Up @@ -67,6 +69,8 @@
"aborted_requests",
]

VERSION_REGEX = re.compile(r".*/(.*)")


class SquidCheck(AgentCheck):
HTTP_CONFIG_REMAPPER = {'cachemgr_username': {'name': 'username'}, 'cachemgr_password': {'name': 'password'}}
Expand All @@ -75,7 +79,6 @@ def check(self, instance):

name, host, port, custom_tags = self.parse_instance(instance)
tags = ["name:%s" % name]

# Get the squid counters values
counters = self.get_counters(host, port, tags + custom_tags)

Expand All @@ -90,6 +93,9 @@ def get_counters(self, host, port, tags):
res = self.http.get(url)
res.raise_for_status()
self.service_check(SERVICE_CHECK, AgentCheck.OK, tags=tags)
headers = res.headers
self.submit_version(headers)

except requests.exceptions.RequestException as e:
self.service_check(SERVICE_CHECK, AgentCheck.CRITICAL, tags=tags)
self.log.error('There was an error connecting to squid at %s: %s', url, e)
Expand Down Expand Up @@ -129,3 +135,20 @@ def parse_counter(self, line):
return None, None

return counter, value

def submit_version(self, headers):
server_version = headers.get("Server", "")

match = VERSION_REGEX.match(server_version)
if match is None:
self.log.debug("Squid version is unknown: %", server_version)
return None

version = match.group(1)

if version is not None:
self.set_metadata('version', version)
self.log.debug("Squid version %s metadata submitted", version)

else:
self.log.debug("Squid version %s not valid version", server_version)
1 change: 0 additions & 1 deletion squid/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
PORT = '3128'
URL = 'http://{}:{}/squid-internal-mgr/counters'.format(HOST, PORT)
SERVICE_CHECK = "squid.can_connect"

CHECK_CONFIG = {"name": "ok_instance", "tags": ["custom_tag"], "host": HOST}

EXPECTED_METRICS = [
Expand Down
44 changes: 44 additions & 0 deletions squid/tests/test_squid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)

import mock
import pytest

from . import common
Expand All @@ -26,3 +27,46 @@ def test_check_ok(aggregator, check, instance):
for metric in common.EXPECTED_METRICS:
aggregator.assert_metric("squid.cachemgr." + metric, tags=expected_tags)
aggregator.assert_all_metrics_covered()


@pytest.mark.parametrize(
'raw_version, version_metadata, count',
[
(
'squid/3.0.3',
{
'version.scheme': 'semver',
'version.major': '3',
'version.minor': '0',
'version.patch': '3',
'version.raw': '3.0.3',
},
5,
),
(
'squid/1.4.5',
{
'version.scheme': 'semver',
'version.major': '1',
'version.minor': '4',
'version.patch': '5',
'version.raw': '1.4.5',
},
5,
),
# these versions aren't valid squid versions, so the version metadata should not be submitted
('squid/1.3', {}, 0),
('squid/1', {}, 0,),
('1.4.5', {}, 0,),
],
)
@pytest.mark.usefixtures("dd_environment")
def test_version_metadata(check, instance, datadog_agent, raw_version, version_metadata, count):
with mock.patch('datadog_checks.base.utils.http.requests.get') as g:
g.return_value.headers = {'Server': raw_version}

check.check_id = 'test:123'
check.check(instance)

datadog_agent.assert_metadata('test:123', version_metadata)
datadog_agent.assert_metadata_count(count)
36 changes: 19 additions & 17 deletions squid/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ def test_get_counters(check):
See https://github.com/DataDog/integrations-core/pull/1643
"""
with mock.patch('datadog_checks.squid.squid.requests.get') as g:
g.return_value = mock.MagicMock(text="client_http.requests=42\n\n")
check.parse_counter = mock.MagicMock(return_value=('foo', 'bar'))
check.get_counters('host', 'port', [])
# we assert `parse_counter` was called only once despite the raw text
# containing multiple `\n` chars
check.parse_counter.assert_called_once()
with mock.patch('datadog_checks.squid.SquidCheck.submit_version'):
g.return_value = mock.MagicMock(text="client_http.requests=42\n\n")
check.parse_counter = mock.MagicMock(return_value=('foo', 'bar'))
check.get_counters('host', 'port', [])
# we assert `parse_counter` was called only once despite the raw text
# containing multiple `\n` chars
check.parse_counter.assert_called_once()


@pytest.mark.parametrize(
Expand All @@ -84,14 +85,15 @@ def test_legacy_username_password(instance, auth_config):
check = SquidCheck(common.CHECK_NAME, {}, {}, [instance])

with mock.patch('datadog_checks.base.utils.http.requests.get') as g:
check.get_counters('host', 'port', [])

g.assert_called_with(
'http://host:port/squid-internal-mgr/counters',
auth=('datadog_user', 'datadog_pass'),
cert=mock.ANY,
headers=mock.ANY,
proxies=mock.ANY,
timeout=mock.ANY,
verify=mock.ANY,
)
with mock.patch('datadog_checks.squid.SquidCheck.submit_version'):
check.get_counters('host', 'port', [])

g.assert_called_with(
'http://host:port/squid-internal-mgr/counters',
auth=('datadog_user', 'datadog_pass'),
cert=mock.ANY,
headers=mock.ANY,
proxies=mock.ANY,
timeout=mock.ANY,
verify=mock.ANY,
)

0 comments on commit a2f8671

Please sign in to comment.