Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect version metadata #6595

Merged
merged 2 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions envoy/datadog_checks/envoy/envoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections import defaultdict

import requests
from six.moves.urllib.parse import urljoin

from datadog_checks.base import AgentCheck

Expand Down Expand Up @@ -52,6 +53,8 @@ def check(self, instance):
if self.caching_metrics is None:
self.caching_metrics = instance.get('cache_metrics', True)

self._collect_metadata(stats_url)

try:
response = self.http.get(stats_url)
except requests.exceptions.Timeout:
Expand Down Expand Up @@ -138,3 +141,33 @@ def whitelisted_metric(self, metric):
return not blacklisted
else:
return True

@AgentCheck.metadata_entrypoint
def _collect_metadata(self, stats_url):
# From http://domain/thing/stats to http://domain/thing/server_info
server_info_url = urljoin(stats_url, 'server_info')

try:
response = self.http.get(server_info_url)
if response.status_code != 200:
msg = 'Envoy endpoint `{}` responded with HTTP status code {}'.format(
server_info_url, response.status_code
)
self.log.info(msg)
return
# {
# "version": "222aaacccfff888/1.14.1/Clean/RELEASE/BoringSSL",
# "state": "LIVE",
# ...
# }
raw_version = response.json()["version"].split('/')[1]
except requests.exceptions.Timeout:
msg = 'Envoy endpoint `{}` timed out after {} seconds'.format(server_info_url, self.http.options['timeout'])
self.log.info(msg)
return
except (requests.exceptions.RequestException, requests.exceptions.ConnectionError):
msg = 'Error accessing Envoy endpoint `{}`'.format(server_info_url)
self.log.info(msg)
return

self.set_metadata('version', raw_version)
9 changes: 9 additions & 0 deletions envoy/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@
'metric_blacklist': [r'envoy\.cluster\.out\.'],
},
}
SERVER_INFO = {
"version": "222aaacccfff888/1.14.1/Clean/RELEASE/BoringSSL",
"state": "LIVE",
}
ENVOY_VERSION = os.getenv('ENVOY_VERSION')


class MockResponse:
def __init__(self, content, status_code):
self.content = content
self.status_code = status_code

def json(self):
# Metadata
return SERVER_INFO


@lru_cache(maxsize=None)
def response(kind):
Expand Down
2 changes: 1 addition & 1 deletion envoy/tests/docker/default/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3'
services:

front-envoy:
image: envoyproxy/envoy:${ENVOY_VERSION}
image: envoyproxy/envoy:v${ENVOY_VERSION}
command: ["/usr/local/bin/envoy", "-c", "/etc/front-envoy.yaml", "--service-cluster", "front-proxy"]
volumes:
- ./front-envoy.yaml:/etc/front-envoy.yaml
Expand Down
51 changes: 50 additions & 1 deletion envoy/tests/test_envoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

import mock
import pytest
import requests

from datadog_checks.envoy import Envoy
from datadog_checks.envoy.metrics import METRIC_PREFIX, METRICS

from .common import HOST, INSTANCES, response
from .common import ENVOY_VERSION, HOST, INSTANCES, response

CHECK_NAME = 'envoy'

Expand Down Expand Up @@ -120,3 +121,51 @@ def test_config(test_case, extra_config, expected_http_kwargs):
)
http_wargs.update(expected_http_kwargs)
r.get.assert_called_with('http://{}:8001/stats'.format(HOST), **http_wargs)


def test_metadata(datadog_agent):
instance = INSTANCES['main']
check = Envoy(CHECK_NAME, {}, [instance])
check.check_id = 'test:123'

with mock.patch('requests.get', side_effect=requests.exceptions.Timeout()):
check._collect_metadata(instance['stats_url'])
datadog_agent.assert_metadata_count(0)
with mock.patch('requests.get', side_effect=requests.exceptions.RequestException):
check._collect_metadata(instance['stats_url'])
datadog_agent.assert_metadata_count(0)

with mock.patch('requests.get', return_value=response('multiple_services')):
check._collect_metadata(instance['stats_url'])

major, minor, patch = ENVOY_VERSION.split('.')
version_metadata = {
'version.scheme': 'semver',
'version.major': major,
'version.minor': minor,
'version.patch': patch,
'version.raw': ENVOY_VERSION,
}

datadog_agent.assert_metadata('test:123', version_metadata)
datadog_agent.assert_metadata_count(len(version_metadata))


@pytest.mark.usefixtures('dd_environment')
def test_metadata_integration(aggregator, datadog_agent):
instance = INSTANCES['main']
c = Envoy(CHECK_NAME, {}, [instance])
c.check_id = 'test:123'
c.check(instance)

major, minor, patch = ENVOY_VERSION.split('.')
version_metadata = {
'version.scheme': 'semver',
'version.major': major,
'version.minor': minor,
'version.patch': patch,
'version.raw': ENVOY_VERSION,
}

datadog_agent.assert_metadata('test:123', version_metadata)
datadog_agent.assert_metadata_count(len(version_metadata))
2 changes: 1 addition & 1 deletion envoy/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ passenv =
COMPOSE*
setenv =
FLAVOR=default
ENVOY_VERSION=v1.14.1
ENVOY_VERSION=1.14.1
commands =
pip install -r requirements.in
pytest -v {posargs} --benchmark-skip
Expand Down