Java Code Examples for com.netflix.spectator.api.Id#tags()

The following examples show how to use com.netflix.spectator.api.Id#tags() . 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: StackdriverWriter.java    From kork with Apache License 2.0 6 votes vote down vote up
/**
 * Generate an Id for the derived timer measurements.
 *
 * @param id The original Measurement id
 * @return A copy of the original but without the 'statistic' tag, and the name will be decorated
 *     with "__count" or "__totalTime" depending on the value of the original statistic tag.
 */
Id deriveBaseTimerId(Id id) {
  String suffix = null;
  ArrayList<Tag> tags = new ArrayList<Tag>();
  for (Tag tag : id.tags()) {
    if (tag.key().equals("statistic")) {
      if (tag.value().equals("totalTime")) {
        suffix = "totalTime";
      } else if (tag.value().equals("count")) {
        suffix = "count";
      } else {
        throw new IllegalStateException("Unexpected statistic=" + tag.value());
      }
      continue;
    }
    tags.add(tag);
  }
  if (suffix == null) {
    // Didnt have statistic, so return original.
    return id;
  }

  return ID_FACTORY.createId(id.name() + "__" + suffix).withTags(tags);
}
 
Example 2
Source File: TagMeasurementFilter.java    From spectator with Apache License 2.0 6 votes vote down vote up
/**
 * Implements MeasurementFilter interface.
 */
@SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation")
@Override public boolean test(Measurement measurement) {
  Id id = measurement.id();
  if (!stringMatches(id.name(), meterNamePattern)) {
      return false;
  }

  if (tagNamePattern != null || tagValuePattern != null) {
    for (Tag tag : id.tags()) {
      boolean nameOk = stringMatches(tag.key(), tagNamePattern);
      boolean valueOk = stringMatches(tag.value(), tagValuePattern);
      if (nameOk && valueOk) {
        return true;
      }
    }
    return false;
  }

  return true;
}
 
Example 3
Source File: ActionMetrics.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
public ActionMetrics(Id rootId, Registry registry) {
    this.root = rootId.name();
    this.commonTags = rootId.tags();
    this.registry = registry;
    this.successCounter = registry.counter(registry.createId(root, commonTags).withTag("status", "success"));

    this.latencyTimerOnSuccess = registry.timer(registry.createId(root + ".latency", commonTags).withTag("status", "success"));
    this.latencyTimerOnError = registry.timer(registry.createId(root + ".latency", commonTags).withTag("status", "failure"));

    this.sinceLastExecutionId = registry.createId(root + ".sinceLastCompletion", commonTags);
    this.lastCompletionRef = new AtomicLong(registry.clock().wallTime());
    PolledMeter.using(registry).withId(sinceLastExecutionId).monitorValue(lastCompletionRef);
}
 
Example 4
Source File: StackdriverWriterTest.java    From kork with Apache License 2.0 5 votes vote down vote up
TimeSeries makeTimeSeries(MetricDescriptor descriptor, Id id, double value, String time) {
  TypedValue tv = new TypedValue();
  tv.setDoubleValue(value);
  TimeInterval timeInterval = new TimeInterval();
  timeInterval.setStartTime("2016-08-28T14:20:00.000000000Z");
  timeInterval.setEndTime(time);

  Point point = new Point();
  point.setValue(tv);
  point.setInterval(timeInterval);

  HashMap<String, String> labels = new HashMap<String, String>();
  labels.put(MetricDescriptorCache.INSTANCE_LABEL, INSTANCE_ID);
  for (Tag tag : id.tags()) {
    labels.put(tag.key(), tag.value());
  }

  Metric metric = new Metric();
  metric.setType(descriptor.getType());
  metric.setLabels(labels);

  TimeSeries ts = new TimeSeries();
  ts.setResource(writer.peekMonitoredResource());
  ts.setMetric(metric);
  ts.setPoints(Arrays.asList(point));
  ts.setMetricKind("CUMULATIVE");
  ts.setValueType("DOUBLE");

  return ts;
}
 
Example 5
Source File: MetricsRegistry.java    From spectator with Apache License 2.0 5 votes vote down vote up
/** Create a new instance. */
public MetricsRegistry(Clock clock, com.codahale.metrics.MetricRegistry impl) {
  this(clock, impl, id -> {
    Id normalized = Utils.normalize(id);
    StringBuilder buf = new StringBuilder();
    buf.append(normalized.name());
    for (Tag t : normalized.tags()) {
      buf.append('.').append(t.key()).append('-').append(t.value());
    }
    return buf.toString();
  });
}
 
Example 6
Source File: JsonUtils.java    From spectator with Apache License 2.0 5 votes vote down vote up
private static Map<String, Integer> buildStringTable(
    JsonGenerator gen,
    Map<String, String> commonTags,
    List<Measurement> measurements) throws IOException {
  Map<String, Integer> strings = new HashMap<>();

  strings.put("name", 0);
  commonTags.forEach((k, v) -> {
    strings.put(k, 0);
    strings.put(v, 0);
  });

  for (Measurement m : measurements) {
    Id id = m.id();
    strings.put(id.name(), 0);
    for (Tag t : id.tags()) {
      strings.put(t.key(), 0);
      strings.put(t.value(), 0);
    }
  }

  String[] sorted = strings.keySet().toArray(new String[0]);
  Arrays.sort(sorted);

  gen.writeNumber(sorted.length);
  for (int i = 0; i < sorted.length; ++i) {
    gen.writeString(sorted[i]);
    strings.put(sorted[i], i);
  }

  return strings;
}
 
Example 7
Source File: JsonUtils.java    From spectator with Apache License 2.0 5 votes vote down vote up
private static void appendMeasurement(
    JsonGenerator gen,
    Map<String, Integer> strings,
    Map<String, String> commonTags,
    Id id,
    double value) throws IOException {

  int op = operation(id);
  if (shouldSend(op, value)) {
    // Number of tag entries, commonTags + name + tags
    int n = commonTags.size() + 1 + Utils.size(id.tags());
    gen.writeNumber(n);

    // Write out the key/value pairs for the tags
    for (Map.Entry<String, String> entry : commonTags.entrySet()) {
      gen.writeNumber(strings.get(entry.getKey()));
      gen.writeNumber(strings.get(entry.getValue()));
    }
    for (Tag t : id.tags()) {
      gen.writeNumber(strings.get(t.key()));
      gen.writeNumber(strings.get(t.value()));
    }
    gen.writeNumber(strings.get("name"));
    gen.writeNumber(strings.get(id.name()));

    // Write out the operation and delta value
    gen.writeNumber(op);
    gen.writeNumber(value);
  }
}
 
Example 8
Source File: JsonUtils.java    From spectator with Apache License 2.0 5 votes vote down vote up
private static int operation(Id id) {
  for (Tag t : id.tags()) {
    if ("statistic".equals(t.key())) {
      return operation(t.value());
    }
  }
  return MAX;
}
 
Example 9
Source File: MicrometerRegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void createIdNameAndTags() {
  Id id = registry.createId("foo", "a", "1", "b", "2");
  Assertions.assertEquals("foo", id.name());

  Map<String, String> tags = new HashMap<>();
  tags.put("a", "1");
  tags.put("b", "2");

  for (Tag t : id.tags()) {
    Assertions.assertEquals(tags.remove(t.key()), t.value());
  }
  Assertions.assertTrue(tags.isEmpty());
}
 
Example 10
Source File: DoubleDistributionSummaryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private String get(Id id, String key) {
  for (Tag t : id.tags()) {
    if (key.equals(t.key())) {
      return t.value();
    }
  }
  return null;
}
 
Example 11
Source File: AtlasRegistry.java    From spectator with Apache License 2.0 5 votes vote down vote up
private Map<String, String> toMap(Id id) {
  Map<String, String> tags = new HashMap<>();

  for (Tag t : id.tags()) {
    String k = charset.replaceNonMembers(t.key(), '_');
    String v = charset.replaceNonMembers(t.value(), '_');
    tags.put(k, v);
  }

  String name = charset.replaceNonMembers(id.name(), '_');
  tags.put("name", name);

  return tags;
}
 
Example 12
Source File: Query.java    From spectator with Apache License 2.0 5 votes vote down vote up
/** Convert {@code id} to a map. */
static Map<String, String> toMap(Id id) {
  Map<String, String> tags = new HashMap<>();
  for (Tag t : id.tags()) {
    tags.put(t.key(), t.value());
  }
  tags.put("name", id.name());
  return tags;
}
 
Example 13
Source File: RollupsTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private Id removeIdxTag(Id id) {
  List<Tag> filtered = new ArrayList<>();
  for (Tag t : id.tags()) {
    if (!"i".equals(t.key())) {
      filtered.add(t);
    }
  }
  return registry.createId(id.name()).withTags(filtered);
}
 
Example 14
Source File: EvaluatorTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
private Map<String, String> toMap(Id id) {
  Map<String, String> tags = new HashMap<>();
  for (Tag t : id.tags()) {
    tags.put(t.key(), t.value());
  }
  tags.put("name", id.name());
  return tags;
}