Example #1
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)
Example #2
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)
Example #3
0
    def request_called_with(self, method, url, data=None, params=None):
        (req_method, req_url), others = self.request_mock.request.call_args
        self.assertEquals(method, req_method, req_method)
        self.assertEquals(url, req_url, req_url)

        if data:
            self.assertIn('data', others)
            self.assertEquals(json.dumps(data), others['data'], others['data'])

        if params:
            self.assertIn('params', others)
            for (k, v) in iteritems(params):
                self.assertIn(k, others['params'], others['params'])
                self.assertEquals(v, others['params'][k])
Example #4
0
    def request_called_with(self, method, url, data=None, params=None):
        (req_method, req_url), others = self.request_mock.request.call_args
        self.assertEquals(method, req_method, req_method)
        self.assertEquals(url, req_url, req_url)

        if data:
            self.assertIn('data', others)
            self.assertEquals(json.dumps(data), others['data'], others['data'])

        if params:
            self.assertIn('params', others)
            for (k, v) in iteritems(params):
                self.assertIn(k, others['params'], others['params'])
                self.assertEquals(v, others['params'][k])
Example #5
0
    def request_called_with(self, method, url, data=None, params=None):
        (req_method, req_url), others = self.request_mock.request.call_args
        assert method == req_method, req_method
        assert url == req_url, req_url

        if data:
            assert 'data' in others
            assert json.dumps(data) == others['data'], others['data']

        if params:
            assert 'params' in others
            for (k, v) in iteritems(params):
                assert k in others['params'], others['params']
                assert v == others['params'][k]
Example #6
0
    def request_called_with(self, method, url, data=None, params=None):
        (req_method, req_url), others = self.request_mock.request.call_args
        assert method == req_method, req_method
        assert url == req_url, req_url

        if data:
            assert 'data' in others
            assert json.dumps(data) == others['data'], others['data']

        if params:
            assert 'params' in others
            for (k, v) in iteritems(params):
                assert k in others['params'], others['params']
                assert v == others['params'][k]
Example #7
0
    def request_called_with(self, method, url, data=None, params=None):
        (req_method, req_url), others = self.request_mock.call_args()
        self.assertEqual(method, req_method, req_method)
        self.assertEqual(url, req_url, req_url)

        if data:
            self.assertIn('data', others)
            others_data = json.loads(others['data'])
            self.assertEqual(data, others_data, others['data'])

        if params:
            self.assertIn('params', others)
            for (k, v) in iteritems(params):
                self.assertIn(k, others['params'], others['params'])
                self.assertEqual(v, others['params'][k])
Example #8
0
    def get_tags(agentConfig):
        if not agentConfig['collect_instance_metadata']:
            log.info(
                "Instance metadata collection is disabled. Not collecting it.")
            return []

        socket_to = None
        try:
            socket_to = socket.getdefaulttimeout()
            socket.setdefaulttimeout(EC2.TIMEOUT)
        except Exception:
            pass

        try:
            iam_role = url_lib.urlopen(
                EC2.URL + "/iam/security-credentials").read().strip()
            iam_params = json.loads(
                url_lib.urlopen(EC2.URL + "/iam/security-credentials" + "/" +
                                str(iam_role)).read().strip())
            from boto.ec2.connection import EC2Connection
            connection = EC2Connection(
                aws_access_key_id=iam_params['AccessKeyId'],
                aws_secret_access_key=iam_params['SecretAccessKey'],
                security_token=iam_params['Token'])
            instance_object = connection.get_only_instances(
                [EC2.metadata['instance-id']])[0]

            EC2_tags = [
                u"%s:%s" % (tag_key, tag_value)
                for tag_key, tag_value in iteritems(instance_object.tags)
            ]

        except Exception:
            log.exception("Problem retrieving custom EC2 tags")
            EC2_tags = []

        try:
            if socket_to is None:
                socket_to = 3
            socket.setdefaulttimeout(socket_to)
        except Exception:
            pass

        return EC2_tags
Example #9
0
    def query(cls, **params):
        """
        Get the events that occurred between the *start* and *end* POSIX timestamps,
        optional filtered by *priority* ("low" or "normal"), *sources* and
        *tags*.

        See the `event API documentation <http://api.datadoghq.com/events>`_ for the
        event data format.

        :return: JSON response from HTTP request

        >>> api.Event.query(start=1313769783, end=1419436870, priority="normal", \
            tags=["application:web"])
        """
        def timestamp_to_integer(k, v):
            if k in cls._timestamp_keys:
                return int(v)
            else:
                return v

        params = dict((k, timestamp_to_integer(k, v)) for k, v in iteritems(params))

        return super(Event, cls)._search(**params)
Example #10
0
    def query(cls, **params):
        """
        Get the events that occurred between the *start* and *end* POSIX timestamps,
        optional filtered by *priority* ("low" or "normal"), *sources* and
        *tags*.

        See the `event API documentation <http://api.datadoghq.com/events>`_ for the
        event data format.

        :return: JSON response from HTTP request

        >>> api.Event.query(start=1313769783, end=1419436870, priority="normal", \
            tags=["application:web"])
        """
        def timestamp_to_integer(k, v):
            if k in cls._timestamp_keys:
                return int(v)
            else:
                return v

        params = dict((k, timestamp_to_integer(k, v)) for k, v in iteritems(params))

        return super(Event, cls)._search(**params)
Example #11
0
    def get_tags(agentConfig):
        if not agentConfig['collect_instance_metadata']:
            log.info("Instance metadata collection is disabled. Not collecting it.")
            return []

        socket_to = None
        try:
            socket_to = socket.getdefaulttimeout()
            socket.setdefaulttimeout(EC2.TIMEOUT)
        except Exception:
            pass

        try:
            iam_role = url_lib.urlopen(EC2.URL + "/iam/security-credentials").read().strip()
            iam_params = json.loads(url_lib.urlopen(EC2.URL + "/iam/security-credentials" + "/" +
                                    str(iam_role)).read().strip())
            from boto.ec2.connection import EC2Connection
            connection = EC2Connection(aws_access_key_id=iam_params['AccessKeyId'],
                                       aws_secret_access_key=iam_params['SecretAccessKey'],
                                       security_token=iam_params['Token'])
            instance_object = connection.get_only_instances([EC2.metadata['instance-id']])[0]

            EC2_tags = [u"%s:%s" % (tag_key, tag_value) for tag_key, tag_value
                        in iteritems(instance_object.tags)]

        except Exception:
            log.exception("Problem retrieving custom EC2 tags")
            EC2_tags = []

        try:
            if socket_to is None:
                socket_to = 3
            socket.setdefaulttimeout(socket_to)
        except Exception:
            pass

        return EC2_tags
Example #12
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)
Example #13
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)
Example #14
0
 def add_event(self, **event):
     # Clean empty values
     event = dict((k, v) for k, v in iteritems(event) if v is not None)
     self._events.append(event)