Exemplo n.º 1
0
    def record(self, tag_map_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 execution context.
        """
        if tag_map_tags is None:
            tag_map_tags = execution_context.get_current_tag_map()
        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=tag_map_tags,
                measurement_map=self.measurement_map,
                timestamp=datetime.utcnow().isoformat() + 'Z',
                attachments=self.attachments
        )
Exemplo n.º 2
0
def propagate_context_into(data_dict):
  """Propagates Tag context into a dictionary."""
  if not _METRICS_ENABLED:
    return
  value = binary_serializer.BinarySerializer().to_byte_array(
      execution_context.get_current_tag_map())
  data_dict['OPENCENSUS_STATS_CONTEXT'] = base64.b64encode(value)
Exemplo n.º 3
0
 def record(self, tag_map_tags=execution_context.get_current_tag_map()):
     """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 execution context.
     """
     self.measure_to_view_map.record(
         tags=tag_map_tags,
         measurement_map=self.measurement_map,
         timestamp=datetime.utcnow().isoformat() + 'Z')
Exemplo n.º 4
0
    def test_set_and_get_tag_map(self):
        key = tag_key_module.TagKey('key')
        value = tag_value_module.TagValue('value')
        tag_map = tag_map_module.TagMap()
        tag_map.insert(key, value)

        execution_context.set_current_tag_map(tag_map)

        result = execution_context.get_current_tag_map()

        self.assertEqual(result, tag_map)
Exemplo n.º 5
0
def set_thread_local_tags(tags: Dict[str, Any]):
    tag_map = execution_context.get_current_tag_map()
    if not tag_map:
        tag_map = TagMap()
        execution_context.set_current_tag_map(tag_map)

    for key, value in tags.items():
        if value:
            tag_map.insert(key, str(value))
        else:
            tag_map.delete(key)
Exemplo n.º 6
0
def _measure(name):
  tmap = copy.deepcopy(execution_context.get_current_tag_map())
  tmap.insert(_key_stage, name)
  start = time.time()
  try:
    yield
  except:
    mmap = STATS.stats_recorder.new_measurement_map()
    mmap.measure_int_put(_m_success, 0)
    mmap.record(tmap)
    raise

  elapsed = (time.time() - start) * 1000
  mmap = STATS.stats_recorder.new_measurement_map()
  mmap.measure_float_put(_m_latency, elapsed)
  mmap.measure_int_put(_m_success, 1)
  mmap.record(tmap)
  _logger.info("%s took %sms", name, elapsed)
Exemplo n.º 7
0
    def test_unset_tag_map(self):
        result = execution_context.get_current_tag_map()

        self.assertIsNone(result)
Exemplo n.º 8
0
def thread_local_tags() -> Optional[TagMap]:
    """Returns a copy of the thread local TagMap"""
    tag_map = execution_context.get_current_tag_map()
    return TagMap(tag_map.map if tag_map else None)