Ejemplo n.º 1
0
def initialize(api_key=None, app_key=None, host_name=None, api_host="https://app.datadoghq.com",
               proxies=None, statsd_host=None, statsd_port=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 proxies: Proxy to use to connect to Datadog API
    :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
    """
    # Configure api
    api._api_key = api_key
    api._application_key = app_key
    api._host_name = host_name if host_name is not None else get_hostname()
    api._api_host = api_host
    api._proxies = proxies

    # Given statsd_host and statsd_port, overrides statsd instance
    if statsd_host and statsd_port:
        statsd.host = statsd_host
        statsd.port = int(statsd_port)
    def __init__(self, api_key, app_key, flush_interval=10, namespace="aplt"):

        datadog.initialize(api_key=api_key, app_key=app_key)
        self._client = ThreadStats()
        self._flush_interval = flush_interval
        self._host = get_hostname()
        self._namespace = namespace
Ejemplo n.º 3
0
 def test_timing(self, mock_timing):
     self.backend.timing("foo", 30, instance="bar")
     mock_timing.assert_called_once_with("sentrytest.foo",
                                         30,
                                         sample_rate=1,
                                         tags=["instance:bar"],
                                         host=get_hostname())
Ejemplo n.º 4
0
 def test_gauge(self, mock_gauge):
     self.backend.gauge("foo", 5, instance="bar")
     mock_gauge.assert_called_once_with("sentrytest.foo",
                                        5,
                                        sample_rate=1,
                                        tags=["instance:bar"],
                                        host=get_hostname())
Ejemplo n.º 5
0
 def test_incr(self, mock_incr):
     self.backend.incr("foo", instance="bar")
     mock_incr.assert_called_once_with("sentrytest.foo",
                                       1,
                                       sample_rate=1,
                                       tags=["instance:bar"],
                                       host=get_hostname())
Ejemplo n.º 6
0
 def test_incr(self, mock_incr):
     self.backend.incr('foo', instance='bar')
     mock_incr.assert_called_once_with(
         'sentrytest.foo', 1,
         tags=['instance:bar'],
         host=get_hostname(),
     )
Ejemplo n.º 7
0
 def test_timing(self, mock_timing):
     self.backend.timing('foo', 30, instance='bar')
     mock_timing.assert_called_once_with(
         'sentrytest.foo', 30,
         sample_rate=1,
         tags=['instance:bar'],
         host=get_hostname(),
     )
Ejemplo n.º 8
0
    def __init__(self, api_key, app_key, flush_interval=10,
                 namespace="aplt"):

        datadog.initialize(api_key=api_key, app_key=app_key)
        self._client = ThreadStats()
        self._flush_interval = flush_interval
        self._host = get_hostname()
        self._namespace = namespace
Ejemplo n.º 9
0
 def test_timing(self, mock_timing):
     self.backend.timing('foo', 30, instance='bar')
     mock_timing.assert_called_once_with(
         'sentrytest.foo', 30,
         sample_rate=1,
         tags=['instance:bar'],
         host=get_hostname(),
     )
Ejemplo n.º 10
0
 def test_incr(self, mock_incr):
     self.backend.incr('foo', instance='bar')
     mock_incr.assert_called_once_with(
         'sentrytest.foo',
         1,
         tags=['instance:bar'],
         host=get_hostname(),
     )
Ejemplo n.º 11
0
 def __init__(self, prefix=None, **kwargs):
     self.tags = kwargs.pop('tags', None)
     if 'host' in kwargs:
         self.host = kwargs.pop('host')
     else:
         self.host = get_hostname()
     initialize(**kwargs)
     super(DatadogMetricsBackend, self).__init__(prefix=prefix)
Ejemplo n.º 12
0
 def __init__(self, prefix=None, **kwargs):
     # TODO(dcramer): it'd be nice if the initialize call wasn't a global
     self.tags = kwargs.pop("tags", None)
     if "host" in kwargs:
         self.host = kwargs.pop("host")
     else:
         self.host = get_hostname()
     initialize(**kwargs)
     super(DatadogMetricsBackend, self).__init__(prefix=prefix)
Ejemplo n.º 13
0
 def __init__(self, prefix=None, **kwargs):
     # TODO(dcramer): it'd be nice if the initialize call wasn't a global
     self.tags = kwargs.pop('tags', None)
     if 'host' in kwargs:
         self.host = kwargs.pop('host')
     else:
         self.host = get_hostname()
     initialize(**kwargs)
     super(DatadogMetricsBackend, self).__init__(prefix=prefix)
Ejemplo n.º 14
0
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, **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
    :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 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 if api_key is not None else os.environ.get('DATADOG_API_KEY')
    api._application_key = app_key if app_key is not None else os.environ.get('DATADOG_APP_KEY')
    api._host_name = host_name if host_name is not None else get_hostname()
    api._api_host = api_host if api_host is not None else \
        os.environ.get('DATADOG_HOST', 'https://app.datadoghq.com')

    # Statsd configuration -overrides default statsd instance attributes-
    if statsd_host:
        statsd.host = statsd_host

    if statsd_port:
        statsd.port = int(statsd_port)

    if statsd_use_default_route:
        statsd.use_default_route = statsd_use_default_route

    # HTTP client and API options
    for key, value in iteritems(kwargs):
        attribute = "_{0}".format(key)
        setattr(api, attribute, value)
Ejemplo n.º 15
0
def initialize(api_key=None,
               app_key=None,
               host_name=None,
               api_host=None,
               statsd_host=None,
               statsd_port=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
    :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 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 exceptions before they escape from the library (default: True).
    :type mute: boolean
    """
    # API configuration
    api._api_key = api_key if api_key is not None else os.environ.get(
        'DATADOG_API_KEY')
    api._application_key = app_key if app_key is not None else os.environ.get(
        'DATADOG_APP_KEY')
    api._host_name = host_name if host_name is not None else get_hostname()
    api._api_host = api_host if api_host is not None else \
        os.environ.get('DATADOG_HOST', 'https://app.datadoghq.com')

    # Statsd configuration -overrides default statsd instance attributes-
    if statsd_host and statsd_port:
        statsd.host = statsd_host
        statsd.port = int(statsd_port)

    # HTTP client and API options
    for key, value in iteritems(kwargs):
        attribute = "_{0}".format(key)
        setattr(api, attribute, value)
Ejemplo n.º 16
0
    def test_basic(self, mock_dog):
        hostname = get_hostname()

        m = DatadogMetrics("someapikey", "someappkey", namespace="testpush")
        ok_(len(mock_dog.mock_calls) > 0)
        m._client = Mock()
        m.start()
        m._client.start.assert_called_with(flush_interval=10,
                                           roll_up_interval=10)
        m.increment("test", 5)
        m._client.increment.assert_called_with("testpush.test", 5,
                                               host=hostname)
        m.timing("lifespan", 113)
        m._client.timing.assert_called_with("testpush.lifespan", value=113,
                                            host=hostname)
Ejemplo n.º 17
0
def initialize(api_key=None,
               app_key=None,
               host_name=None,
               api_host=None,
               proxies=None,
               statsd_host=None,
               statsd_port=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 proxies: Proxy to use to connect to Datadog API
    :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
    """
    # Configure api
    api._api_key = api_key if api_key is not None else os.environ.get(
        'DATADOG_API_KEY')
    api._application_key = app_key if app_key is not None else os.environ.get(
        'DATADOG_APP_KEY')
    api._host_name = host_name if host_name is not None else get_hostname()
    api._api_host = api_host if api_host is not None else \
        os.environ.get('DATADOG_HOST', 'https://app.datadoghq.com')
    api._proxies = proxies

    # Given statsd_host and statsd_port, overrides statsd instance
    if statsd_host and statsd_port:
        statsd.host = statsd_host
        statsd.port = int(statsd_port)
Ejemplo n.º 18
0
def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
               proxies=None, statsd_host=None, statsd_port=None, cacert=True):
    """
    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
    :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 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
    """
    # Configure api
    api._api_key = api_key if api_key is not None else os.environ.get('DATADOG_API_KEY')
    api._application_key = app_key if app_key is not None else os.environ.get('DATADOG_APP_KEY')
    api._host_name = host_name if host_name is not None else get_hostname()
    api._api_host = api_host if api_host is not None else \
        os.environ.get('DATADOG_HOST', 'https://app.datadoghq.com')
    api._proxies = proxies
    api._cacert = cacert

    # Given statsd_host and statsd_port, overrides statsd instance
    if statsd_host and statsd_port:
        statsd.host = statsd_host
        statsd.port = int(statsd_port)
Ejemplo n.º 19
0
def initialize(
    env_name: str,
    host: str,
    namespace: str,
    port: int = 8125,
    host_name: str = None,
    use_default_route: bool = False,
    constant_tags: Iterable[str] = None,
) -> None:
    """
    Initialize a statsd client.

    NOTE: `host` is the statsd host and `hostname` is the hostname to report as a tag.
    """
    LOG.debug('Initializing Gooee StatsD lib with host={host} port={port}')
    # Monkeypatch the datadog statsd lib so that the "defaults" are auto supplied.
    api._host_name = host_name if host_name is not None else get_hostname()
    api._api_host = host
    statsd.host = statsd.resolve_host(host, use_default_route)
    statsd.port = port
    statsd.namespace = namespace
    statsd.constant_tags = [f'env:{env_name}', f'app_name:{namespace}'
                            ] + (constant_tags or [])
Ejemplo n.º 20
0
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)
Ejemplo n.º 21
0
 def test_hostname_warning_not_present(self):
     try:
         get_hostname(hostname_from_config=False)
     except CfgNotFound:
         pytest.fail("Unexpected CfgNotFound Exception")
Ejemplo n.º 22
0
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)
Ejemplo n.º 23
0
 def __init__(self, prefix=None, host=None, tags=None, **kwargs):
     self.tags = tags
     self.host = host or get_hostname()
     initialize(**kwargs)
     super().__init__(prefix=prefix)