Skip to content

Commit

Permalink
Add HTTP option to control the size of streaming responses (DataDog#1…
Browse files Browse the repository at this point in the history
…0183)

* Add HTTP option to control the size of streaming responses

* wrap `requests.Response` objects

* sync config

* fix

* update example config
  • Loading branch information
ofek authored Sep 28, 2021
1 parent 27c1070 commit b1d2b0f
Show file tree
Hide file tree
Showing 138 changed files with 564 additions and 40 deletions.
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@ typing,PyPI,PSF,"Guido van Rossum, Jukka Lehtosalo, Łukasz Langa, Ivan Levkivsk
uptime,PyPI,BSD-2-Clause,Koen Crolla
vertica-python,PyPI,Apache-2.0,"Justin Berka, Alex Kim, Siting Ren"
win-inet-pton,PyPI,Unlicense,Ryan Vennell
wrapt,PyPI,BSD-3-Clause,Graham Dumpleton
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 16

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
5 changes: 5 additions & 0 deletions airflow/datadog_checks/airflow/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 16

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
10 changes: 5 additions & 5 deletions amazon_msk/datadog_checks/amazon_msk/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,6 @@ instances:
#
# cache_shared_labels: true

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from the `openmetrics_endpoint` at a time.
#
# request_size: 16

## @param raw_line_filters - list of strings - optional
## A list of regular expressions used to exclude lines read from the `openmetrics_endpoint`
## from being parsed.
Expand Down Expand Up @@ -553,6 +548,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 16

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
5 changes: 5 additions & 0 deletions ambari/datadog_checks/ambari/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 16

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
4 changes: 4 additions & 0 deletions apache/datadog_checks/apache/config_models/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def instance_read_timeout(field, value):
return get_default_field_value(field, value)


def instance_request_size(field, value):
return 16


def instance_service(field, value):
return get_default_field_value(field, value)

Expand Down
1 change: 1 addition & 0 deletions apache/datadog_checks/apache/config_models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Config:
persist_connections: Optional[bool]
proxy: Optional[Proxy]
read_timeout: Optional[float]
request_size: Optional[float]
service: Optional[str]
skip_proxy: Optional[bool]
tags: Optional[Sequence[str]]
Expand Down
5 changes: 5 additions & 0 deletions apache/datadog_checks/apache/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 16

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
10 changes: 5 additions & 5 deletions avi_vantage/datadog_checks/avi_vantage/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,6 @@ instances:
#
# cache_shared_labels: true

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from the `openmetrics_endpoint` at a time.
#
# request_size: 16

## @param raw_line_filters - list of strings - optional
## A list of regular expressions used to exclude lines read from the `openmetrics_endpoint`
## from being parsed.
Expand Down Expand Up @@ -529,6 +524,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 16

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 10
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 10

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
5 changes: 5 additions & 0 deletions cilium/datadog_checks/cilium/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 10
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 10

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
5 changes: 5 additions & 0 deletions cisco_aci/datadog_checks/cisco_aci/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 16

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def instance_read_timeout(field, value):
return get_default_field_value(field, value)


def instance_request_size(field, value):
return 16


def instance_service(field, value):
return get_default_field_value(field, value)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Config:
persist_connections: Optional[bool]
proxy: Optional[Proxy]
read_timeout: Optional[float]
request_size: Optional[float]
service: Optional[str]
skip_proxy: Optional[bool]
tags: Optional[Sequence[str]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 16

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 16

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
5 changes: 5 additions & 0 deletions cockroachdb/datadog_checks/cockroachdb/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 10
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 10

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
4 changes: 4 additions & 0 deletions consul/datadog_checks/consul/config_models/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def instance_read_timeout(field, value):
return get_default_field_value(field, value)


def instance_request_size(field, value):
return 16


def instance_self_leader_check(field, value):
return False

Expand Down
1 change: 1 addition & 0 deletions consul/datadog_checks/consul/config_models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Config:
persist_connections: Optional[bool]
proxy: Optional[Proxy]
read_timeout: Optional[float]
request_size: Optional[float]
self_leader_check: Optional[bool]
service: Optional[str]
services_exclude: Optional[Sequence[str]]
Expand Down
5 changes: 5 additions & 0 deletions consul/datadog_checks/consul/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 16

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
5 changes: 5 additions & 0 deletions coredns/datadog_checks/coredns/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 10
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 10

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
5 changes: 5 additions & 0 deletions couch/datadog_checks/couch/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 16

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
5 changes: 5 additions & 0 deletions couchbase/datadog_checks/couchbase/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 16
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 16

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
5 changes: 5 additions & 0 deletions crio/datadog_checks/crio/data/conf.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,11 @@ instances:
#
# read_timeout: <READ_TIMEOUT>

## @param request_size - number - optional - default: 10
## The number of kibibytes (KiB) to read from streaming HTTP responses at a time.
#
# request_size: 10

## @param log_requests - boolean - optional - default: false
## Whether or not to debug log the HTTP(S) requests made, including the method and URL.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class OpenMetricsBaseCheck(OpenMetricsScraperMixin, AgentCheck):
'ssl_private_key': {'name': 'tls_private_key'},
'ssl_ca_cert': {'name': 'tls_ca_cert'},
'prometheus_timeout': {'name': 'timeout'},
'request_size': {'name': 'request_size', 'default': 10},
}

def __init__(self, *args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class OpenMetricsScraperMixin(object):
# This class is not supposed to be used by itself, it provides scraping behavior but
# need to be within a check in the end

REQUESTS_CHUNK_SIZE = 1024 * 10 # use 10kb as chunk size when using the Stream feature in requests.get
# indexes in the sample tuple of core.Metric
SAMPLE_NAME = 0
SAMPLE_LABELS = 1
Expand Down Expand Up @@ -421,7 +420,7 @@ def parse_metric_family(self, response, scraper_config):
"""
if response.encoding is None:
response.encoding = 'utf-8'
input_gen = response.iter_lines(chunk_size=self.REQUESTS_CHUNK_SIZE, decode_unicode=True)
input_gen = response.iter_lines(decode_unicode=True)
if scraper_config['_text_filter_blacklist']:
input_gen = self._text_filter_input(input_gen, scraper_config)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from itertools import chain
from math import isinf, isnan

from binary import KIBIBYTE
from prometheus_client.openmetrics.parser import text_fd_to_metric_families as parse_metric_families_strict
from prometheus_client.parser import text_fd_to_metric_families as parse_metric_families

Expand Down Expand Up @@ -154,10 +153,6 @@ def __init__(self, check, config):
ignored_tags_re = re.compile('|'.join(set(ignore_tags)))
custom_tags = [tag for tag in custom_tags if not ignored_tags_re.search(tag)]

# 16 KiB seems optimal, and is also the standard chunk size of the Bittorrent protocol:
# https://www.bittorrent.org/beps/bep_0003.html
self.request_size = int(float(config.get('request_size') or 16) * KIBIBYTE)

# These will be applied only to service checks
self.static_tags = [f'endpoint:{self.endpoint}']
self.static_tags.extend(custom_tags)
Expand Down Expand Up @@ -276,7 +271,7 @@ def generate_sample_data(self, metric):

def stream_connection_lines(self):
with self.get_connection() as connection:
for line in connection.iter_lines(chunk_size=self.request_size, decode_unicode=True):
for line in connection.iter_lines(decode_unicode=True):
yield line

def filter_connection_lines(self, line_streamer):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@ typing==3.7.4.1; python_version < "3.0"
uptime==3.0.1
vertica-python==0.10.1
win-inet-pton==1.1.0; sys_platform == "win32" and python_version < "3.0"
wrapt==1.12.1
Loading

0 comments on commit b1d2b0f

Please sign in to comment.