Search in sources :

Example 1 with CreateTimeSeriesRequest

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());
}
Also used : Empty(com.google.protobuf.Empty) TimeSeries(com.google.monitoring.v3.TimeSeries) ProjectName(com.google.monitoring.v3.ProjectName) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) ArrayList(java.util.ArrayList) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 2 with CreateTimeSeriesRequest

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&lt;TimeSeries&gt; timeSeries = new ArrayList&lt;&gt;();
   *   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);
}
Also used : CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest)

Example 3 with CreateTimeSeriesRequest

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]
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) TimeInterval(com.google.monitoring.v3.TimeInterval) ProjectName(com.google.monitoring.v3.ProjectName) HashMap(java.util.HashMap) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) ArrayList(java.util.ArrayList) MonitoredResource(com.google.api.MonitoredResource) Point(com.google.monitoring.v3.Point) Metric(com.google.api.Metric) TypedValue(com.google.monitoring.v3.TypedValue)

Example 4 with CreateTimeSeriesRequest

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();
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) TimeInterval(com.google.monitoring.v3.TimeInterval) ProjectName(com.google.monitoring.v3.ProjectName) HashMap(java.util.HashMap) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) ArrayList(java.util.ArrayList) MonitoredResource(com.google.api.MonitoredResource) Point(com.google.monitoring.v3.Point) Metric(com.google.api.Metric) TypedValue(com.google.monitoring.v3.TypedValue)

Example 5 with CreateTimeSeriesRequest

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;
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) TableResult(com.google.cloud.bigquery.TableResult) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) ArrayList(java.util.ArrayList) Job(com.google.cloud.bigquery.Job) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) JobId(com.google.cloud.bigquery.JobId)

Aggregations

CreateTimeSeriesRequest (com.google.monitoring.v3.CreateTimeSeriesRequest)7 TimeSeries (com.google.monitoring.v3.TimeSeries)5 ArrayList (java.util.ArrayList)5 ProjectName (com.google.monitoring.v3.ProjectName)3 Metric (com.google.api.Metric)2 MonitoredResource (com.google.api.MonitoredResource)2 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)2 Point (com.google.monitoring.v3.Point)2 TimeInterval (com.google.monitoring.v3.TimeInterval)2 TypedValue (com.google.monitoring.v3.TypedValue)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ApiException (com.google.api.gax.rpc.ApiException)1 Job (com.google.cloud.bigquery.Job)1 JobId (com.google.cloud.bigquery.JobId)1 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)1 TableResult (com.google.cloud.bigquery.TableResult)1 Empty (com.google.protobuf.Empty)1 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)1 Metric (io.opencensus.metrics.export.Metric)1