Skip to content

Commit

Permalink
Format datadog_checks_dev with black
Browse files Browse the repository at this point in the history
  • Loading branch information
vivek-datadog committed Aug 18, 2023
1 parent 7ac5ff1 commit 594c222
Show file tree
Hide file tree
Showing 229 changed files with 8,805 additions and 5,586 deletions.
2 changes: 1 addition & 1 deletion datadog_checks_dev/datadog_checks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
__path__ = __import__('pkgutil').extend_path(__path__, __name__) # type: ignore
__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore
2 changes: 1 addition & 1 deletion datadog_checks_dev/datadog_checks/dev/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
__version__ = '23.0.0'
__version__ = "23.0.0"
129 changes: 73 additions & 56 deletions datadog_checks_dev/datadog_checks/dev/_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@
from six import iteritems

DDTRACE_OPTIONS_LIST = [
'DD_TAGS',
'DD_TRACE*',
'DD_PROFILING*',
'DD_SERVICE',
'DD_AGENT_HOST',
'DD_ENV',
"DD_TAGS",
"DD_TRACE*",
"DD_PROFILING*",
"DD_SERVICE",
"DD_AGENT_HOST",
"DD_ENV",
]
E2E_PREFIX = 'DDEV_E2E'
E2E_ENV_VAR_PREFIX = '{}_ENV_'.format(E2E_PREFIX)
E2E_SET_UP = '{}_UP'.format(E2E_PREFIX)
E2E_TEAR_DOWN = '{}_DOWN'.format(E2E_PREFIX)
E2E_PARENT_PYTHON = '{}_PYTHON_PATH'.format(E2E_PREFIX)
E2E_PREFIX = "DDEV_E2E"
E2E_ENV_VAR_PREFIX = "{}_ENV_".format(E2E_PREFIX)
E2E_SET_UP = "{}_UP".format(E2E_PREFIX)
E2E_TEAR_DOWN = "{}_DOWN".format(E2E_PREFIX)
E2E_PARENT_PYTHON = "{}_PYTHON_PATH".format(E2E_PREFIX)

E2E_FIXTURE_NAME = 'dd_environment'
TESTING_PLUGIN = 'DDEV_TESTING_PLUGIN'
SKIP_ENVIRONMENT = 'DDEV_SKIP_ENV'
E2E_FIXTURE_NAME = "dd_environment"
TESTING_PLUGIN = "DDEV_TESTING_PLUGIN"
SKIP_ENVIRONMENT = "DDEV_SKIP_ENV"

JMX_TO_INAPP_TYPES = {
'counter': 'gauge', # JMX counter -> DSD gauge -> in-app gauge
'rate': 'gauge', # JMX rate -> DSD gauge -> in-app gauge
'monotonic_count': 'rate', # JMX monotonic_count -> DSD count -> in-app rate
"counter": "gauge", # JMX counter -> DSD gauge -> in-app gauge
"rate": "gauge", # JMX rate -> DSD gauge -> in-app gauge
"monotonic_count": "rate", # JMX monotonic_count -> DSD count -> in-app rate
# TODO: Support JMX histogram
# JMX histogram -> DSD histogram -> multiple in-app metrics (max, median, avg, count)
}

EVENT_PLATFORM_EVENT_TYPES = [
'dbm-samples',
'dbm-metrics',
'dbm-activity',
'network-devices-metadata',
"dbm-samples",
"dbm-metrics",
"dbm-activity",
"network-devices-metadata",
]


Expand All @@ -56,18 +56,22 @@ def e2e_testing():

def set_env_vars(env_vars):
for key, value in iteritems(env_vars):
key = '{}{}'.format(E2E_ENV_VAR_PREFIX, key)
key = "{}{}".format(E2E_ENV_VAR_PREFIX, key)
os.environ[key] = value


def remove_env_vars(env_vars):
for ev in env_vars:
os.environ.pop('{}{}'.format(E2E_ENV_VAR_PREFIX, ev), None)
os.environ.pop("{}{}".format(E2E_ENV_VAR_PREFIX, ev), None)


def get_env_vars(raw=False):
if raw:
return {key: value for key, value in iteritems(os.environ) if key.startswith(E2E_ENV_VAR_PREFIX)}
return {
key: value
for key, value in iteritems(os.environ)
if key.startswith(E2E_ENV_VAR_PREFIX)
}
else:
env_vars = {}

Expand All @@ -93,19 +97,19 @@ def save_state(key, value):


def set_up_env():
return os.getenv(E2E_SET_UP, 'true') != 'false'
return os.getenv(E2E_SET_UP, "true") != "false"


def tear_down_env():
return os.getenv(E2E_TEAR_DOWN, 'true') != 'false'
return os.getenv(E2E_TEAR_DOWN, "true") != "false"


def format_config(config):
if 'instances' not in config:
config = {'instances': [config]}
if "instances" not in config:
config = {"instances": [config]}

# Agent 5 requires init_config
if 'init_config' not in config:
if "init_config" not in config:
config = dict(init_config={}, **config)

return config
Expand All @@ -114,32 +118,34 @@ def format_config(config):
def replay_check_run(agent_collector, stub_aggregator, stub_agent):
errors = []
for collector in agent_collector:
aggregator = collector['aggregator']
inventories = collector.get('inventories')
runner = collector.get('runner', {})
check_id = runner.get('CheckID', '')
check_name = runner.get('CheckName', '')
aggregator = collector["aggregator"]
inventories = collector.get("inventories")
runner = collector.get("runner", {})
check_id = runner.get("CheckID", "")
check_name = runner.get("CheckName", "")

if inventories:
for metadata in inventories.values():
for meta_key, meta_val in metadata.items():
stub_agent.set_check_metadata(check_name, meta_key, meta_val)
for data in aggregator.get('metrics', []):
for _, value in data['points']:
raw_metric_type = data['type']
if data.get('source_type_name') == 'JMX':
raw_metric_type = JMX_TO_INAPP_TYPES.get(raw_metric_type, raw_metric_type)
for data in aggregator.get("metrics", []):
for _, value in data["points"]:
raw_metric_type = data["type"]
if data.get("source_type_name") == "JMX":
raw_metric_type = JMX_TO_INAPP_TYPES.get(
raw_metric_type, raw_metric_type
)
metric_type = stub_aggregator.METRIC_ENUM_MAP[raw_metric_type]
stub_aggregator.submit_metric_e2e(
# device is only present when replaying e2e tests. In integration tests it will be a tag
check_name,
check_id,
metric_type,
data['metric'],
data["metric"],
value,
data['tags'],
data['host'],
data.get('device'),
data["tags"],
data["host"],
data.get("device"),
)

for ep_event_type in EVENT_PLATFORM_EVENT_TYPES:
Expand All @@ -148,40 +154,51 @@ def replay_check_run(agent_collector, stub_aggregator, stub_agent):
stub_aggregator.submit_event_platform_event(
check_name,
check_id,
json.dumps(event['UnmarshalledEvent']),
event['EventType'],
json.dumps(event["UnmarshalledEvent"]),
event["EventType"],
)

for data in aggregator.get('service_checks', []):
for data in aggregator.get("service_checks", []):
stub_aggregator.submit_service_check(
check_name, check_id, data['check'], data['status'], data['tags'], data['host_name'], data['message']
check_name,
check_id,
data["check"],
data["status"],
data["tags"],
data["host_name"],
data["message"],
)

if runner.get('LastError'):
if runner.get("LastError"):
try:
new_errors = json.loads(runner['LastError'])
new_errors = json.loads(runner["LastError"])
except json.decoder.JSONDecodeError:
new_errors = [
{
'message': str(runner['LastError']),
'traceback': '',
"message": str(runner["LastError"]),
"traceback": "",
}
]
errors.extend(new_errors)
if errors:
raise Exception("\n".join("Message: {}\n{}".format(err['message'], err['traceback']) for err in errors))
raise Exception(
"\n".join(
"Message: {}\n{}".format(err["message"], err["traceback"])
for err in errors
)
)


def serialize_data(data):
data = json.dumps(data, separators=(',', ':'))
data = json.dumps(data, separators=(",", ":"))
# Using base64 ensures:
# 1. Printing to stdout won't fail
# 2. Easy parsing since there are no spaces
#
# TODO: Remove str() when we drop Python 2
return str(urlsafe_b64encode(data.encode('utf-8')).decode('utf-8'))
return str(urlsafe_b64encode(data.encode("utf-8")).decode("utf-8"))


def deserialize_data(data):
decoded = urlsafe_b64decode(data.encode('utf-8'))
return json.loads(decoded.decode('utf-8'))
decoded = urlsafe_b64decode(data.encode("utf-8"))
return json.loads(decoded.decode("utf-8"))
24 changes: 15 additions & 9 deletions datadog_checks_dev/datadog_checks/dev/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,47 @@
def running_on_gh_actions():
# GITHUB_ACTIONS always set to true when GitHub Actions is running the workflow.
# https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
return os.environ.get('GITHUB_ACTIONS') == 'true'
return os.environ.get("GITHUB_ACTIONS") == "true"


def running_on_ci():
return running_on_gh_actions() or _running_on_azp_ci()


def running_on_windows_ci():
return (running_on_gh_actions() and os.environ.get('RUNNER_OS') == 'Windows') or _running_on_azp_windows_ci()
return (
running_on_gh_actions() and os.environ.get("RUNNER_OS") == "Windows"
) or _running_on_azp_windows_ci()


def running_on_linux_ci():
return (running_on_gh_actions() and os.environ.get('RUNNER_OS') == 'Linux') or _running_on_azp_linux_ci()
return (
running_on_gh_actions() and os.environ.get("RUNNER_OS") == "Linux"
) or _running_on_azp_linux_ci()


def running_on_macos_ci():
return (running_on_gh_actions() and os.environ.get('RUNNER_OS') == 'macOS') or _running_on_azp_macos_ci()
return (
running_on_gh_actions() and os.environ.get("RUNNER_OS") == "macOS"
) or _running_on_azp_macos_ci()


def get_ci_env_vars():
# TODO: Remove this as it is unused
return ('AGENT_OS', 'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI')
return ("AGENT_OS", "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI")


def _running_on_azp_ci():
return 'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI' in os.environ
return "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI" in os.environ


def _running_on_azp_windows_ci():
return _running_on_azp_ci() and os.environ.get('AGENT_OS') == 'Windows_NT'
return _running_on_azp_ci() and os.environ.get("AGENT_OS") == "Windows_NT"


def _running_on_azp_linux_ci():
return _running_on_azp_ci() and os.environ.get('AGENT_OS') == 'Linux'
return _running_on_azp_ci() and os.environ.get("AGENT_OS") == "Linux"


def _running_on_azp_macos_ci():
return _running_on_azp_ci() and os.environ.get('AGENT_OS') == 'Darwin'
return _running_on_azp_ci() and os.environ.get("AGENT_OS") == "Darwin"
Loading

0 comments on commit 594c222

Please sign in to comment.