com.google.monitoring.v3.Point Java Examples
The following examples show how to use
com.google.monitoring.v3.Point.
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: BigQueryRunner.java From java-docs-samples with Apache License 2.0 | 6 votes |
private TimeSeries prepareMetric(MetricDescriptor requiredMetric, long metricValue) { TimeInterval interval = TimeInterval.newBuilder() .setEndTime(Timestamps.fromMillis(System.currentTimeMillis())) .build(); TypedValue value = TypedValue.newBuilder().setInt64Value(metricValue).build(); Point point = Point.newBuilder().setInterval(interval).setValue(value).build(); List<Point> pointList = Lists.newArrayList(); pointList.add(point); Metric metric = Metric.newBuilder().setType(requiredMetric.getName()).build(); return TimeSeries.newBuilder().setMetric(metric).addAllPoints(pointList).build(); }
Example #2
Source File: StackdriverExportUtils.java From opencensus-java with Apache License 2.0 | 5 votes |
static List<TimeSeries> createTimeSeriesList( io.opencensus.metrics.export.Metric metric, MonitoredResource monitoredResource, String domain, String projectId, Map<LabelKey, LabelValue> constantLabels) { List<TimeSeries> timeSeriesList = Lists.newArrayList(); io.opencensus.metrics.export.MetricDescriptor metricDescriptor = metric.getMetricDescriptor(); if (!projectId.equals(cachedProjectIdForExemplar)) { cachedProjectIdForExemplar = projectId; } // Shared fields for all TimeSeries generated from the same Metric TimeSeries.Builder shared = TimeSeries.newBuilder(); shared.setMetricKind(createMetricKind(metricDescriptor.getType())); shared.setResource(monitoredResource); shared.setValueType(createValueType(metricDescriptor.getType())); // Each entry in timeSeriesList will be converted into an independent TimeSeries object for (io.opencensus.metrics.export.TimeSeries timeSeries : metric.getTimeSeriesList()) { // TODO(mayurkale): Consider using setPoints instead of builder clone and addPoints. TimeSeries.Builder builder = shared.clone(); builder.setMetric( createMetric(metricDescriptor, timeSeries.getLabelValues(), domain, constantLabels)); io.opencensus.common.Timestamp startTimeStamp = timeSeries.getStartTimestamp(); for (io.opencensus.metrics.export.Point point : timeSeries.getPoints()) { builder.addPoints(createPoint(point, startTimeStamp)); } timeSeriesList.add(builder.build()); } return timeSeriesList; }
Example #3
Source File: StackdriverExportUtils.java From opencensus-java with Apache License 2.0 | 5 votes |
@VisibleForTesting static Point createPoint( io.opencensus.metrics.export.Point point, @javax.annotation.Nullable io.opencensus.common.Timestamp startTimestamp) { TimeInterval.Builder timeIntervalBuilder = TimeInterval.newBuilder(); timeIntervalBuilder.setEndTime(convertTimestamp(point.getTimestamp())); if (startTimestamp != null) { timeIntervalBuilder.setStartTime(convertTimestamp(startTimestamp)); } Point.Builder builder = Point.newBuilder(); builder.setInterval(timeIntervalBuilder.build()); builder.setValue(createTypedValue(point.getValue())); return builder.build(); }
Example #4
Source File: StackdriverExportUtils.java From opencensus-java with Apache License 2.0 | 5 votes |
private static void createTimeSeries( List<LabelValue> labelValues, Value value, io.opencensus.common.Timestamp pointTimestamp, @javax.annotation.Nullable io.opencensus.common.Timestamp timeSeriesTimestamp, List<io.opencensus.metrics.export.TimeSeries> timeSeriesList) { timeSeriesList.add( io.opencensus.metrics.export.TimeSeries.createWithOnePoint( labelValues, io.opencensus.metrics.export.Point.create(value, pointTimestamp), timeSeriesTimestamp)); }
Example #5
Source File: TimeSeriesSummary.java From java-docs-samples with Apache License 2.0 | 5 votes |
Point getMostRecentPoint(TimeSeries timeSeries) { Point max = Collections.max( timeSeries.getPointsList(), Comparator.comparingLong(p -> p.getInterval().getEndTime().getSeconds())); mostRecentRunTime = max.getInterval().getEndTime(); return max; }
Example #6
Source File: TimeSeriesSummary.java From java-docs-samples with Apache License 2.0 | 5 votes |
private StringTimeSeriesSummary(TimeSeries timeSeries) { super(timeSeries); Point max = getMostRecentPoint(timeSeries); if (max == null) { return; } mostRecentValue = max.getValue().getStringValue(); values = Lists.newArrayList( Collections2.transform( timeSeries.getPointsList(), point -> point.getValue().getStringValue())); }
Example #7
Source File: TimeSeriesSummary.java From java-docs-samples with Apache License 2.0 | 5 votes |
private Int64TimeSeriesSummary(TimeSeries timeSeries) { super(timeSeries); Point max = getMostRecentPoint(timeSeries); if (max == null) { return; } mostRecentValue = max.getValue().getInt64Value(); values = Lists.newArrayList( Collections2.transform( timeSeries.getPointsList(), point -> point.getValue().getInt64Value())); }
Example #8
Source File: MonitoringServlet.java From tomcat-runtime with Apache License 2.0 | 5 votes |
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { JsonNode body = objectMapper.readTree(req.getReader()); String name = body.path("name").asText(); long token = body.path("token").asLong(); logger.info("Creating Time series with name " + name + " and token " + token); MetricServiceClient serviceClient = MetricServiceClient.create(); TimeSeries timeSeries = TimeSeries.newBuilder() .addPoints(Point.newBuilder() .setValue(TypedValue.newBuilder().setInt64Value(token)) .setInterval(TimeInterval.newBuilder() .setEndTime(Timestamp.now().toProto()))) .setMetric(Metric.newBuilder().setType(name)) .build(); serviceClient.createTimeSeries( ProjectName.create(ServiceOptions.getDefaultProjectId()), Collections.singletonList(timeSeries)); resp.setContentType("text/plain"); resp.getWriter().println("OK"); }
Example #9
Source File: MetricScaler.java From cloud-bigtable-examples with Apache License 2.0 | 5 votes |
/** * Retrieves the latest value for the metric defined by {@link #CPU_METRIC}. * @return * @throws IOException */ Point getLatestValue() throws IOException { // [START get_bigtable_cpu] Timestamp now = timeXMinutesAgo(0); Timestamp fiveMinutesAgo = timeXMinutesAgo(5); TimeInterval interval = TimeInterval.newBuilder().setStartTime(fiveMinutesAgo).setEndTime(now).build(); String filter = "metric.type=\"" + CPU_METRIC + "\""; ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(projectName, filter, interval, TimeSeriesView.FULL); return response.getPage().getValues().iterator().next().getPointsList().get(0); // [END get_bigtable_cpu] }
Example #10
Source File: MonitoringService.java From healthcare-dicom-dicomweb-adapter with Apache License 2.0 | 4 votes |
private void flush() { HashMap<IMonitoringEvent, Long> flushEvents = null; synchronized (aggregateEvents) { flushEvents = new HashMap<>(aggregateEvents); aggregateEvents.clear(); } try { Timestamp flushTime = Timestamps.fromMillis(System.currentTimeMillis()); List<TimeSeries> timeSeriesList = new ArrayList<>(); for (IMonitoringEvent event : monitoredEvents) { TimeInterval interval = TimeInterval.newBuilder() .setEndTime(flushTime) .build(); TypedValue value = TypedValue.newBuilder() .setInt64Value(flushEvents.getOrDefault(event, 0L)) .build(); Point point = Point.newBuilder() .setInterval(interval) .setValue(value) .build(); List<Point> pointList = new ArrayList<>(); pointList.add(point); Metric metric = Metric.newBuilder() .setType(event.getMetricName()) .build(); TimeSeries timeSeries = TimeSeries.newBuilder() .setMetric(metric) .setMetricKind(MetricDescriptor.MetricKind.GAUGE) .setResource(monitoredResource) .addAllPoints(pointList) .build(); timeSeriesList.add(timeSeries); } ProjectName projectName = ProjectName.of(projectId); CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder() .setName(projectName.toString()) .addAllTimeSeries(timeSeriesList) .build(); client.createTimeSeries(request); log.trace("Flushed {} non-zero time series", flushEvents.size()); if (flushEvents.size() > 0) { log.info("Flushed: {}", flushEvents); } } catch (Throwable e) { log.error("Failed to flush time series", e); } }
Example #11
Source File: StackdriverExportUtils.java From opencensus-java with Apache License 2.0 | 4 votes |
@VisibleForTesting static List<io.opencensus.metrics.export.Metric> convertSummaryMetric( io.opencensus.metrics.export.Metric summaryMetric) { List<io.opencensus.metrics.export.Metric> metricsList = Lists.newArrayList(); final List<io.opencensus.metrics.export.TimeSeries> percentileTimeSeries = new ArrayList<>(); final List<io.opencensus.metrics.export.TimeSeries> summaryCountTimeSeries = new ArrayList<>(); final List<io.opencensus.metrics.export.TimeSeries> summarySumTimeSeries = new ArrayList<>(); for (final io.opencensus.metrics.export.TimeSeries timeSeries : summaryMetric.getTimeSeriesList()) { final List<LabelValue> labelValuesWithPercentile = new ArrayList<>(timeSeries.getLabelValues()); final io.opencensus.common.Timestamp timeSeriesTimestamp = timeSeries.getStartTimestamp(); for (io.opencensus.metrics.export.Point point : timeSeries.getPoints()) { final io.opencensus.common.Timestamp pointTimestamp = point.getTimestamp(); point .getValue() .match( Functions.<Void>returnNull(), Functions.<Void>returnNull(), Functions.<Void>returnNull(), new Function<Summary, Void>() { @Override public Void apply(Summary summary) { Long count = summary.getCount(); if (count != null) { createTimeSeries( timeSeries.getLabelValues(), Value.longValue(count), pointTimestamp, timeSeriesTimestamp, summaryCountTimeSeries); } Double sum = summary.getSum(); if (sum != null) { createTimeSeries( timeSeries.getLabelValues(), Value.doubleValue(sum), pointTimestamp, timeSeriesTimestamp, summarySumTimeSeries); } Snapshot snapshot = summary.getSnapshot(); for (ValueAtPercentile valueAtPercentile : snapshot.getValueAtPercentiles()) { labelValuesWithPercentile.add( LabelValue.create(valueAtPercentile.getPercentile() + "")); createTimeSeries( labelValuesWithPercentile, Value.doubleValue(valueAtPercentile.getValue()), pointTimestamp, null, percentileTimeSeries); labelValuesWithPercentile.remove(labelValuesWithPercentile.size() - 1); } return null; } }, Functions.<Void>returnNull()); } } // Metric for summary->count. if (summaryCountTimeSeries.size() > 0) { addMetric( metricsList, io.opencensus.metrics.export.MetricDescriptor.create( summaryMetric.getMetricDescriptor().getName() + SUMMARY_SUFFIX_COUNT, summaryMetric.getMetricDescriptor().getDescription(), "1", Type.CUMULATIVE_INT64, summaryMetric.getMetricDescriptor().getLabelKeys()), summaryCountTimeSeries); } // Metric for summary->sum. if (summarySumTimeSeries.size() > 0) { addMetric( metricsList, io.opencensus.metrics.export.MetricDescriptor.create( summaryMetric.getMetricDescriptor().getName() + SUMMARY_SUFFIX_SUM, summaryMetric.getMetricDescriptor().getDescription(), summaryMetric.getMetricDescriptor().getUnit(), Type.CUMULATIVE_DOUBLE, summaryMetric.getMetricDescriptor().getLabelKeys()), summarySumTimeSeries); } // Metric for summary->snapshot->percentiles. List<LabelKey> labelKeys = new ArrayList<>(summaryMetric.getMetricDescriptor().getLabelKeys()); labelKeys.add(PERCENTILE_LABEL_KEY); addMetric( metricsList, io.opencensus.metrics.export.MetricDescriptor.create( summaryMetric.getMetricDescriptor().getName() + SNAPSHOT_SUFFIX_PERCENTILE, summaryMetric.getMetricDescriptor().getDescription(), summaryMetric.getMetricDescriptor().getUnit(), Type.GAUGE_DOUBLE, labelKeys), percentileTimeSeries); return metricsList; }
Example #12
Source File: Snippets.java From java-docs-samples with Apache License 2.0 | 4 votes |
/** * Demonstrates writing a time series value for the metric type * 'custom.google.apis.com/my_metric'. * * <p>This method assumes `my_metric` descriptor has already been created as a DOUBLE value_type * and GAUGE metric kind. If the metric descriptor doesn't exist, it will be auto-created. */ // CHECKSTYLE OFF: VariableDeclarationUsageDistance void writeTimeSeries() throws IOException { // [START monitoring_write_timeseries] String projectId = System.getProperty("projectId"); // Instantiates a client MetricServiceClient metricServiceClient = MetricServiceClient.create(); // Prepares an individual data point TimeInterval interval = TimeInterval.newBuilder() .setEndTime(Timestamps.fromMillis(System.currentTimeMillis())) .build(); TypedValue value = TypedValue.newBuilder().setDoubleValue(123.45).build(); Point point = Point.newBuilder().setInterval(interval).setValue(value).build(); List<Point> pointList = new ArrayList<>(); pointList.add(point); ProjectName name = ProjectName.of(projectId); // Prepares the metric descriptor Map<String, String> metricLabels = new HashMap<>(); Metric metric = Metric.newBuilder() .setType("custom.googleapis.com/my_metric") .putAllLabels(metricLabels) .build(); // Prepares the monitored resource descriptor Map<String, String> resourceLabels = new HashMap<>(); resourceLabels.put("instance_id", "1234567890123456789"); resourceLabels.put("zone", "us-central1-f"); MonitoredResource resource = MonitoredResource.newBuilder().setType("gce_instance").putAllLabels(resourceLabels).build(); // Prepares the time series request TimeSeries timeSeries = TimeSeries.newBuilder() .setMetric(metric) .setResource(resource) .addAllPoints(pointList) .build(); List<TimeSeries> timeSeriesList = new ArrayList<>(); timeSeriesList.add(timeSeries); CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder() .setName(name.toString()) .addAllTimeSeries(timeSeriesList) .build(); // Writes time series data metricServiceClient.createTimeSeries(request); System.out.println("Done writing time series value."); // [END monitoring_write_timeseries] }
Example #13
Source File: QuickstartSample.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void main(String... args) throws Exception { // Your Google Cloud Platform project ID String projectId = System.getProperty("projectId"); if (projectId == null) { System.err.println("Usage: QuickstartSample -DprojectId=YOUR_PROJECT_ID"); return; } // Instantiates a client MetricServiceClient metricServiceClient = MetricServiceClient.create(); // Prepares an individual data point TimeInterval interval = TimeInterval.newBuilder() .setEndTime(Timestamps.fromMillis(System.currentTimeMillis())) .build(); TypedValue value = TypedValue.newBuilder().setDoubleValue(3.14).build(); Point point = Point.newBuilder().setInterval(interval).setValue(value).build(); List<Point> pointList = new ArrayList<>(); pointList.add(point); ProjectName name = ProjectName.of(projectId); // Prepares the metric descriptor Map<String, String> metricLabels = new HashMap<String, String>(); metricLabels.put("store_id", "Pittsburg"); Metric metric = Metric.newBuilder() .setType("custom.googleapis.com/my_metric") .putAllLabels(metricLabels) .build(); // Prepares the monitored resource descriptor Map<String, String> resourceLabels = new HashMap<String, String>(); resourceLabels.put("instance_id", "1234567890123456789"); resourceLabels.put("zone", "us-central1-f"); MonitoredResource resource = MonitoredResource.newBuilder().setType("gce_instance").putAllLabels(resourceLabels).build(); // Prepares the time series request TimeSeries timeSeries = TimeSeries.newBuilder() .setMetric(metric) .setResource(resource) .addAllPoints(pointList) .build(); List<TimeSeries> timeSeriesList = new ArrayList<>(); timeSeriesList.add(timeSeries); CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder() .setName(name.toString()) .addAllTimeSeries(timeSeriesList) .build(); // Writes time series data metricServiceClient.createTimeSeries(request); System.out.printf("Done writing time series data.%n"); metricServiceClient.close(); }