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

The following examples show how to use com.netflix.spectator.api.Id#withTag() . 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: VertxMetersInitializer.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Override
public void init(GlobalRegistry globalRegistry, EventBus eventBus, MetricsBootstrapConfig config) {
  Registry registry = globalRegistry.getDefaultRegistry();

  Id endpointsId = registry.createId(VERTX_ENDPOINTS);
  VertxEndpointsMeter clientMeter = new HttpClientEndpointsMeter(
      endpointsId.withTag(ENDPOINTS_TYPE, ENDPOINTS_CLINET),
      SharedVertxFactory.getMetricsFactory()
          .getVertxMetrics()
          .getClientEndpointMetricManager()
          .getClientEndpointMetricMap());
  SpectatorUtils.registerMeter(registry, clientMeter);

  VertxEndpointsMeter serverMeter = new ServerEndpointsMeter(
      endpointsId.withTag(ENDPOINTS_TYPE, ENDPOINTS_SERVER),
      SharedVertxFactory.getMetricsFactory()
          .getVertxMetrics()
          .getServerEndpointMetricMap());
  SpectatorUtils.registerMeter(registry, serverMeter);
}
 
Example 2
Source File: Utils.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public static MeasurementNode createStageNode(String stage,
    double count,
    double totalTime,
    double max) {
  Id id = registry.createId("id").withTag(Statistic.count);
  Measurement countMeasurement = new Measurement(id.withTag(Statistic.count), 0, count);
  Measurement totalTimeMeasurement = new Measurement(id.withTag(Statistic.totalTime), 0, totalTime);
  Measurement maxMeasurement = new Measurement(id.withTag(Statistic.max), 0, max);

  MeasurementNode stageNode = new MeasurementNode(stage, null);
  stageNode.addChild(Statistic.count.name(), countMeasurement);
  stageNode.addChild(Statistic.totalTime.name(), totalTimeMeasurement);
  stageNode.addChild(Statistic.max.name(), maxMeasurement);

  return stageNode;
}
 
Example 3
Source File: SparkNameFunction.java    From spectator with Apache License 2.0 6 votes vote down vote up
Id apply(String metric) {
  final Matcher m = pattern.matcher(metric);
  if (m.matches()) {
    Id id = registry.createId(PREFIX + m.group(name));
    for (Map.Entry<String, Integer> entry : tags.entrySet()) {
      id = id.withTag(entry.getKey(), m.group(entry.getValue()));
    }
    // separate executor jvm metrics from driver executor metrics
    if (!tags.containsKey("role")) {
      id = id.withTag("role", "executor");
    }
    return id;
  } else {
    return DROP_METRIC;
  }

}
 
Example 4
Source File: DoubleDistributionSummary.java    From spectator with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new instance.
 */
DoubleDistributionSummary(Clock clock, Id id, long resetFreq) {
  this.clock = clock;
  this.id = id;
  this.resetFreq = resetFreq;
  lastResetTime = new AtomicLong(clock.wallTime());
  lastUpdateTime = new AtomicLong(clock.wallTime());
  count = new AtomicLong(0L);
  totalAmount = new AtomicLong(ZERO);
  totalOfSquares = new AtomicLong(ZERO);
  max = new AtomicLong(ZERO);
  countId = id.withTag(Statistic.count);
  totalAmountId = id.withTag(Statistic.totalAmount);
  totalOfSquaresId = id.withTag(Statistic.totalOfSquares);
  maxId = id.withTag(Statistic.max);
}
 
Example 5
Source File: CpuMeter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public CpuMeter(Id id) {
  allCpuUsage = new OsCpuUsage(id.withTag(OsMeter.OS_TYPE, OsMeter.OS_TYPE_ALL_CPU));
  processCpuUsage = new ProcessCpuUsage(id.withTag(OsMeter.OS_TYPE, OsMeter.OS_TYPE_PROCESS_CPU));

  //must refresh all first
  update();
  allCpuUsage.setUsage(0);
  processCpuUsage.setUsage(0);
}
 
Example 6
Source File: EndpointMeter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public EndpointMeter(Id id, DefaultEndpointMetric metric) {
  id = id.withTag(ADDRESS, metric.getAddress().toString());
  this.id = id;
  idConnect = id.withTag(STATISTIC, CONNECT_COUNT);
  idDisconnect = id.withTag(STATISTIC, DISCONNECT_COUNT);
  idConnections = id.withTag(STATISTIC, CONNECTIONS);
  idBytesRead = id.withTag(STATISTIC, BYTES_READ);
  idBytesWritten = id.withTag(STATISTIC, BYTES_WRITTEN);
  this.metric = metric;
}
 
Example 7
Source File: GcsStorageService.java    From front50 with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
GcsStorageService(
    String bucketName,
    String bucketLocation,
    String basePath,
    String projectName,
    Storage storage,
    int maxRetries,
    TaskScheduler taskScheduler,
    Registry registry) {
  this.bucketName = bucketName;
  this.bucketLocation = bucketLocation;
  this.basePath = basePath;
  this.projectName = projectName;
  this.storage = storage;
  this.registry = registry;
  this.obj_api = storage.objects();
  this.dataFilename = DEFAULT_DATA_FILENAME;
  this.taskScheduler = taskScheduler;
  this.safeRetry = GoogleCommonSafeRetry.builder().maxRetries(maxRetries).build();

  Id id = registry.createId("google.storage.invocation");
  deleteTimer = id.withTag("method", "delete");
  purgeTimer = id.withTag("method", "purgeTimestamp");
  loadTimer = id.withTag("method", "load");
  listTimer = id.withTag("method", "list");
  mediaDownloadTimer = id.withTag("method", "mediaDownload");
  insertTimer = id.withTag("method", "insert");
  patchTimer = id.withTag("method", "patch");
}
 
Example 8
Source File: JobAndTaskMetrics.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private List<Gauge> updateStateCounters(Id baseId, String state, Histogram.Builder histogramBuilder, List<Gauge> gauges) {
    if (histogramBuilder == null) {
        // Nothing running for this state, reset gauges
        if (gauges != null) {
            gauges.forEach(g -> g.set(0));
        }
        return Collections.emptyList();
    }

    List<Long> counters = histogramBuilder.build().getCounters();

    // First time we have data for this capacity group.
    if (gauges == null) {
        Id id = baseId.withTag("state", state);
        List<Long> valueBounds = HISTOGRAM_DESCRIPTOR.getValueBounds();
        List<Gauge> newGauges = new ArrayList<>();
        for (int i = 0; i <= valueBounds.size(); i++) {
            Gauge newGauge;
            if (i < valueBounds.size()) {
                long delayMs = valueBounds.get(i);
                newGauge = registry.gauge(id.withTag("delay", DateTimeExt.toTimeUnitString(delayMs)));
            } else {
                newGauge = registry.gauge(id.withTag("delay", "Unlimited"));
            }
            newGauge.set(counters.get(i));
            newGauges.add(newGauge);
        }
        return newGauges;
    }

    // Update gauges
    for (int i = 0; i < counters.size(); i++) {
        gauges.get(i).set(counters.get(i));
    }

    return gauges;
}
 
Example 9
Source File: FsmMetricsImpl.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private StateHolder(S state, String reason) {
    String stateName = nameOf.apply(state);
    this.state = state;
    this.lifecycle = StateHolderLifecycle.Active;
    this.registry = FsmMetricsImpl.this.registry;

    Id currentUpdateId = baseUpdatesId.withTag("state", stateName);
    if (StringExt.isNotEmpty(reason)) {
        currentUpdateId = currentUpdateId.withTag("reason", reason);
    }
    registry.counter(currentUpdateId).increment();

    this.currentStateId = baseStateId.withTag("state", stateName);
    if (!finalStateEval.apply(state)) {
        PolledMeter.using(registry).withId(this.currentStateId).monitorValue(this,
                // Be sure to access all fields via 'self' object in this method
                self -> {
                    switch (self.lifecycle) {
                        case Active:
                            return 1;
                        case Inactive:
                            self.lifecycle = StateHolderLifecycle.Removable;
                            return 0;
                    }
                    // Removable
                    PolledMeter.remove(self.registry, self.currentStateId);
                    return 0;
                }
        );
    }
}
 
Example 10
Source File: ManyTags.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Override public void accept(Registry registry) {
  Random random = new Random(42);
  for (int i = 0; i < 10000; ++i) {
    Id id = registry.createId("manyTagsTest");
    for (int j = 0; j < 20; ++j) {
      String v = "" + random.nextInt(10);
      id = id.withTag("" + j, v);
    }
    registry.counter(id).increment();
  }
}
 
Example 11
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 12
Source File: GcsStorageService.java    From front50 with Apache License 2.0 4 votes vote down vote up
public GcsStorageService(
    String bucketName,
    String bucketLocation,
    String basePath,
    String projectName,
    String credentialsPath,
    String applicationVersion,
    String dataFilename,
    Integer connectTimeoutSec,
    Integer readTimeoutSec,
    int maxWaitInterval,
    int retryIntervalBase,
    int jitterMultiplier,
    int maxRetries,
    TaskScheduler taskScheduler,
    Registry registry) {
  Storage storage;

  try {
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    GoogleCredentials credentials = loadCredential(credentialsPath);
    HttpRequestInitializer requestInitializer =
        new HttpCredentialsAdapter(credentials) {
          public void initialize(HttpRequest request) throws IOException {
            super.initialize(request);
            request.setConnectTimeout(connectTimeoutSec * 1000);
            request.setReadTimeout(readTimeoutSec * 1000);
          }
        };

    String applicationName = "Spinnaker/" + applicationVersion;
    storage =
        new Storage.Builder(httpTransport, jsonFactory, requestInitializer)
            .setApplicationName(applicationName)
            .build();
  } catch (IOException | java.security.GeneralSecurityException e) {
    throw new IllegalStateException(e);
  }

  // "google.com:" is deprecated but may be in certain old projects.
  this.bucketName = bucketName.replace("google.com:", "");
  this.bucketLocation = bucketLocation;
  this.basePath = basePath;
  this.projectName = projectName;
  this.storage = storage;
  this.obj_api = this.storage.objects();
  this.dataFilename = dataFilename;
  this.taskScheduler = taskScheduler;
  this.registry = registry;
  this.safeRetry =
      GoogleCommonSafeRetry.builder()
          .maxWaitInterval(maxWaitInterval)
          .retryIntervalBase(retryIntervalBase)
          .jitterMultiplier(jitterMultiplier)
          .maxRetries(maxRetries)
          .build();

  Id id = registry.createId("google.storage.invocation");
  deleteTimer = id.withTag("method", "delete");
  purgeTimer = id.withTag("method", "purgeTimestamp");
  loadTimer = id.withTag("method", "load");
  listTimer = id.withTag("method", "list");
  mediaDownloadTimer = id.withTag("method", "mediaDownload");
  insertTimer = id.withTag("method", "insert");
  patchTimer = id.withTag("method", "patch");
}
 
Example 13
Source File: HttpLogEntry.java    From spectator with Apache License 2.0 4 votes vote down vote up
private static void log(Logger logger, Marker marker, HttpLogEntry entry) {
  Preconditions.checkNotNull(entry.method, "method");

  Id dimensions = REGISTRY.createId("tags")
      .withTag("mode", marker.getName())
      .withTag("status", entry.getStatusTag())
      .withTag("statusCode", entry.getStatusCodeTag())
      .withTag("method", entry.method);

  if (entry.clientName != null) {
    dimensions = dimensions.withTag("client", entry.clientName);
  }

  if (marker == SERVER && entry.path != null) {
    dimensions = dimensions.withTag("endpoint", longestPrefixMatch(entry.path, "other"));
  }

  // Update stats for the final attempt after retries are exhausted
  if (!entry.canRetry || entry.attempt >= entry.maxAttempts) {
    BucketTimer.get(REGISTRY, COMPLETE.withTags(dimensions.tags()), BUCKETS)
        .record(entry.getOverallLatency(), TimeUnit.MILLISECONDS);
  }

  // Update stats for every actual http request
  BucketTimer.get(REGISTRY, ATTEMPT.withTags(dimensions.tags()), BUCKETS)
      .record(entry.getLatency(), TimeUnit.MILLISECONDS);
  REGISTRY.distributionSummary(REQ_HEADER_SIZE.withTags(dimensions.tags()))
      .record(entry.getRequestHeadersLength());
  REGISTRY.distributionSummary(REQ_ENTITY_SIZE.withTags(dimensions.tags()))
      .record(entry.requestContentLength);
  REGISTRY.distributionSummary(RES_HEADER_SIZE.withTags(dimensions.tags()))
      .record(entry.getResponseHeadersLength());
  REGISTRY.distributionSummary(RES_ENTITY_SIZE.withTags(dimensions.tags()))
      .record(entry.responseContentLength);

  // Write data out to logger if enabled. For many monitoring use-cases there tend to be
  // frequent requests that can be quite noisy so the log level is set to debug. This class is
  // mostly intended to generate something like an access log so it presumes users who want the
  // information will configure an appender based on the markers to send the data to a
  // dedicated file. Others shouldn't have to deal with the spam in the logs, so debug for the
  // level seems reasonable.
  if (logger.isDebugEnabled(marker)) {
    logger.debug(marker, entry.toString());
  }
}
 
Example 14
Source File: MetricsInterceptor.java    From kork with Apache License 2.0 4 votes vote down vote up
@Override
public void afterCompletion(
    HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
    throws Exception {
  if (handler instanceof HandlerMethod) {
    HandlerMethod handlerMethod = (HandlerMethod) handler;

    String controller = handlerMethod.getMethod().getDeclaringClass().getSimpleName();
    if (controllersToExclude.contains(controller)) {
      return;
    }

    Integer status = response.getStatus();
    if (ex != null) {
      // propagated exceptions should get tracked as '500' regardless of response status
      status = 500;
    }

    Id id =
        registry
            .createId(metricName)
            .withTag("controller", controller)
            .withTag("method", handlerMethod.getMethod().getName())
            .withTag("status", status.toString().charAt(0) + "xx")
            .withTag("statusCode", status.toString());

    Map variables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
    for (String pathVariable : pathVariablesToTag) {
      if (variables.containsKey(pathVariable)) {
        id = id.withTag(pathVariable, variables.get(pathVariable).toString());
      } else {
        id = id.withTag(pathVariable, "None");
      }
    }

    for (String queryParamName : queryParamsToTag) {
      String parameter = request.getParameter(queryParamName);
      if (parameter != null) {
        id = id.withTag(queryParamName, parameter);
      } else {
        id = id.withTag(queryParamName, "None");
      }
    }

    if (ex != null) {
      id = id.withTag("success", "false").withTag("cause", ex.getClass().getSimpleName());
    } else {
      id = id.withTag("success", "true").withTag("cause", "None");
    }

    PercentileTimer.get(registry, id)
        .record(
            getNanoTime() - ((Long) request.getAttribute(TIMER_ATTRIBUTE)), TimeUnit.NANOSECONDS);

    PercentileDistributionSummary.get(
            registry, registry.createId(contentLengthMetricName).withTags(id.tags()))
        .record(request.getContentLengthLong());
  }
}
 
Example 15
Source File: SimpleTimer.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
public SimpleTimer(Id id) {
  this.id = id;
  this.idCount = id.withTag(Statistic.count);
  this.idTotalTime = id.withTag(Statistic.totalTime);
  this.idMax = id.withTag(Statistic.max);
}
 
Example 16
Source File: LatencyScopeMeter.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
public LatencyScopeMeter(Id latencyDistributionId, LatencyScopeConfig config) {
  nanoMin = TimeUnit.MILLISECONDS.toNanos(config.getMsMin());
  nanoMax = TimeUnit.MILLISECONDS.toNanos(config.getMsMax());
  scopeId = latencyDistributionId.withTag("scope",
      String.format("[%d,%s)", config.getMsMin(), config.getMsMax() == Long.MAX_VALUE ? "" : config.getMsMax()));
}
 
Example 17
Source File: InterfaceUsage.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
public InterfaceUsage(Id id, String name) {
  this.name = name;
  id = id.withTag(INTERFACE, name);
  init(id);
}