Skip to content

Commit

Permalink
Fix version parsing (DataDog#9423)
Browse files Browse the repository at this point in the history
* Fix version parsing
  • Loading branch information
hithwen authored May 26, 2021
1 parent fc0dfd0 commit 9611293
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
38 changes: 22 additions & 16 deletions glusterfs/datadog_checks/glusterfs/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import json

try:
from json import JSONDecodeError
from json.decoder import JSONDecodeError
except ImportError:
from simplejson import JSONDecodeError

import os
import shlex
from typing import Any
from typing import Dict, List

from six import iteritems

Expand All @@ -34,7 +34,7 @@ class GlusterfsCheck(AgentCheck):
BRICK_SC = "brick.health"

def __init__(self, name, init_config, instances):
# type: (*Any, **Any) -> None
# type: (str, Dict, List[Dict]) -> None
super(GlusterfsCheck, self).__init__(name, init_config, instances)
self._tags = self.instance.get('tags', [])

Expand Down Expand Up @@ -102,29 +102,35 @@ def check(self, _):

@AgentCheck.metadata_entrypoint
def submit_version_metadata(self, data):
try:
raw_version = data[GLUSTER_VERSION]
except KeyError as e:
self.log.debug("Could not retrieve GlusterFS version: %s", str(e))
raw_version = data.get(GLUSTER_VERSION)
if not raw_version:
self.log.warning('Could not retrieve GlusterFS version info: %s', raw_version)
return

if raw_version:
major, minor = self.parse_version(raw_version)
self.log.debug('Found GlusterFS version: %s', raw_version)
try:
major, minor, patch = self.parse_version(raw_version)
version_parts = {'major': str(int(major)), 'minor': str(int(minor))}
if patch:
version_parts['patch'] = str(int(patch))
self.set_metadata('version', raw_version, scheme='parts', part_map=version_parts)
self.log.debug('Found GlusterFS version: %s', raw_version)
else:
self.log.warning('Could not retrieve GlusterFS version info: %s', raw_version)
except Exception as e:
self.log.debug("Could not handle GlusterFS version: %s", str(e))

def parse_version(self, version):
# type (str) -> str, str, str
"""
GlusterFS versions are in format <major>.<minor>
"""
major, minor, patch = None, None, None
try:
major, minor = version.split('.')
split_version = version.split('.')
major, minor = split_version[0:2]
if len(split_version) > 2:
patch = split_version[2]
except ValueError as e:
self.log.debug("Unable to parse GlusterFS version: %s", str(e))
else:
return major, minor
self.log.debug("Unable to parse GlusterFS version %s: %s", str(version), str(e))
return major, minor, patch

def parse_volume_summary(self, output):
for volume in output:
Expand Down
14 changes: 11 additions & 3 deletions glusterfs/tests/test_glusterfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@pytest.mark.unit
def test_check(aggregator, instance, mock_gstatus_data):
# type: (AggregatorStub, Dict[str, Any]) -> None
# type: (AggregatorStub, Dict[str, Any], str) -> None
check = GlusterfsCheck(CHECK, E2E_INIT_CONFIG, [instance])
check.check(instance)

Expand All @@ -30,8 +30,7 @@ def test_version_metadata(aggregator, datadog_agent, instance):
c = GlusterfsCheck(CHECK, E2E_INIT_CONFIG, [instance])
c.check_id = 'test:123'
c.check(instance)

major, minor = c.parse_version(GLUSTER_VERSION)
major, minor, patch = c.parse_version(GLUSTER_VERSION)

version_metadata = {
'version.raw': GLUSTER_VERSION,
Expand All @@ -42,3 +41,12 @@ def test_version_metadata(aggregator, datadog_agent, instance):

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


@pytest.mark.unit
def test_parse_version(instance):
c = GlusterfsCheck(CHECK, E2E_INIT_CONFIG, [instance])
major, minor, patch = c.parse_version('3.13.2')
assert major == '3'
assert minor == '13'
assert patch == '2'
2 changes: 2 additions & 0 deletions glusterfs/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ envdir =
py27: {toxworkdir}/py27
py38: {toxworkdir}/py38
dd_check_style = true
dd_check_types = true
dd_mypy_args = --py2 datadog_checks/ tests/ --exclude '.*/config_models/.*\.py$'
usedevelop = true
platform = linux|darwin|win32
deps =
Expand Down

0 comments on commit 9611293

Please sign in to comment.