def PercentageDistribution(
    name, num_buckets=1000, reset_after=False,
    description=None, field_spec=_MISSING):
  """Returns a metric handle for a cumulative distribution for percentage.

  The distribution handle returned by this method is better suited for reporting
  percentage values than the default one. The bucketing is optimized for values
  in [0,100].

  Args:
    name: The name of this metric.
    num_buckets: This metric buckets the percentage values before
        reporting. This argument controls the number of the bucket the range
        [0,100] is divided in. The default gives you 0.1% resolution.
    reset_after: Should the metric be reset after reporting.
    description: A string description of the metric.
    field_spec: A sequence of ts_mon.Field objects to specify the field schema.
  """
  # The last bucket actually covers [100, 100 + 1.0/num_buckets), so it
  # corresponds to values that exactly match 100%.
  bucket_width = 100 / num_buckets
  b = ts_mon.FixedWidthBucketer(bucket_width, num_buckets)
  return ts_mon.CumulativeDistributionMetric(
      name, bucketer=b,
      description=description, field_spec=field_spec)
def _BatchAndSendSpans(project_id, client, batch_sequence):
    """Batches and sends spans to the cloud trace API.

  Args:
    project_id: The Google Cloud project id
    client: The google python api client
    batch_sequence: An iterable of Span batches represented as JSON objects.
  """
    batch_size_metric = metrics.CumulativeDistribution(
        _BATCH_SIZE_METRIC,
        description="The size of batches emitted by export_to_cloud_trace",
        bucketer=ts_mon.FixedWidthBucketer(1, MIN_BATCH_SIZE * 2),
        field_spec=None)

    for batch in _ImpatientlyRebatched(batch_sequence, MIN_BATCH_SIZE,
                                       BATCH_PATIENCE):
        batch_size_metric.add(len(batch))

        traces = []
        groups = _GroupBy(batch, key=lambda span: span.get('traceId'))
        for trace_id, spans in groups:
            traces.append({
                'traceId': trace_id,
                'projectId': project_id,
                'spans': spans
            })

        if traces:
            client.projects().patchTraces(projectId=project_id,
                                          body={'traces': traces})
Example #3
0
def CumulativeSmallIntegerDistribution(name, reset_after=False):
    """Returns a metric handle for a cumulative distribution named |name|.

  This differs slightly from CumulativeDistribution, in that the underlying
  metric uses a uniform bucketer rather than a geometric one.

  This metric type is suitable for holding a distribution of numbers that are
  nonnegative integers in the range of 0 to 100.
  """
    return ts_mon.CumulativeDistributionMetric(
        name, bucketer=ts_mon.FixedWidthBucketer(1))
    ts_mon.StringField('builder'),
    ts_mon.StringField('master'),
    ts_mon.StringField('project_id'),
    ts_mon.StringField('result'),
    ts_mon.StringField('slave'),
    ts_mon.StringField('step_name'),
    ts_mon.StringField('subproject_tag'),
]

step_durations = ts_mon.CumulativeDistributionMetric(
    'buildbot/master/builders/steps/durations',
    'Time (in seconds) from step start to step end',
    step_field_spec,
    units=ts_mon.MetricsDataUnits.SECONDS,
    # Use fixed-width bucketer up to 2.7 hours with 10-second precision.
    bucketer=ts_mon.FixedWidthBucketer(10, 1000))

step_counts = ts_mon.CounterMetric(
    'buildbot/master/builders/steps/count',
    'Count of step results, per builder and step',
    step_field_spec)

field_spec = [
    ts_mon.StringField('builder'),
    ts_mon.StringField('master'),
    ts_mon.StringField('project_id'),
    ts_mon.StringField('result'),
    ts_mon.StringField('slave'),
    ts_mon.StringField('subproject_id'),
]