Exemplo n.º 1
0
 def test_record_against_implicit_tag_map(self):
     measure_to_view_map = mock.Mock()
     measurement_map = measurement_map_module.MeasurementMap(
         measure_to_view_map=measure_to_view_map)
     TagContext.set(TagMap(tags=[Tag('testtag1', 'testtag1val')]))
     measurement_map.record()
     self.assertTrue(measure_to_view_map.record.called)
    def record(self, tags=None):
        """records all the measures at the same time with a tag_map.
        tag_map could either be explicitly passed to the method, or implicitly
        read from current runtime context.
        """
        if tags is None:
            tags = TagContext.get()
        if self._invalid:
            logger.warning("Measurement map has included negative value "
                           "measurements, refusing to record")
            return
        for measure, value in self.measurement_map.items():
            if value < 0:
                self._invalid = True
                logger.warning("Dropping values, value to record must be "
                               "non-negative")
                logger.info("Measure '{}' has negative value ({}), refusing "
                            "to record measurements from {}".format(
                                measure.name, value, self))
                return

        self.measure_to_view_map.record(tags=tags,
                                        measurement_map=self.measurement_map,
                                        timestamp=utils.to_iso_str(),
                                        attachments=self.attachments)
Exemplo n.º 3
0
def set_context_tags(tags: Dict[str, Any]) -> ContextVar:
    tag_map = TagMap(TagContext.get())

    for key, value in tags.items():
        if value:
            tag_map.insert(key, str(value))
        else:
            tag_map.delete(key)

    return TagContext.contextvar.set(tag_map)
Exemplo n.º 4
0
def context_tags() -> TagMap:
    """Returns a copy of the context TagMap"""
    tag_map = TagContext.get()
    return TagMap(tag_map.map if tag_map else None)