|
| 1 | +/* |
| 2 | + Copyright 2017, Google, Inc. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package com.example.monitoring; |
| 18 | + |
| 19 | +// [START monitoring_quickstart] |
| 20 | +// Imports the Google Cloud client library |
| 21 | +import com.google.cloud.monitoring.spi.v3.MetricServiceClient; |
| 22 | + |
| 23 | +import com.google.api.Metric; |
| 24 | +import com.google.api.MonitoredResource; |
| 25 | +import com.google.monitoring.v3.CreateTimeSeriesRequest; |
| 26 | +import com.google.monitoring.v3.Point; |
| 27 | +import com.google.monitoring.v3.ProjectName; |
| 28 | +import com.google.monitoring.v3.TimeInterval; |
| 29 | +import com.google.monitoring.v3.TimeSeries; |
| 30 | +import com.google.monitoring.v3.TypedValue; |
| 31 | +import com.google.protobuf.util.Timestamps; |
| 32 | + |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | + |
| 38 | +public class QuickstartSample { |
| 39 | + private static final String LEGACY_PROJECT_ENV_NAME = "GCLOUD_PROJECT"; |
| 40 | + private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT"; |
| 41 | + |
| 42 | + private static String getProjectId() { |
| 43 | + String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME)); |
| 44 | + if (projectId == null) { |
| 45 | + projectId = System.getProperty(LEGACY_PROJECT_ENV_NAME, System.getenv(LEGACY_PROJECT_ENV_NAME)); |
| 46 | + } |
| 47 | + return projectId; |
| 48 | + } |
| 49 | + |
| 50 | + public static void main(String... args) throws Exception { |
| 51 | + // Your Google Cloud Platform project ID |
| 52 | + String projectId = System.getProperty("projectId"); |
| 53 | + |
| 54 | + if (projectId == null) { |
| 55 | + System.err.println("Usage: QuickstartSample -DprojectId=YOUR_PROJECT_ID"); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // Instantiates a client |
| 60 | + MetricServiceClient metricServiceClient = MetricServiceClient.create(); |
| 61 | + |
| 62 | + // Prepares an individual data point |
| 63 | + TimeInterval interval = TimeInterval.newBuilder() |
| 64 | + .setEndTime(Timestamps.fromMillis(System.currentTimeMillis())) |
| 65 | + .build(); |
| 66 | + TypedValue value = TypedValue.newBuilder() |
| 67 | + .setDoubleValue(123.45) |
| 68 | + .build(); |
| 69 | + Point point = Point.newBuilder() |
| 70 | + .setInterval(interval) |
| 71 | + .setValue(value) |
| 72 | + .build(); |
| 73 | + |
| 74 | + List<Point> pointList = new ArrayList<>(); |
| 75 | + pointList.add(point); |
| 76 | + |
| 77 | + ProjectName name = ProjectName.create(projectId); |
| 78 | + |
| 79 | + // Prepares the metric descriptor |
| 80 | + Map<String, String> metricLabels = new HashMap<String, String>(); |
| 81 | + metricLabels.put("store_id", "Pittsburg"); |
| 82 | + Metric metric = Metric.newBuilder() |
| 83 | + .setType("custom.googleapis.com/stores/daily_sales") |
| 84 | + .putAllLabels(metricLabels) |
| 85 | + .build(); |
| 86 | + |
| 87 | + // Prepares the monitored resource descriptor |
| 88 | + Map<String, String> resourceLabels = new HashMap<String, String>(); |
| 89 | + resourceLabels.put("project_id", projectId); |
| 90 | + MonitoredResource resource = MonitoredResource.newBuilder() |
| 91 | + .setType("global") |
| 92 | + .putAllLabels(resourceLabels) |
| 93 | + .build(); |
| 94 | + |
| 95 | + // Prepares the time series request |
| 96 | + TimeSeries timeSeries = TimeSeries.newBuilder() |
| 97 | + .setMetric(metric) |
| 98 | + .setResource(resource) |
| 99 | + .addAllPoints(pointList) |
| 100 | + .build(); |
| 101 | + List<TimeSeries> timeSeriesList = new ArrayList<>(); |
| 102 | + timeSeriesList.add(timeSeries); |
| 103 | + |
| 104 | + CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder() |
| 105 | + .setNameWithProjectName(name) |
| 106 | + .addAllTimeSeries(timeSeriesList) |
| 107 | + .build(); |
| 108 | + |
| 109 | + // Writes time series data |
| 110 | + metricServiceClient.createTimeSeries(request); |
| 111 | + |
| 112 | + System.out.printf("Done writing time series data.%n"); |
| 113 | + |
| 114 | + metricServiceClient.close(); |
| 115 | + } |
| 116 | +} |
| 117 | +// [END monitoring_quickstart] |
0 commit comments