Esempio n. 1
0
    def event(self, event):
        # type: (Event) -> None
        """Send an event.

        An event is a dictionary with the following keys and data types:

        .. code:: python

            {
                "timestamp": int,        # the epoch timestamp for the event
                "event_type": str,       # the event name
                "api_key": str,          # the api key for your account
                "msg_title": str,        # the title of the event
                "msg_text": str,         # the text body of the event
                "aggregation_key": str,  # a key to use for aggregating events
                "alert_type": str,       # (optional) one of ('error', 'warning', 'success', 'info'), defaults to 'info'
                "source_type_name": str, # (optional) the source type name
                "host": str,             # (optional) the name of the host
                "tags": list,            # (optional) a list of tags to associate with this event
                "priority": str,         # (optional) specifies the priority of the event ("normal" or "low")
            }

        :param ev event: the event to be sent.
        """
        # Enforce types of some fields, considerably facilitates handling in go bindings downstream
        for key, value in iteritems(event):
            if not isinstance(value, (text_type, binary_type)):
                continue

            try:
                event[key] = to_native_string(value)  # type: ignore
                # ^ Mypy complains about dynamic key assignment -- arguably for good reason.
                # Ideally we should convert this to a dict literal so that submitted events only include known keys.
            except UnicodeError:
                self.log.warning(
                    'Encoding error with field `%s`, cannot submit event', key)
                return

        if event.get('tags'):
            event['tags'] = self._normalize_tags_type(event['tags'])
        if event.get('timestamp'):
            event['timestamp'] = int(event['timestamp'])
        if event.get('aggregation_key'):
            event['aggregation_key'] = to_native_string(
                event['aggregation_key'])

        if self.__NAMESPACE__:
            event.setdefault('source_type_name', self.__NAMESPACE__)

        aggregator.submit_event(self, self.check_id, event)
Esempio n. 2
0
    def event(self, event):
        """Send an event.

        An event is a dictionary with the following keys and data types:

        .. code:: python

            {
                "timestamp": int,        # the epoch timestamp for the event
                "event_type": str,       # the event name
                "api_key": str,          # the api key for your account
                "msg_title": str,        # the title of the event
                "msg_text": str,         # the text body of the event
                "aggregation_key": str,  # a key to use for aggregating events
                "alert_type": str,       # (optional) one of ('error', 'warning', 'success', 'info'), defaults to 'info'
                "source_type_name": str, # (optional) the source type name
                "host": str,             # (optional) the name of the host
                "tags": list,            # (optional) a list of tags to associate with this event
                "priority": str,         # (optional) specifies the priority of the event ("normal" or "low")
            }

        :param ev event: the event to be sent.
        """
        # Enforce types of some fields, considerably facilitates handling in go bindings downstream
        for key, value in list(iteritems(event)):
            # transform any bytes objects to utf-8
            if isinstance(value, bytes):
                try:
                    event[key] = event[key].decode('utf-8')
                except UnicodeError:
                    self.log.warning(
                        'Error decoding unicode field `{}` to utf-8 encoded string, cannot submit event'
                        .format(key))
                    return

        if event.get('tags'):
            event['tags'] = self._normalize_tags_type(event['tags'])
        if event.get('timestamp'):
            event['timestamp'] = int(event['timestamp'])
        if event.get('aggregation_key'):
            event['aggregation_key'] = ensure_unicode(event['aggregation_key'])

        if self.__NAMESPACE__:
            event.setdefault('source_type_name', self.__NAMESPACE__)

        aggregator.submit_event(self, self.check_id, event)
Esempio n. 3
0
 def event(self, event):
     # Enforce types of some fields, considerably facilitates handling in go bindings downstream
     for key, value in event.items():
         # transform the unicode objects to plain strings with utf-8 encoding
         if isinstance(value, unicode):
             try:
                 event[key] = event[key].encode('utf-8')
             except UnicodeError:
                 self.log.warning("Error encoding unicode field '%s' of event to utf-8 encoded string, can't submit event", key)
                 return
     if event.get('tags'):
         event['tags'] = self._normalize_tags_type(event['tags'])
     if event.get('timestamp'):
         event['timestamp'] = int(event['timestamp'])
     if event.get('aggregation_key'):
         event['aggregation_key'] = str(event['aggregation_key'])
     aggregator.submit_event(self, self.check_id, event)
Esempio n. 4
0
 def event(self, event):
     # Enforce types of some fields, considerably facilitates handling in go bindings downstream
     for key, value in event.items():
         # transform the unicode objects to plain strings with utf-8 encoding
         if isinstance(value, unicode):
             try:
                 event[key] = event[key].encode('utf-8')
             except UnicodeError:
                 self.log.warning("Error encoding unicode field '%s' of event to utf-8 encoded string, can't submit event", key)
                 return
     if event.get('tags'):
         event['tags'] = self._normalize_tags_type(event['tags'])
     if event.get('timestamp'):
         event['timestamp'] = int(event['timestamp'])
     if event.get('aggregation_key'):
         event['aggregation_key'] = str(event['aggregation_key'])
     aggregator.submit_event(self, self.check_id, event)
Esempio n. 5
0
 def event(self, event):
     # Enforce types of some fields, considerably facilitates handling in go bindings downstream
     for key, value in list(iteritems(event)):
         # transform any bytes objects to utf-8
         if isinstance(value, bytes):
             try:
                 event[key] = event[key].decode('utf-8')
             except UnicodeError:
                 self.log.warning(
                     'Error decoding unicode field `{}` to utf-8 encoded string, cannot submit event'.format(key)
                 )
                 return
     if event.get('tags'):
         event['tags'] = self._normalize_tags_type(event['tags'])
     if event.get('timestamp'):
         event['timestamp'] = int(event['timestamp'])
     if event.get('aggregation_key'):
         event['aggregation_key'] = ensure_unicode(event['aggregation_key'])
     aggregator.submit_event(self, self.check_id, event)
Esempio n. 6
0
    def event(self, event):
        # Enforce types of some fields, considerably facilitates handling in go bindings downstream
        for key, value in list(iteritems(event)):
            # transform the unicode objects to plain strings with utf-8 encoding
            if isinstance(value, text_type):
                try:
                    event[key] = event[key].encode('utf-8')
                except UnicodeError:
                    self.log.warning(
                        "Error encoding unicode field '%s' to utf-8 encoded string, can't submit event",
                        key)
                    return

        if event.get('tags'):
            event['tags'] = self._normalize_tags_type(event['tags'])
        if event.get('timestamp'):
            event['timestamp'] = int(event['timestamp'])
        if event.get('aggregation_key'):
            event['aggregation_key'] = ensure_bytes(event['aggregation_key'])

        if self.__NAMESPACE__:
            event.setdefault('source_type_name', self.__NAMESPACE__)

        aggregator.submit_event(self, self.check_id, event)