use of com.google.monitoring.v3.CreateTimeSeriesRequest in project google-cloud-java by GoogleCloudPlatform.
the class MetricServiceClientTest method createTimeSeriesTest.
@Test
@SuppressWarnings("all")
public void createTimeSeriesTest() {
Empty expectedResponse = Empty.newBuilder().build();
mockMetricService.addResponse(expectedResponse);
ProjectName name = ProjectName.create("[PROJECT]");
List<TimeSeries> timeSeries = new ArrayList<>();
client.createTimeSeries(name, timeSeries);
List<GeneratedMessageV3> actualRequests = mockMetricService.getRequests();
Assert.assertEquals(1, actualRequests.size());
CreateTimeSeriesRequest actualRequest = (CreateTimeSeriesRequest) actualRequests.get(0);
Assert.assertEquals(name, actualRequest.getNameAsProjectName());
Assert.assertEquals(timeSeries, actualRequest.getTimeSeriesList());
}
use of com.google.monitoring.v3.CreateTimeSeriesRequest in project google-cloud-java by GoogleCloudPlatform.
the class MetricServiceClient method createTimeSeries.
// AUTO-GENERATED DOCUMENTATION AND METHOD
/**
* Creates or adds data to one or more time series. The response is empty if all time series in
* the request were written. If any time series could not be written, a corresponding failure
* message is included in the error response.
*
* <p>Sample code:
*
* <pre><code>
* try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) {
* ProjectName name = ProjectName.create("[PROJECT]");
* List<TimeSeries> timeSeries = new ArrayList<>();
* metricServiceClient.createTimeSeries(name, timeSeries);
* }
* </code></pre>
*
* @param name The project on which to execute the request. The format is
* `"projects/{project_id_or_number}"`.
* @param timeSeries The new data to be added to a list of time series. Adds at most one data
* point to each of several time series. The new data point must be more recent than any other
* point in its time series. Each `TimeSeries` value must fully specify a unique time series
* by supplying all label values for the metric and the monitored resource.
* @throws com.google.api.gax.grpc.ApiException if the remote call fails
*/
public final void createTimeSeries(ProjectName name, List<TimeSeries> timeSeries) {
CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder().setNameWithProjectName(name).addAllTimeSeries(timeSeries).build();
createTimeSeries(request);
}
use of com.google.monitoring.v3.CreateTimeSeriesRequest in project java-docs-samples by GoogleCloudPlatform.
the class Snippets method writeTimeSeries.
/**
* 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]
}
use of com.google.monitoring.v3.CreateTimeSeriesRequest in project java-docs-samples by GoogleCloudPlatform.
the class QuickstartSample method main.
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(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<String, String>();
metricLabels.put("store_id", "Pittsburg");
Metric metric = Metric.newBuilder().setType("custom.googleapis.com/stores/daily_sales").putAllLabels(metricLabels).build();
// Prepares the monitored resource descriptor
Map<String, String> resourceLabels = new HashMap<String, String>();
resourceLabels.put("project_id", projectId);
MonitoredResource resource = MonitoredResource.newBuilder().setType("global").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();
}
use of com.google.monitoring.v3.CreateTimeSeriesRequest in project java-docs-samples by GoogleCloudPlatform.
the class BigQueryRunner method runQuery.
public void runQuery() throws InterruptedException {
// [START bigquery_logging_query]
QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder("SELECT " + "CONCAT('https://stackoverflow.com/questions/', CAST(id as STRING)) as url, " + "view_count " + "FROM `bigquery-public-data.stackoverflow.posts_questions` " + "WHERE tags like '%google-bigquery%' " + "ORDER BY favorite_count DESC LIMIT 10").setUseLegacySql(false).build();
List<TimeSeries> timeSeriesList = new ArrayList<>();
long queryStartTime = System.currentTimeMillis();
// Create a job ID so that we can safely retry.
JobId jobId = JobId.of(UUID.randomUUID().toString());
Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());
// Wait for the query to complete.
queryJob = queryJob.waitFor();
// Check for errors
if (queryJob == null) {
throw new RuntimeException("Job no longer exists");
} else if (queryJob.getStatus().getError() != null) {
// errors, not just the latest one.
throw new RuntimeException(queryJob.getStatus().getError().toString());
}
// [END bigquery_logging_query]
// [START bigquery_logging_log_metrics]
// Log the result metrics.
TableResult result = queryJob.getQueryResults();
long queryEndTime = System.currentTimeMillis();
// Add query duration metric.
timeSeriesList.add(prepareMetric(QUERY_DURATION_METRIC, queryEndTime - queryStartTime));
// Add rows returned metric.
timeSeriesList.add(prepareMetric(ROWS_RETURNED_METRIC, result.getTotalRows()));
// Prepares the time series request
CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder().setName(projectName).addAllTimeSeries(timeSeriesList).build();
createMetricsIfNeeded();
client.createTimeSeries(request);
os.println("Done writing metrics.");
// [END bigquery_logging_log_metrics]
mostRecentRunResult = result;
}
Aggregations