Search in sources :

Example 21 with TimeSeries

use of com.google.api.ads.admanager.axis.v202202.TimeSeries in project java-docs-samples by GoogleCloudPlatform.

the class BigQueryRunner method getTimeSeriesValues.

// [START bigquery_logging_list_time_series]
public List<TimeSeriesSummary> getTimeSeriesValues() {
    List<TimeSeriesSummary> summaries = Lists.newArrayList();
    createMetricsIfNeeded();
    for (MetricDescriptor metric : REQUIRED_METRICS) {
        ListTimeSeriesRequest listTimeSeriesRequest = ListTimeSeriesRequest.newBuilder().setName(projectName).setFilter(String.format("metric.type = \"%s\"", metric.getType())).setInterval(TimeInterval.newBuilder().setStartTime(Timestamps.subtract(Timestamps.fromMillis(System.currentTimeMillis()), com.google.protobuf.Duration.newBuilder().setSeconds(// 30 days ago
        60L * 60L * 24L * 30L).build())).setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build()).build();
        try {
            ListTimeSeriesPagedResponse listTimeSeriesResponse = client.listTimeSeries(listTimeSeriesRequest);
            ArrayList<TimeSeries> timeSeries = Lists.newArrayList(listTimeSeriesResponse.iterateAll());
            summaries.addAll(timeSeries.stream().map(TimeSeriesSummary::fromTimeSeries).collect(Collectors.toList()));
        } catch (RuntimeException ex) {
            os.println("MetricDescriptors not yet synced. Please try again in a moment.");
        }
    }
    return summaries;
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) TimeSeries(com.google.monitoring.v3.TimeSeries) ListTimeSeriesPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest)

Example 22 with TimeSeries

use of com.google.api.ads.admanager.axis.v202202.TimeSeries 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)

Example 23 with TimeSeries

use of com.google.api.ads.admanager.axis.v202202.TimeSeries in project instrumentation-java by census-instrumentation.

the class StackdriverExportUtilsTest method createTimeSeriesList_Cumulative.

@Test
public void createTimeSeriesList_Cumulative() {
    List<TimeSeries> timeSeriesList = StackdriverExportUtils.createTimeSeriesList(METRIC, DEFAULT_RESOURCE, CUSTOM_OPENCENSUS_DOMAIN, PROJECT_ID, DEFAULT_CONSTANT_LABELS);
    assertThat(timeSeriesList).hasSize(1);
    TimeSeries expectedTimeSeries = TimeSeries.newBuilder().setMetricKind(MetricKind.CUMULATIVE).setValueType(MetricDescriptor.ValueType.DOUBLE).setMetric(StackdriverExportUtils.createMetric(METRIC_DESCRIPTOR, LABEL_VALUE, CUSTOM_OPENCENSUS_DOMAIN, DEFAULT_CONSTANT_LABELS)).setResource(MonitoredResource.newBuilder().setType("global")).addPoints(StackdriverExportUtils.createPoint(POINT, TIMESTAMP_2)).build();
    assertThat(timeSeriesList).containsExactly(expectedTimeSeries);
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) Test(org.junit.Test)

Example 24 with TimeSeries

use of com.google.api.ads.admanager.axis.v202202.TimeSeries in project instrumentation-java by census-instrumentation.

the class StackdriverExportUtilsTest method createTimeSeriesList_withCustomMonitoredResource.

@Test
public void createTimeSeriesList_withCustomMonitoredResource() {
    MonitoredResource resource = MonitoredResource.newBuilder().setType("global").putLabels("key", "value").build();
    List<TimeSeries> timeSeriesList = StackdriverExportUtils.createTimeSeriesList(METRIC, resource, CUSTOM_OPENCENSUS_DOMAIN, PROJECT_ID, DEFAULT_CONSTANT_LABELS);
    assertThat(timeSeriesList).containsExactly(TimeSeries.newBuilder().setMetricKind(MetricKind.CUMULATIVE).setValueType(MetricDescriptor.ValueType.DOUBLE).setMetric(StackdriverExportUtils.createMetric(METRIC_DESCRIPTOR, LABEL_VALUE, CUSTOM_OPENCENSUS_DOMAIN, DEFAULT_CONSTANT_LABELS)).setResource(resource).addPoints(StackdriverExportUtils.createPoint(POINT, TIMESTAMP_2)).build());
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) MonitoredResource(com.google.api.MonitoredResource) Test(org.junit.Test)

Example 25 with TimeSeries

use of com.google.api.ads.admanager.axis.v202202.TimeSeries in project instrumentation-java by census-instrumentation.

the class CreateTimeSeriesExporter method export.

@Override
public void export(Collection<Metric> metrics) {
    List<TimeSeries> timeSeriesList = new ArrayList<>(metrics.size());
    for (Metric metric : metrics) {
        timeSeriesList.addAll(StackdriverExportUtils.createTimeSeriesList(metric, monitoredResource, domain, projectName.getProject(), constantLabels));
    }
    Span span = tracer.getCurrentSpan();
    for (List<TimeSeries> batchedTimeSeries : Lists.partition(timeSeriesList, MAX_BATCH_EXPORT_SIZE)) {
        span.addAnnotation("Export Stackdriver TimeSeries.");
        try {
            CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder().setName(projectName.toString()).addAllTimeSeries(batchedTimeSeries).build();
            metricServiceClient.createTimeSeries(request);
            span.addAnnotation("Finish exporting TimeSeries.");
        } catch (ApiException e) {
            logger.log(Level.WARNING, "ApiException thrown when exporting TimeSeries.", e);
            span.setStatus(Status.CanonicalCode.valueOf(e.getStatusCode().getCode().name()).toStatus().withDescription("ApiException thrown when exporting TimeSeries: " + StackdriverExportUtils.exceptionMessage(e)));
        } catch (Throwable e) {
            logger.log(Level.WARNING, "Exception thrown when exporting TimeSeries.", e);
            span.setStatus(Status.UNKNOWN.withDescription("Exception thrown when exporting TimeSeries: " + StackdriverExportUtils.exceptionMessage(e)));
        }
    }
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) ArrayList(java.util.ArrayList) Metric(io.opencensus.metrics.export.Metric) Span(io.opencensus.trace.Span) ApiException(com.google.api.gax.rpc.ApiException)

Aggregations

TimeSeries (com.google.monitoring.v3.TimeSeries)17 TimeSeries (org.spf4j.tsdb2.TimeSeries)11 ProjectName (com.google.monitoring.v3.ProjectName)9 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)6 ListTimeSeriesRequest (com.google.monitoring.v3.ListTimeSeriesRequest)6 TimeInterval (com.google.monitoring.v3.TimeInterval)6 ListTimeSeriesPagedResponse (com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse)5 CreateTimeSeriesRequest (com.google.monitoring.v3.CreateTimeSeriesRequest)5 HashMap (java.util.HashMap)4 MonitoredResource (com.google.api.MonitoredResource)3 Metric (com.google.api.Metric)2 Aggregation (com.google.monitoring.v3.Aggregation)2 Point (com.google.monitoring.v3.Point)2 TypedValue (com.google.monitoring.v3.TypedValue)2 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2