com.netflix.spectator.api.Id Java Examples

The following examples show how to use com.netflix.spectator.api.Id. 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: EVCacheMetricsFactory.java    From EVCache with Apache License 2.0 6 votes vote down vote up
public Timer getPercentileTimer(String metric, Collection<Tag> tags, Duration max) {
    final String name = tags != null ? metric + tags.toString() : metric;
    final Timer duration = timerMap.get(name);
    if (duration != null) return duration;

    writeLock.lock();
    try {
        if (timerMap.containsKey(name))
            return timerMap.get(name);
        else {
            Id id = getId(metric, tags);
            final Timer _duration = PercentileTimer.builder(getRegistry()).withId(id).withRange(Duration.ofNanos(100000), max).build();
            timerMap.put(name, _duration);
            return _duration;
        }
    } finally {
        writeLock.unlock();
    }
}
 
Example #2
Source File: ConsolidatorTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void noneRandom() {
  Id id = Id.create("test");
  Id measurementId = id.withTag("atlas.dstype", "rate").withTag(Statistic.count);
  ManualClock clock = new ManualClock();

  Counter primary = registry(clock, CONSOLIDATED_STEP).counter(id);
  Counter consolidated = registry(clock, CONSOLIDATED_STEP).counter(id);

  Consolidator consolidator = new Consolidator.None();

  consolidateRandomData(
      measurementId,
      clock,
      consolidator,
      primary::add,
      consolidated::add,
      primary::measure,
      consolidated::measure);
}
 
Example #3
Source File: Ids.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Threads(1)
@Benchmark
public void withTag(Blackhole bh) {
  Id id = registry.createId("http.req.complete")
      .withTag(    "nf.app", "test_app")
      .withTag("nf.cluster", "test_app-main")
      .withTag(    "nf.asg", "test_app-main-v042")
      .withTag(  "nf.stack", "main")
      .withTag(    "nf.ami", "ami-0987654321")
      .withTag( "nf.region", "us-east-1")
      .withTag(   "nf.zone", "us-east-1e")
      .withTag(   "nf.node", "i-1234567890")
      .withTag(   "country", "US")
      .withTag(    "device", "xbox")
      .withTag(    "status", "200")
      .withTag(    "client", "ab");
  bh.consume(id);
}
 
Example #4
Source File: Ids.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Threads(1)
@Benchmark
public void withTagsVararg(Blackhole bh) {
  Id id = registry.createId("http.req.complete").withTags(
          "nf.app", "test_app",
      "nf.cluster", "test_app-main",
          "nf.asg", "test_app-main-v042",
        "nf.stack", "main",
          "nf.ami", "ami-0987654321",
       "nf.region", "us-east-1",
         "nf.zone", "us-east-1e",
         "nf.node", "i-1234567890",
         "country", "US",
          "device", "xbox",
          "status", "200",
          "client", "ab");
  bh.consume(id);
}
 
Example #5
Source File: QueryIndexTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void removals() {
  QueryIndex<Query> idx = QueryIndex.newInstance(registry);
  idx.add(SIMPLE_QUERY, SIMPLE_QUERY);
  idx.add(HASKEY_QUERY, HASKEY_QUERY);
  idx.add(IN_QUERY, IN_QUERY);

  Id id1 = id("a", "key", "b", "c", "12345");
  Assertions.assertEquals(list(SIMPLE_QUERY, IN_QUERY, HASKEY_QUERY), idx.findMatches(id1));

  Assertions.assertFalse(idx.remove(Parser.parseQuery("name,a,:eq")));
  Assertions.assertEquals(list(SIMPLE_QUERY, IN_QUERY, HASKEY_QUERY), idx.findMatches(id1));

  Assertions.assertTrue(idx.remove(IN_QUERY));
  Assertions.assertEquals(list(SIMPLE_QUERY, HASKEY_QUERY), idx.findMatches(id1));

  Assertions.assertTrue(idx.remove(SIMPLE_QUERY));
  Assertions.assertEquals(list(HASKEY_QUERY), idx.findMatches(id1));

  Assertions.assertTrue(idx.remove(HASKEY_QUERY));
  Assertions.assertTrue(idx.isEmpty());
  Assertions.assertTrue(idx.findMatches(id1).isEmpty());

  idx.add(SIMPLE_QUERY, SIMPLE_QUERY);
  Assertions.assertEquals(list(SIMPLE_QUERY), idx.findMatches(id1));
}
 
Example #6
Source File: AtlasMeterRegistry.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Override
protected Meter newMeter(Meter.Id id, Meter.Type type, Iterable<io.micrometer.core.instrument.Measurement> measurements) {
    Id spectatorId = spectatorId(id);
    com.netflix.spectator.api.AbstractMeter<Id> spectatorMeter = new com.netflix.spectator.api.AbstractMeter<Id>(registry.clock(), spectatorId, spectatorId) {
        @Override
        public Iterable<com.netflix.spectator.api.Measurement> measure() {
            return stream(measurements.spliterator(), false)
                    .map(m -> {
                        com.netflix.spectator.api.Statistic stat = AtlasUtils.toSpectatorStatistic(m.getStatistic());
                        Id idWithStat = stat == null ? id : id.withTag("statistic", stat.toString());
                        return new com.netflix.spectator.api.Measurement(idWithStat, clock.wallTime(), m.getValue());
                    })
                    .collect(toList());
        }
    };
    registry.register(spectatorMeter);
    return new DefaultMeter(id, type, measurements);
}
 
Example #7
Source File: DefaultCapacityMonitoringServiceMetrics.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private EnumMap<Tier, EnumMap<ResourceType, AtomicLong>> buildResourceShortageMap(Registry registry) {
    EnumMap<Tier, EnumMap<ResourceType, AtomicLong>> result = new EnumMap<>(Tier.class);
    for (Tier tier : Tier.values()) {
        EnumMap<ResourceType, AtomicLong> resourceMap = new EnumMap<>(ResourceType.class);
        result.put(tier, resourceMap);

        Id tierId = registry.createId(MetricConstants.METRIC_CAPACITY_MANAGEMENT + "monitor.capacityShortage", "tier", tier.name());

        for (ResourceType resourceType : ResourceType.values()) {
            resourceMap.put(
                    resourceType,
                    registry.gauge(tierId.withTag("resourceType", resourceType.name()), new AtomicLong())
            );
        }
    }
    return result;
}
 
Example #8
Source File: Ids.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Threads(1)
@Benchmark
public void withTagsVarargSorted(Blackhole bh) {
  Id id = registry.createId("http.req.complete").withTags(
          "client", "ab",
         "country", "US",
          "device", "xbox",
          "nf.ami", "ami-0987654321",
          "nf.app", "test_app",
          "nf.asg", "test_app-main-v042",
      "nf.cluster", "test_app-main",
         "nf.node", "i-1234567890",
       "nf.region", "us-east-1",
        "nf.stack", "main",
         "nf.zone", "us-east-1e",
          "status", "200"
  );
  bh.consume(id);
}
 
Example #9
Source File: GcsStorageService.java    From front50 with Apache License 2.0 6 votes vote down vote up
private <T> T timeExecute(Id timerId, AbstractGoogleClientRequest<T> request) throws IOException {
  T result;
  Clock clock = registry.clock();
  long startTime = clock.monotonicTime();
  int statusCode = -1;

  try {
    result = request.execute();
    statusCode = request.getLastStatusCode();
  } catch (HttpResponseException e) {
    statusCode = e.getStatusCode();
    throw e;
  } catch (IOException ioex) {
    throw ioex;
  } catch (Exception ex) {
    throw new IllegalStateException(ex);
  } finally {
    long nanos = clock.monotonicTime() - startTime;
    String status = Integer.toString(statusCode).charAt(0) + "xx";

    Id id = timerId.withTags("status", status, "statusCode", Integer.toString(statusCode));
    registry.timer(id).record(nanos, TimeUnit.NANOSECONDS);
  }
  return result;
}
 
Example #10
Source File: AgentCacheMetrics.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private InstanceGroupMetrics(AgentInstanceGroup instanceGroup) {
    this.instanceGroup = instanceGroup;

    List<Tag> commonTags = asList(
            new BasicTag("id", instanceGroup.getId()),
            new BasicTag("instanceType", instanceGroup.getInstanceType()),
            new BasicTag("state", instanceGroup.getLifecycleStatus().getState().name()),
            new BasicTag("tier", instanceGroup.getTier().name())
    );

    Id counterId = registry.createId(MetricConstants.METRIC_AGENT + "instanceGroup", commonTags);
    this.counter = registry.gauge(counterId, new AtomicInteger(1));

    Id capacityId = registry.createId(MetricConstants.METRIC_AGENT + "instanceGroup.capacity", commonTags);
    this.min = registry.gauge(capacityId.withTag("size", "min"), new AtomicInteger(instanceGroup.getMin()));
    this.desired = registry.gauge(capacityId.withTag("size", "desired"), new AtomicInteger(instanceGroup.getDesired()));
    this.current = registry.gauge(capacityId.withTag("size", "current"), new AtomicInteger(instanceGroup.getCurrent()));
    this.max = registry.gauge(capacityId.withTag("size", "max"), new AtomicInteger(instanceGroup.getMax()));
}
 
Example #11
Source File: RollupsTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void aggregateGauges() {
  for (int i = 0; i < 10; ++i) {
    registry.gauge("test", "i", "" + i).set(2.0);
  }
  clock.setWallTime(5000);
  List<Measurement> input = registry.measurements().collect(Collectors.toList());
  List<Measurement> aggr = Rollups.aggregate(this::removeIdxTag, input);
  Assertions.assertEquals(1, aggr.size());

  Measurement m = aggr.get(0);
  Id id = registry.createId("test")
      .withTag("atlas.dstype", "gauge")
      .withTag(Statistic.gauge);
  Assertions.assertEquals(id, m.id());
  Assertions.assertEquals(2.0, m.value(), 1e-12);
}
 
Example #12
Source File: IpcLogEntry.java    From spectator with Apache License 2.0 6 votes vote down vote up
private Id getInflightId() {
  if (inflightId == null) {
    Map<String, String> tags = new HashMap<>();

    // Required for both client and server
    putTag(tags, IpcTagKey.owner.key(), owner);

    // Optional for both client and server
    putTag(tags, IpcTagKey.vip.key(), vip);

    String name = isClient()
        ? IpcMetric.clientInflight.metricName()
        : IpcMetric.serverInflight.metricName();
    inflightId = registry.createId(name, tags);
  }
  return inflightId;
}
 
Example #13
Source File: EVCacheMetricsFactory.java    From EVCache with Apache License 2.0 6 votes vote down vote up
public Counter getCounter(String cName, Collection<Tag> tags) {
    final String name = tags != null ? cName + tags.toString() : cName;
    Counter counter = counterMap.get(name);
    if (counter == null) {
        writeLock.lock();
        try {
            if (counterMap.containsKey(name)) {
                counter = counterMap.get(name);
            } else {
                List<Tag> tagList = new ArrayList<Tag>(tags.size() + 1);
                tagList.addAll(tags);
                final Id id = getId(cName, tagList);
                counter = getRegistry().counter(id);
                counterMap.put(name, counter);
            }
        } finally {
            writeLock.unlock();
        }
    }
    return counter;
}
 
Example #14
Source File: PollMetersBench.java    From spectator with Apache License 2.0 5 votes vote down vote up
private Id randomId(Random r) {
  Id tmp = Id.create(randomString(r, 2 + r.nextInt(120)));
  int n = r.nextInt(20);
  for (int i = 0; i < n; ++i) {
    String k = randomString(r, 2 + r.nextInt(60));
    String v = randomString(r, 2 + r.nextInt(120));
    tmp = tmp.withTag(k, v);
  }
  return tmp;
}
 
Example #15
Source File: PolledMeter.java    From spectator with Apache License 2.0 5 votes vote down vote up
/** Force the polling of all meters associated with the registry. */
public static void update(Registry registry) {
  Iterator<Map.Entry<Id, Object>> iter = registry.state().entrySet().iterator();
  while (iter.hasNext()) {
    Map.Entry<Id, Object> entry = iter.next();
    if (entry.getValue() instanceof AbstractMeterState) {
      AbstractMeterState tuple = (AbstractMeterState) entry.getValue();
      tuple.doUpdate(registry);
      if (tuple.hasExpired()) {
        iter.remove();
      }
    }
  }
}
 
Example #16
Source File: Ids.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Threads(1)
@Benchmark
public void emptyAppend4(Blackhole bh) {
  Id id = emptyId.withTags(
      "country", "US",
      "device", "xbox",
      "status", "200",
      "client", "ab");
  bh.consume(id);
}
 
Example #17
Source File: MeasurementSerializerTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void encode() throws Exception {
  Id id = registry.createId("foo", "bar", "baz");
  Measurement m = new Measurement(id, 42L, 3.0);
  String json = mapper.writeValueAsString(m);
  String tags = "{\"name\":\"foo\",\"bar\":\"baz\",\"atlas.dstype\":\"gauge\"}";
  String expected = "{\"tags\":" + tags + ",\"timestamp\":42,\"value\":3.0}";
  Assertions.assertEquals(expected, json);
}
 
Example #18
Source File: SparkNameFunctionTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void executorMetric20_CodeGenerator() {
  final String name = "97278898-4bd4-49c2-9889-aa5f969a7816-0013.2.CodeGenerator.compilationTime";
  final Id expected = registry.createId("spark.CodeGenerator.compilationTime")
          .withTag("appId", "97278898-4bd4-49c2-9889-aa5f969a7816-0013")
          .withTag("executorId", "2")
          .withTag("role", "executor");
  assertEquals(expected, f.apply(name));
}
 
Example #19
Source File: AtlasMeter.java    From spectator with Apache License 2.0 5 votes vote down vote up
/** Create a new instance. */
AtlasMeter(Id id, Clock clock, long ttl) {
  this.id = id;
  this.clock = clock;
  this.ttl = ttl;
  lastUpdated = clock.wallTime();
}
 
Example #20
Source File: MicrometerRegistry.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Override public Meter get(Id id) {
  try {
    return impl.get(id.name())
        .tags(convert(id.tags()))
        .meters()
        .stream()
        .filter(m -> id.equals(convert(m.getId())))
        .map(this::convert)
        .filter(Objects::nonNull)
        .findFirst()
        .orElse(null);
  } catch (MeterNotFoundException e) {
    return null;
  }
}
 
Example #21
Source File: Ids.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Threads(1)
@Benchmark
public void emptyAppend2(Blackhole bh) {
  Id id = emptyId.withTags(
      "country", "US",
      "device", "xbox");
  bh.consume(id);
}
 
Example #22
Source File: FsmMetricsImpl.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
FsmMetricsImpl(Id rootId,
               Function<S, String> nameOf,
               Function<S, Boolean> finalStateEval,
               S initialState,
               Registry registry) {
    this.nameOf = nameOf;
    this.finalStateEval = finalStateEval;
    this.registry = registry;

    String effectiveRootName = rootId.name().endsWith(".") ? rootId.name() : rootId.name() + '.';
    this.baseStateId = registry.createId(effectiveRootName + "currentState", rootId.tags());
    this.baseUpdatesId = registry.createId(effectiveRootName + "updates", rootId.tags());
    this.currentState = new AtomicReference<>(new StateHolder(initialState, ""));
}
 
Example #23
Source File: BucketDistributionSummary.java    From spectator with Apache License 2.0 5 votes vote down vote up
/** Create a new instance. */
BucketDistributionSummary(Registry registry, Id id, LongFunction<String> f) {
  this.registry = registry;
  this.id = id;
  this.f = f;
  this.summaries = new ConcurrentHashMap<>();
}
 
Example #24
Source File: AbstractInvocationMeters.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
protected AbstractInvocationMeter getOrCreateMeters(Invocation invocation, Response response) {
  // build string key is faster then use Id to locate timer directly
  StringBuilder keyBuilder = new StringBuilder(maxKeyLen);
  String invocationName;
  //check edge
  if (invocation.isConsumer() && invocation.isEdge()) {
    invocationName = MeterInvocationConst.EDGE_INVOCATION_NAME;
  } else {
    invocationName = invocation.getInvocationType().name();
  }

  keyBuilder
      .append(invocationName)
      .append(invocation.getRealTransportName())
      .append(invocation.getMicroserviceQualifiedName())
      .append(response.getStatusCode());
  if (keyBuilder.length() > maxKeyLen) {
    maxKeyLen = keyBuilder.length();
  }

  return metersMap.computeIfAbsent(keyBuilder.toString(), k -> {
    Id id = registry.createId(MeterInvocationConst.INVOCATION_NAME,
        MeterInvocationConst.TAG_ROLE,
        invocationName,
        MeterInvocationConst.TAG_TRANSPORT,
        invocation.getRealTransportName(),
        MeterInvocationConst.TAG_OPERATION,
        invocation.getMicroserviceQualifiedName(),
        MeterInvocationConst.TAG_STATUS,
        String.valueOf(response.getStatusCode()));

    AbstractInvocationMeter meter = createMeter(id);
    SpectatorUtils.registerMeter(registry, meter);
    return meter;
  });
}
 
Example #25
Source File: SpectatorReporter.java    From spectator with Apache License 2.0 5 votes vote down vote up
private void setGaugeValue(String name, double v) {
  Id id = nameFunction.apply(name);
  if (id != null) {
    final double cv = valueFunction.convert(name, v);
    LOGGER.debug("setting gauge {} to {}", name, cv);
    spectatorRegistry.gauge(id).set(cv);
  }
}
 
Example #26
Source File: PolledMeterTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void monitorStaticMethodMonotonicCounter() {
  Registry r = new DefaultRegistry();
  Id id = r.createId("test");

  PolledMeter.using(r)
      .withId(id)
      .monitorStaticMethodMonotonicCounter(PolledMeterTest::testCounter);
  PolledMeter.update(r);
  PolledMeter.update(r);
  PolledMeter.update(r);
  PolledMeter.update(r);

  Assertions.assertEquals(4, r.counter(id).count());
}
 
Example #27
Source File: MeasurementSerializerTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void publishPayloadWithCommonTags() throws Exception {
  Id id = registry.createId("foo");
  Measurement m = new Measurement(id, 42L, 3.0);
  PublishPayload p = new PublishPayload(Collections.singletonMap("a", "b"), Collections.singletonList(m));
  String json = mapper.writeValueAsString(p);
  String tags = "{\"name\":\"foo\",\"atlas.dstype\":\"gauge\"}";
  String mjson = "{\"tags\":" + tags + ",\"timestamp\":42,\"value\":3.0}";
  String expected = "{\"tags\":{\"a\":\"b\"},\"metrics\":[" + mjson + "]}";
  Assertions.assertEquals(expected, json);
}
 
Example #28
Source File: EdgeInvocationMeter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public EdgeInvocationMeter(Registry registry, Id id) {
  super(registry, id);
  executorQueueTimer = createStageTimer(MeterInvocationConst.STAGE_EXECUTOR_QUEUE);
  serverFiltersRequestTimer = createStageTimer(MeterInvocationConst.STAGE_SERVER_FILTERS_REQUEST);
  serverFiltersResponseTimer = createStageTimer(MeterInvocationConst.STAGE_SERVER_FILTERS_RESPONSE);
  sendResponseTimer = createStageTimer(MeterInvocationConst.STAGE_PRODUCER_SEND_RESPONSE);
}
 
Example #29
Source File: LatencyDistributionMeter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public LatencyDistributionMeter(Id id, String config) {
  this.id = id;

  LatencyDistributionConfig latencyDistributionConfig = new LatencyDistributionConfig(config);
  for (LatencyScopeConfig scopeConfig : latencyDistributionConfig.getScopeConfigs()) {
    latencyScopeMeters.add(new LatencyScopeMeter(id, scopeConfig));
  }
}
 
Example #30
Source File: IpcMetricTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void validateFailureInjectionInvalid() {
  Assertions.assertThrows(IllegalArgumentException.class, () -> {
    Id id = registry.createId(IpcMetric.clientCall.metricName())
            .withTag(IpcTagKey.owner.tag("test"))
            .withTag(IpcResult.success)
            .withTag(IpcStatus.success)
            .withTag(IpcAttempt.initial)
            .withTag(Tag.of(IpcTagKey.failureInjected.key(), "false"))
            .withTag(IpcTagKey.attemptFinal.key(), true);
    IpcMetric.clientCall.validate(id);
  });
}