/** * Gets the value of the given metrics. * * @param tags tags for the request * @param metric names of the metric * @return values of the metrics * @throws IOException if a network error occurred * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server */ public MetricQueryResult query(Map<String, String> tags, String metric) throws IOException, UnauthenticatedException, UnauthorizedException { return query(tags, ImmutableList.of(metric), ImmutableList.<String>of(), ImmutableMap.<String, String>of()); }
public RuntimeMetrics getServiceMetrics(ServiceId serviceId) { return getMetrics(MetricsTags.service(serviceId), Constants.Metrics.Name.Service.SERVICE_INPUT, Constants.Metrics.Name.Service.SERVICE_PROCESSED, Constants.Metrics.Name.Service.SERVICE_EXCEPTIONS); }
@Override public RuntimeMetrics getMetrics() { return metricsClient.getServiceMetrics(serviceId); } }
/** * Gets the value of the given metrics. * * @param tags tags for the request * @param metrics names of the metrics * @param groupBys groupBys for the request * @param timeRangeParams parameters specifying the time range * @return values of the metrics * @throws IOException if a network error occurred * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server */ // TODO: take in query object shared by MetricsHandler public MetricQueryResult query(Map<String, String> tags, List<String> metrics, List<String> groupBys, Map<String, String> timeRangeParams) throws IOException, UnauthenticatedException, UnauthorizedException { List<String> queryParts = Lists.newArrayList(); queryParts.add("target=tag"); add("metric", metrics, queryParts); add("groupBy", groupBys, queryParts); addTags(tags, queryParts); addTimeRangeParametersToQuery(timeRangeParams, queryParts); URL url = config.resolveURLV3(String.format("metrics/query?%s", Joiner.on("&").join(queryParts))); HttpResponse response = restClient.execute(HttpMethod.POST, url, config.getAccessToken()); return ObjectResponse.fromJsonBody(response, MetricQueryResult.class).getResponseObject(); }
@Override public void perform(Arguments arguments, PrintStream output) throws Exception { Map<String, String> tags = ArgumentParser.parseMap(arguments.getOptional("tags", ""), "<tags>"); List<String> results = client.searchMetrics(tags); for (String result : results) { output.println(result); } }
public RemoteServiceManager(ServiceId serviceId, ClientConfig clientConfig, RESTClient restClient, RemoteApplicationManager remoteApplicationManager) { super(serviceId, remoteApplicationManager); this.serviceId = serviceId; this.metricsClient = new MetricsClient(clientConfig, restClient); this.programClient = new ProgramClient(clientConfig, restClient); this.serviceClient = new ServiceClient(clientConfig, restClient); }
@Override public void perform(Arguments arguments, PrintStream output) throws Exception { Map<String, String> tags = ArgumentParser.parseMap(arguments.getOptional("tags", ""), "<tags>"); List<MetricTagValue> results = client.searchTags(tags); for (MetricTagValue result : results) { output.printf("%s=%s\n", result.getName(), result.getValue()); } }
/** * Searches for metrics matching the given tags. * * @param tags the tags to match * @return the metrics matching the given tags * @throws IOException if a network error occurred * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server */ public List<String> searchMetrics(Map<String, String> tags) throws IOException, UnauthenticatedException, UnauthorizedException { List<String> queryParts = Lists.newArrayList(); queryParts.add("target=metric"); addTags(tags, queryParts); URL url = config.resolveURLV3(String.format("metrics/search?%s", Joiner.on("&").join(queryParts))); HttpResponse response = restClient.execute(HttpMethod.POST, url, config.getAccessToken()); ObjectResponse<List<String>> result = ObjectResponse.fromJsonBody( response, new TypeToken<List<String>>() { }.getType()); return result.getResponseObject(); }
@Override public void perform(Arguments arguments, PrintStream output) throws Exception { Map<String, String> tags = ArgumentParser.parseMap(arguments.getOptional("tags", ""), "<tags>"); List<String> results = client.searchMetrics(tags); for (String result : results) { output.println(result); } }
@SuppressWarnings("unused") protected MetricsClient getMetricsClient() { return new MetricsClient(getClientConfig(), getRestClient()); }
@Override public void perform(Arguments arguments, PrintStream output) throws Exception { Map<String, String> tags = ArgumentParser.parseMap(arguments.getOptional("tags", ""), "<tags>"); List<MetricTagValue> results = client.searchTags(tags); for (MetricTagValue result : results) { output.printf("%s=%s\n", result.getName(), result.getValue()); } }
/** * Searches for metrics tags matching the given tags. * * @param tags the tags to match * @return the metrics matching the given tags * @throws IOException if a network error occurred * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server */ public List<MetricTagValue> searchTags(Map<String, String> tags) throws IOException, UnauthenticatedException, UnauthorizedException { List<String> queryParts = Lists.newArrayList(); queryParts.add("target=tag"); addTags(tags, queryParts); URL url = config.resolveURLV3(String.format("metrics/search?%s", Joiner.on("&").join(queryParts))); HttpResponse response = restClient.execute(HttpMethod.POST, url, config.getAccessToken()); ObjectResponse<List<MetricTagValue>> result = ObjectResponse.fromJsonBody( response, new TypeToken<List<MetricTagValue>>() { }.getType()); return result.getResponseObject(); }
/** * Gets the value of the given metrics. * * @param tags tags for the request * @param metrics names of the metrics * @param groupBys groupBys for the request * @return values of the metrics * @throws IOException if a network error occurred * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server */ public MetricQueryResult query(Map<String, String> tags, List<String> metrics, List<String> groupBys, @Nullable String start, @Nullable String end) throws IOException, UnauthenticatedException, UnauthorizedException { Map<String, String> timeRangeParams = Maps.newHashMap(); if (start != null) { timeRangeParams.put("start", start); } if (end != null) { timeRangeParams.put("end", end); } return query(tags, metrics, groupBys, timeRangeParams); }
private long getTotalCounter(Map<String, String> tags, String metricName) { try { MetricQueryResult result = query(tags, metricName); if (result.getSeries().length == 0) { return 0; } MetricQueryResult.TimeValue[] timeValues = result.getSeries()[0].getData(); if (timeValues.length == 0) { return 0; } // since it is totals, we know there's one value only return timeValues[0].getValue(); } catch (Exception e) { throw Throwables.propagate(e); } }
@Override public void perform(Arguments arguments, PrintStream output) throws Exception { String metric = arguments.get("metric-name"); Map<String, String> tags = ArgumentParser.parseMap(arguments.getOptional("tags", ""), "<tags>"); String start = arguments.getOptional("start", ""); String end = arguments.getOptional("end", ""); MetricQueryResult result = client.query( tags, ImmutableList.of(metric), ImmutableList.<String>of(), start.isEmpty() ? null : start, end.isEmpty() ? null : end); output.printf("Start time: %d\n", result.getStartTime()); output.printf("End time: %d\n", result.getEndTime()); for (MetricQueryResult.TimeSeries series : result.getSeries()) { output.println(); output.printf("Series: %s\n", series.getMetricName()); if (!series.getGrouping().isEmpty()) { output.printf("Grouping: %s\n", Joiner.on(",").withKeyValueSeparator("=").join(series.getGrouping())); } Table table = Table.builder() .setHeader("timestamp", "value") .setRows(ImmutableList.copyOf(series.getData()), new RowMaker<MetricQueryResult.TimeValue>() { @Override public List<?> makeRow(MetricQueryResult.TimeValue object) { return Lists.newArrayList(object.getTime(), object.getValue()); } }).build(); cliConfig.getTableRenderer().render(cliConfig, output, table); } }
@Override public void perform(Arguments arguments, PrintStream output) throws Exception { String metric = arguments.get("metric-name"); Map<String, String> tags = ArgumentParser.parseMap(arguments.getOptional("tags", ""), "<tags>"); String start = arguments.getOptional("start", ""); String end = arguments.getOptional("end", ""); MetricQueryResult result = client.query( tags, ImmutableList.of(metric), ImmutableList.<String>of(), start.isEmpty() ? null : start, end.isEmpty() ? null : end); output.printf("Start time: %d\n", result.getStartTime()); output.printf("End time: %d\n", result.getEndTime()); for (MetricQueryResult.TimeSeries series : result.getSeries()) { output.println(); output.printf("Series: %s\n", series.getMetricName()); if (!series.getGrouping().isEmpty()) { output.printf("Grouping: %s\n", Joiner.on(",").withKeyValueSeparator("=").join(series.getGrouping())); } Table table = Table.builder() .setHeader("timestamp", "value") .setRows(ImmutableList.copyOf(series.getData()), new RowMaker<MetricQueryResult.TimeValue>() { @Override public List<?> makeRow(MetricQueryResult.TimeValue object) { return Lists.newArrayList(object.getTime(), object.getValue()); } }).build(); cliConfig.getTableRenderer().render(cliConfig, output, table); } }