def _report(self, metric, metric_type, value, tags, sample_rate): """ Create a metric packet and send it. More information about the packets' format: http://docs.datadoghq.com/guides/dogstatsd/ """ if value is None: return if sample_rate != 1 and random() > sample_rate: return # Resolve the full tag list if self.constant_tags: if tags: tags = tags + self.constant_tags else: tags = self.constant_tags # Create/format the metric packet payload = "%s%s:%s|%s%s%s" % ( (self.namespace + ".") if self.namespace else "", metric, value, metric_type, ("|@" + text(sample_rate)) if sample_rate != 1 else "", ("|#" + ",".join(tags)) if tags else "", ) # Send it self._send(payload)
def _serialize_metric(self, metric, metric_type, value, tags, sample_rate=1): # Create/format the metric packet return "%s%s:%s|%s%s%s" % ( (self.namespace + ".") if self.namespace else "", metric, value, metric_type, ("|@" + text(sample_rate)) if sample_rate != 1 else "", ("|#" + ",".join(normalize_tags(tags))) if tags else "", )
def initialize(api_key=None, app_key=None, host_name=None, api_host=None, statsd_host=None, statsd_port=None, statsd_use_default_route=False, statsd_socket_path=None, statsd_namespace=None, **kwargs): """ Initialize and configure Datadog.api and Datadog.statsd modules :param api_key: Datadog API key :type api_key: string :param app_key: Datadog application key :type app_key: string :param proxies: Proxy to use to connect to Datadog API; for example, 'proxies': {'http': "http:<user>:<pass>@<ip>:<port>/"} :type proxies: dictionary mapping protocol to the URL of the proxy. :param api_host: Datadog API endpoint :type api_host: url :param statsd_host: Host of DogStatsd server or statsd daemon :type statsd_host: address :param statsd_port: Port of DogStatsd server or statsd daemon :type statsd_port: port :param statsd_use_default_route: Dynamically set the statsd host to the default route (Useful when running the client in a container) :type statsd_use_default_route: boolean :param statsd_socket_path: path to the DogStatsd UNIX socket. Supersedes statsd_host and stats_port if provided. :param cacert: Path to local certificate file used to verify SSL \ certificates. Can also be set to True (default) to use the systems \ certificate store, or False to skip SSL verification :type cacert: path or boolean :param mute: Mute any ApiError or ClientError before they escape \ from datadog.api.HTTPClient (default: True). :type mute: boolean """ # API configuration api._api_key = api_key or api._api_key or os.environ.get( 'DATADOG_API_KEY', os.environ.get('DD_API_KEY')) api._application_key = (app_key or api._application_key or os.environ.get( 'DATADOG_APP_KEY', os.environ.get('DD_APP_KEY'))) api._host_name = host_name or api._host_name or get_hostname() api._api_host = api_host or api._api_host or os.environ.get( 'DATADOG_HOST', 'https://api.datadoghq.com') # Statsd configuration # ...overrides the default `statsd` instance attributes if statsd_socket_path: statsd.socket_path = statsd_socket_path statsd.host = None statsd.port = None else: if statsd_host or statsd_use_default_route: statsd.host = statsd.resolve_host(statsd_host, statsd_use_default_route) if statsd_port: statsd.port = int(statsd_port) if statsd_namespace: statsd.namespace = text(statsd_namespace) # HTTP client and API options for key, value in iteritems(kwargs): attribute = "_{}".format(key) setattr(api, attribute, value)
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, max_buffer_size=50, namespace=None, constant_tags=None, use_ms=False, use_default_route=False, socket_path=None, default_sample_rate=1, disable_telemetry=False): """ Initialize a DogStatsd object. >>> statsd = DogStatsd() :envvar DD_AGENT_HOST: the host of the DogStatsd server. If set, it overrides default value. :type DD_AGENT_HOST: string :envvar DD_DOGSTATSD_PORT: the port of the DogStatsd server. If set, it overrides default value. :type DD_DOGSTATSD_PORT: integer :param host: the host of the DogStatsd server. :type host: string :param port: the port of the DogStatsd server. :type port: integer :param max_buffer_size: Maximum number of metrics to buffer before sending to the server if sending metrics in batch :type max_buffer_size: integer :param namespace: Namespace to prefix all metric names :type namespace: string :param constant_tags: Tags to attach to all metrics :type constant_tags: list of strings :param use_ms: Report timed values in milliseconds instead of seconds (default False) :type use_ms: boolean :envvar DATADOG_TAGS: Tags to attach to every metric reported by dogstatsd client :type DATADOG_TAGS: list of strings :envvar DD_ENTITY_ID: Tag to identify the client entity. :type DD_ENTITY_ID: string :param use_default_route: Dynamically set the DogStatsd host to the default route (Useful when running the client in a container) (Linux only) :type use_default_route: boolean :param socket_path: Communicate with dogstatsd through a UNIX socket instead of UDP. If set, disables UDP transmission (Linux only) :type socket_path: string :param default_sample_rate: Sample rate to use by default for all metrics :type default_sample_rate: float """ self.lock = Lock() # Check host and port env vars agent_host = os.environ.get('DD_AGENT_HOST') if agent_host and host == DEFAULT_HOST: host = agent_host dogstatsd_port = os.environ.get('DD_DOGSTATSD_PORT') if dogstatsd_port and port == DEFAULT_PORT: try: port = int(dogstatsd_port) except ValueError: log.warning( "Port number provided in DD_DOGSTATSD_PORT env var is not an integer: \ %s, using %s as port number", dogstatsd_port, port) # Connection if socket_path is not None: self.socket_path = socket_path self.host = None self.port = None transport = "uds" else: self.socket_path = None self.host = self.resolve_host(host, use_default_route) self.port = int(port) transport = "udp" # Socket self.socket = None self.max_buffer_size = max_buffer_size self._send = self._send_to_server self.encoding = 'utf-8' # Options env_tags = [ tag for tag in os.environ.get('DATADOG_TAGS', '').split(',') if tag ] if constant_tags is None: constant_tags = [] self.constant_tags = constant_tags + env_tags entity_id = os.environ.get('DD_ENTITY_ID') if entity_id: entity_tag = '{name}:{value}'.format(name=ENTITY_ID_TAG_NAME, value=entity_id) self.constant_tags.append(entity_tag) if namespace is not None: namespace = text(namespace) self.namespace = namespace self.use_ms = use_ms self.default_sample_rate = default_sample_rate # init telemetry self._client_tags = [ "client:py", "client_version:{}".format(get_version()), "client_transport:{}".format(transport), ] self._reset_telementry() self._telemetry = not disable_telemetry
def __init__(self, host='localhost', port=8125, max_buffer_size=50, namespace=None, constant_tags=None, use_ms=False, use_default_route=False, socket_path=None): """ Initialize a DogStatsd object. >>> statsd = DogStatsd() :param host: the host of the DogStatsd server. :type host: string :param port: the port of the DogStatsd server. :type port: integer :param max_buffer_size: Maximum number of metrics to buffer before sending to the server if sending metrics in batch :type max_buffer_size: integer :param namespace: Namespace to prefix all metric names :type namespace: string :param constant_tags: Tags to attach to all metrics :type constant_tags: list of strings :param use_ms: Report timed values in milliseconds instead of seconds (default False) :type use_ms: boolean :envvar DATADOG_TAGS: Tags to attach to every metric reported by dogstatsd client :type constant_tags: list of strings :param use_default_route: Dynamically set the DogStatsd host to the default route (Useful when running the client in a container) (Linux only) :type use_default_route: boolean :param socket_path: Communicate with dogstatsd through a UNIX socket instead of UDP. If set, disables UDP transmission (Linux only) :type socket_path: string """ # Connection if socket_path is not None: self.socket_path = socket_path self.host = None self.port = None else: self.socket_path = None self.host = self.resolve_host(host, use_default_route) self.port = int(port) # Socket self.socket = None self.max_buffer_size = max_buffer_size self._send = self._send_to_server self.encoding = 'utf-8' # Options env_tags = [ tag for tag in os.environ.get('DATADOG_TAGS', '').split(',') if tag ] if constant_tags is None: constant_tags = [] self.constant_tags = constant_tags + env_tags if namespace is not None: namespace = text(namespace) self.namespace = namespace self.use_ms = use_ms
def __init__( self, host=DEFAULT_HOST, # type: Text port=DEFAULT_PORT, # type: int max_buffer_size=None, # type: None namespace=None, # type: Optional[Text] constant_tags=None, # type: Optional[List[str]] use_ms=False, # type: bool use_default_route=False, # type: bool socket_path=None, # type: Optional[Text] default_sample_rate=1, # type: float disable_telemetry=False, # type: bool telemetry_min_flush_interval=( DEFAULT_TELEMETRY_MIN_FLUSH_INTERVAL), # type: int telemetry_host=None, # type: Text telemetry_port=None, # type: Union[str, int] telemetry_socket_path=None, # type: Text max_buffer_len=0, # type: int ): # type: (...) -> None """ Initialize a DogStatsd object. >>> statsd = DogStatsd() :envvar DD_AGENT_HOST: the host of the DogStatsd server. If set, it overrides default value. :type DD_AGENT_HOST: string :envvar DD_DOGSTATSD_PORT: the port of the DogStatsd server. If set, it overrides default value. :type DD_DOGSTATSD_PORT: integer :envvar DATADOG_TAGS: Tags to attach to every metric reported by dogstatsd client. :type DATADOG_TAGS: comma-delimited string :envvar DD_ENTITY_ID: Tag to identify the client entity. :type DD_ENTITY_ID: string :envvar DD_ENV: the env of the service running the dogstatsd client. If set, it is appended to the constant (global) tags of the statsd client. :type DD_ENV: string :envvar DD_SERVICE: the name of the service running the dogstatsd client. If set, it is appended to the constant (global) tags of the statsd client. :type DD_SERVICE: string :envvar DD_VERSION: the version of the service running the dogstatsd client. If set, it is appended to the constant (global) tags of the statsd client. :type DD_VERSION: string :envvar DD_DOGSTATSD_DISABLE: Disable any statsd metric collection (default False) :type DD_DOGSTATSD_DISABLE: boolean :envvar DD_TELEMETRY_HOST: the host for the dogstatsd server we wish to submit telemetry stats to. If set, it overrides default value. :type DD_TELEMETRY_HOST: string :envvar DD_TELEMETRY_PORT: the port for the dogstatsd server we wish to submit telemetry stats to. If set, it overrides default value. :type DD_TELEMETRY_PORT: integer :param host: the host of the DogStatsd server. :type host: string :param port: the port of the DogStatsd server. :type port: integer :max_buffer_size: Deprecated option, do not use it anymore. :type max_buffer_type: None :param namespace: Namespace to prefix all metric names :type namespace: string :param constant_tags: Tags to attach to all metrics :type constant_tags: list of strings :param use_ms: Report timed values in milliseconds instead of seconds (default False) :type use_ms: boolean :param use_default_route: Dynamically set the DogStatsd host to the default route (Useful when running the client in a container) (Linux only) :type use_default_route: boolean :param socket_path: Communicate with dogstatsd through a UNIX socket instead of UDP. If set, disables UDP transmission (Linux only) :type socket_path: string :param default_sample_rate: Sample rate to use by default for all metrics :type default_sample_rate: float :param max_buffer_len: Maximum number of bytes to buffer before sending to the server if sending metrics in batch. If not specified it will be adjusted to a optimal value depending on the connection type. :type max_buffer_len: integer :param disable_telemetry: Should client telemetry be disabled :type disable_telemetry: boolean :param telemetry_min_flush_interval: Minimum flush interval for telemetry in seconds :type telemetry_min_flush_interval: integer :param telemetry_host: the host for the dogstatsd server we wish to submit telemetry stats to. Optional. If telemetry is enabled and this is not specified the default host will be used. :type host: string :param telemetry_port: the port for the dogstatsd server we wish to submit telemetry stats to. Optional. If telemetry is enabled and this is not specified the default host will be used. :type port: integer :param telemetry_socket_path: Submit client telemetry to dogstatsd through a UNIX socket instead of UDP. If set, disables UDP transmission (Linux only) :type telemetry_socket_path: string """ self._socket_lock = Lock() # Check for deprecated option if max_buffer_size is not None: log.warning( "The parameter max_buffer_size is now deprecated and is not used anymore" ) # Check host and port env vars agent_host = os.environ.get("DD_AGENT_HOST") if agent_host and host == DEFAULT_HOST: host = agent_host dogstatsd_port = os.environ.get("DD_DOGSTATSD_PORT") if dogstatsd_port and port == DEFAULT_PORT: try: port = int(dogstatsd_port) except ValueError: log.warning( "Port number provided in DD_DOGSTATSD_PORT env var is not an integer: \ %s, using %s as port number", dogstatsd_port, port, ) # Assuming environment variables always override telemetry_host = os.environ.get("DD_TELEMETRY_HOST", telemetry_host) telemetry_port = os.environ.get("DD_TELEMETRY_PORT", telemetry_port) or port # Check enabled if os.environ.get("DD_DOGSTATSD_DISABLE") not in { "True", "true", "yes", "1" }: self._enabled = True else: self._enabled = False # Connection self._max_payload_size = max_buffer_len if socket_path is not None: self.socket_path = socket_path # type: Optional[text] self.host = None self.port = None transport = "uds" if not self._max_payload_size: self._max_payload_size = UDS_OPTIMAL_PAYLOAD_LENGTH else: self.socket_path = None self.host = self.resolve_host(host, use_default_route) self.port = int(port) transport = "udp" if not self._max_payload_size: self._max_payload_size = UDP_OPTIMAL_PAYLOAD_LENGTH self.telemetry_socket_path = telemetry_socket_path self.telemetry_host = None self.telemetry_port = None if not telemetry_socket_path and telemetry_host: self.telemetry_socket_path = None self.telemetry_host = self.resolve_host(telemetry_host, use_default_route) self.telemetry_port = int(telemetry_port) # Socket self.socket = None self.telemetry_socket = None self._send = self._send_to_server self.encoding = "utf-8" # Options env_tags = [ tag for tag in os.environ.get("DATADOG_TAGS", "").split(",") if tag ] # Inject values of DD_* environment variables as global tags. for var, tag_name in DD_ENV_TAGS_MAPPING.items(): value = os.environ.get(var, "") if value: env_tags.append("{name}:{value}".format(name=tag_name, value=value)) if constant_tags is None: constant_tags = [] self.constant_tags = constant_tags + env_tags if namespace is not None: namespace = text(namespace) self.namespace = namespace self.use_ms = use_ms self.default_sample_rate = default_sample_rate # init telemetry version self._client_tags = [ "client:py", "client_version:{}".format(__version__), "client_transport:{}".format(transport), ] self._reset_telemetry() self._telemetry_flush_interval = telemetry_min_flush_interval self._telemetry = not disable_telemetry self._buffer_lock = RLock()
def initialize( api_key=None, # type: Optional[str] app_key=None, # type: Optional[str] host_name=None, # type: Optional[str] api_host=None, # type: Optional[str] statsd_host=None, # type: Optional[str] statsd_port=None, # type: Optional[int] statsd_use_default_route=False, # type: bool statsd_socket_path=None, # type: Optional[str] statsd_namespace=None, # type: Optional[str] statsd_constant_tags=None, # type: Optional[List[str]] return_raw_response=False, # type: bool hostname_from_config=True, # type: bool **kwargs # type: Any ): # type: (...) -> None """ Initialize and configure Datadog.api and Datadog.statsd modules :param api_key: Datadog API key :type api_key: string :param app_key: Datadog application key :type app_key: string :param host_name: Set a specific hostname :type host_name: string :param proxies: Proxy to use to connect to Datadog API; for example, 'proxies': {'http': "http:<user>:<pass>@<ip>:<port>/"} :type proxies: dictionary mapping protocol to the URL of the proxy. :param api_host: Datadog API endpoint :type api_host: url :param statsd_host: Host of DogStatsd server or statsd daemon :type statsd_host: address :param statsd_port: Port of DogStatsd server or statsd daemon :type statsd_port: port :param statsd_use_default_route: Dynamically set the statsd host to the default route (Useful when running the client in a container) :type statsd_use_default_route: boolean :param statsd_socket_path: path to the DogStatsd UNIX socket. Supersedes statsd_host and stats_port if provided. :param statsd_constant_tags: A list of tags to be applied to all metrics ("tag", "tag:value") :type statsd_constant_tags: list of string :param cacert: Path to local certificate file used to verify SSL \ certificates. Can also be set to True (default) to use the systems \ certificate store, or False to skip SSL verification :type cacert: path or boolean :param mute: Mute any ApiError or ClientError before they escape \ from datadog.api.HTTPClient (default: True). :type mute: boolean :param return_raw_response: Whether or not to return the raw response object in addition \ to the decoded response content (default: False) :type return_raw_response: boolean :param hostname_from_config: Set the hostname from the Datadog agent config (agent 5). Will be deprecated :type hostname_from_config: boolean """ # API configuration api._api_key = api_key or api._api_key or os.environ.get( 'DATADOG_API_KEY', os.environ.get('DD_API_KEY')) api._application_key = (app_key or api._application_key or os.environ.get( 'DATADOG_APP_KEY', os.environ.get('DD_APP_KEY'))) api._hostname_from_config = hostname_from_config api._host_name = host_name or api._host_name or get_hostname( hostname_from_config) api._api_host = api_host or api._api_host or os.environ.get( 'DATADOG_HOST', 'https://api.datadoghq.com') # Statsd configuration # ...overrides the default `statsd` instance attributes if statsd_socket_path: statsd.socket_path = statsd_socket_path statsd.host = None statsd.port = None else: if statsd_host or statsd_use_default_route: statsd.host = statsd.resolve_host(statsd_host, statsd_use_default_route) if statsd_port: statsd.port = int(statsd_port) if statsd_namespace: statsd.namespace = text(statsd_namespace) if statsd_constant_tags: statsd.constant_tags += statsd_constant_tags api._return_raw_response = return_raw_response # HTTP client and API options for key, value in iteritems(kwargs): attribute = "_{}".format(key) setattr(api, attribute, value)
def __init__( self, host=DEFAULT_HOST, port=DEFAULT_PORT, max_buffer_size=50, namespace=None, constant_tags=None, use_ms=False, use_default_route=False, socket_path=None, default_sample_rate=1, disable_telemetry=False, telemetry_min_flush_interval=DEFAULT_TELEMETRY_MIN_FLUSH_INTERVAL, timer_metric="timing"): """ Initialize a DogStatsd object. >>> statsd = DogStatsd() :envvar DD_AGENT_HOST: the host of the DogStatsd server. If set, it overrides default value. :type DD_AGENT_HOST: string :envvar DD_DOGSTATSD_PORT: the port of the DogStatsd server. If set, it overrides default value. :type DD_DOGSTATSD_PORT: integer :envvar DATADOG_TAGS: Tags to attach to every metric reported by dogstatsd client. :type DATADOG_TAGS: comma-delimited string :envvar DD_ENTITY_ID: Tag to identify the client entity. :type DD_ENTITY_ID: string :envvar DD_ENV: the env of the service running the dogstatsd client. If set, it is appended to the constant (global) tags of the statsd client. :type DD_ENV: string :envvar DD_SERVICE: the name of the service running the dogstatsd client. If set, it is appended to the constant (global) tags of the statsd client. :type DD_SERVICE: string :envvar DD_VERSION: the version of the service running the dogstatsd client. If set, it is appended to the constant (global) tags of the statsd client. :type DD_VERSION: string :param host: the host of the DogStatsd server. :type host: string :param port: the port of the DogStatsd server. :type port: integer :param max_buffer_size: Maximum number of metrics to buffer before sending to the server if sending metrics in batch :type max_buffer_size: integer :param namespace: Namespace to prefix all metric names :type namespace: string :param constant_tags: Tags to attach to all metrics :type constant_tags: list of strings :param use_ms: Report timed values in milliseconds instead of seconds (default False) :type use_ms: boolean :param use_default_route: Dynamically set the DogStatsd host to the default route (Useful when running the client in a container) (Linux only) :type use_default_route: boolean :param socket_path: Communicate with dogstatsd through a UNIX socket instead of UDP. If set, disables UDP transmission (Linux only) :type socket_path: string :param default_sample_rate: Sample rate to use by default for all metrics :type default_sample_rate: float """ self.lock = Lock() # Check host and port env vars agent_host = os.environ.get('DD_AGENT_HOST') if agent_host and host == DEFAULT_HOST: host = agent_host dogstatsd_port = os.environ.get('DD_DOGSTATSD_PORT') if dogstatsd_port and port == DEFAULT_PORT: try: port = int(dogstatsd_port) except ValueError: log.warning( "Port number provided in DD_DOGSTATSD_PORT env var is not an integer: \ %s, using %s as port number", dogstatsd_port, port) # Connection if socket_path is not None: self.socket_path = socket_path self.host = None self.port = None transport = "uds" else: self.socket_path = None self.host = self.resolve_host(host, use_default_route) self.port = int(port) transport = "udp" # Socket self.socket = None self.max_buffer_size = max_buffer_size self._send = self._send_to_server self.encoding = 'utf-8' # Options env_tags = [ tag for tag in os.environ.get('DATADOG_TAGS', '').split(',') if tag ] # Inject values of DD_* environment variables as global tags. for var, tag_name in DD_ENV_TAGS_MAPPING.items(): value = os.environ.get(var, '') if value: env_tags.append('{name}:{value}'.format(name=tag_name, value=value)) if constant_tags is None: constant_tags = [] self.constant_tags = constant_tags + env_tags if namespace is not None: namespace = text(namespace) self.namespace = namespace self.use_ms = use_ms self.default_sample_rate = default_sample_rate self.timer_metric_function = self._choose_timer_metric_function( timer_metric) # init telemetry version try: client_version = get_pkg_version() except Exception: client_version = u"unknown" self._client_tags = [ "client:py", "client_version:{}".format(client_version), "client_transport:{}".format(transport), ] self._reset_telementry() self._telemetry_flush_interval = telemetry_min_flush_interval self._telemetry = not disable_telemetry