Java Code Examples for io.opencensus.tags.TagContextBuilder#build()

The following examples show how to use io.opencensus.tags.TagContextBuilder#build() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TagContextSerializationTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializeTooLargeTagContext() throws TagContextSerializationException {
  TagContextBuilder builder = tagger.emptyBuilder();
  for (int i = 0; i < BinarySerializationUtils.TAGCONTEXT_SERIALIZED_SIZE_LIMIT / 8 - 1; i++) {
    // Each tag will be with format {key : "0123", value : "0123"}, so the length of it is 8.
    String str;
    if (i < 10) {
      str = "000" + i;
    } else if (i < 100) {
      str = "00" + i;
    } else if (i < 1000) {
      str = "0" + i;
    } else {
      str = String.valueOf(i);
    }
    builder.put(TagKey.create(str), TagValue.create(str));
  }
  // The last tag will be of size 9, so the total size of the TagContext (8193) will be one byte
  // more than limit.
  builder.put(TagKey.create("last"), TagValue.create("last1"));

  TagContext tagContext = builder.build();
  thrown.expect(TagContextSerializationException.class);
  thrown.expectMessage("Size of TagContext exceeds the maximum serialized size ");
  serializer.toByteArray(tagContext);
}
 
Example 2
Source File: TelemetryUtils.java    From meghanada-server with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("try")
public static void recordTaggedStat(
    TagKey[] keys, String[] values, Measure.MeasureDouble md, Double d) {
  TagContextBuilder builder = tagger.emptyBuilder();
  for (int i = 0; i < keys.length; i++) {
    builder.putLocal(keys[i], TagValue.create(values[i]));
  }
  TagContext tctx = builder.build();
  try (Scope ss = tagger.withTagContext(tctx)) {
    statsRecorder.newMeasureMap().put(md, d).record();
  }
}
 
Example 3
Source File: TelemetryUtils.java    From meghanada-server with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("try")
public static void recordTaggedStat(
    TagKey[] keys, String[] values, Measure.MeasureLong md, Long n) {
  TagContextBuilder builder = tagger.emptyBuilder();
  for (int i = 0; i < keys.length; i++) {
    builder.putLocal(keys[i], TagValue.create(values[i]));
  }
  TagContext tctx = builder.build();

  try (Scope ss = tagger.withTagContext(tctx)) {
    statsRecorder.newMeasureMap().put(md, n).record();
  }
}
 
Example 4
Source File: RecordMultipleViewsBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private TagContext createContext(int size) {
  TagContextBuilder builder = tagger.emptyBuilder();
  for (int i = 0; i < size; i++) {
    builder.put(
        TagsBenchmarksUtil.TAG_KEYS.get(i),
        TagsBenchmarksUtil.TAG_VALUES.get(0),
        TagsBenchmarksUtil.UNLIMITED_PROPAGATION);
  }
  return builder.build();
}
 
Example 5
Source File: TagsBenchmarksUtil.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Adds 'numTags' tags to 'tagsBuilder' and returns the associated tag context. */
@VisibleForTesting
public static TagContext createTagContext(TagContextBuilder tagsBuilder, int numTags) {
  for (int i = 0; i < numTags; i++) {
    tagsBuilder.put(TAG_KEYS.get(i), TAG_VALUES.get(i), UNLIMITED_PROPAGATION);
  }
  return tagsBuilder.build();
}
 
Example 6
Source File: OpenCensusPluginSink.java    From ffwd with Apache License 2.0 4 votes vote down vote up
public void sendMetric(Metric metric) {
  try {
    String metricName = getOutputMetricName(metric);
    MeasureLong measure = measures.get(metricName);

    if (measure == null) {
      if (measures.size() > maxViews) {
        throw new RuntimeException("maxViews exceeded. " +
            "Please increase in configuration or decrease number of metrics.");
      }

      measure = MeasureLong.create("Events", "Number of Events", "1");
      measures.put(metricName, measure);

      // Stackdriver expects each metric to have the same set of tags so metrics
      // missing tags will be rejected. NB by default stackdriver will create
      // the metricDescription based on the first metric received so be consistant
      // from the start.
      final List<TagKey> columns = new ArrayList<TagKey>(metric.getTags().size());
      metric.getTags().keySet().forEach(tagName -> {
          columns.add(TagKey.create(sanitizeName(tagName)));
      });
      final View view =
          View.create(
              Name.create(metricName),
              metricName,
              measure,
              Sum.create(),
              columns);

      Stats.getViewManager().registerView(view);
    }
    final TagContextBuilder builder = tagger.emptyBuilder();
    metric.getTags().forEach((k, v) -> {
        builder.putPropagating(TagKey.create(sanitizeName(k)), TagValue.create(v));
    });
    final TagContext context = builder.build();

    statsRecorder.newMeasureMap().put(measure, (long) metric.getValue()).record(context);
  } catch (Exception ex) {
    log.error("Couldn't send metric %s", ex);
    throw ex;
  }
}