Search in sources :

Example 31 with TimeSeries

use of com.google.api.ads.admanager.axis.v202205.TimeSeries in project workbench by all-of-us.

the class WorkspaceAdminServiceImpl method getCloudStorageTraffic.

@Override
public CloudStorageTraffic getCloudStorageTraffic(String workspaceNamespace) {
    CloudStorageTraffic response = new CloudStorageTraffic().receivedBytes(new ArrayList<>());
    String googleProject = getWorkspaceByNamespaceOrThrow(workspaceNamespace).getGoogleProject();
    for (TimeSeries timeSeries : cloudMonitoringService.getCloudStorageReceivedBytes(googleProject, TRAILING_TIME_TO_QUERY)) {
        for (Point point : timeSeries.getPointsList()) {
            response.addReceivedBytesItem(new TimeSeriesPoint().timestamp(Timestamps.toMillis(point.getInterval().getEndTime())).value(point.getValue().getDoubleValue()));
        }
    }
    response.getReceivedBytes().sort(Comparator.comparing(TimeSeriesPoint::getTimestamp));
    return response;
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) TimeSeriesPoint(org.pmiops.workbench.model.TimeSeriesPoint) CloudStorageTraffic(org.pmiops.workbench.model.CloudStorageTraffic) Point(com.google.monitoring.v3.Point) TimeSeriesPoint(org.pmiops.workbench.model.TimeSeriesPoint)

Example 32 with TimeSeries

use of com.google.api.ads.admanager.axis.v202205.TimeSeries in project workbench by all-of-us.

the class WorkspaceAdminServiceTest method getCloudStorageTraffic_sortsPointsByTimestamp.

@Test
public void getCloudStorageTraffic_sortsPointsByTimestamp() {
    TimeSeries timeSeries = TimeSeries.newBuilder().addPoints(Point.newBuilder().setInterval(TimeInterval.newBuilder().setEndTime(Timestamps.fromMillis(2000))).setValue(TypedValue.newBuilder().setDoubleValue(1234))).addPoints(Point.newBuilder().setInterval(TimeInterval.newBuilder().setEndTime(Timestamps.fromMillis(1000))).setValue(TypedValue.newBuilder().setDoubleValue(1234))).build();
    when(mockCloudMonitoringService.getCloudStorageReceivedBytes(anyString(), any(Duration.class))).thenReturn(Collections.singletonList(timeSeries));
    final CloudStorageTraffic cloudStorageTraffic = workspaceAdminService.getCloudStorageTraffic(WORKSPACE_NAMESPACE);
    assertThat(cloudStorageTraffic.getReceivedBytes().stream().map(TimeSeriesPoint::getTimestamp).collect(Collectors.toList())).containsExactly(1000L, 2000L);
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) Duration(java.time.Duration) CloudStorageTraffic(org.pmiops.workbench.model.CloudStorageTraffic) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) Test(org.junit.jupiter.api.Test)

Example 33 with TimeSeries

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

the class BigQueryRunner method getTimeSeriesValues.

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 34 with TimeSeries

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

the class BigQueryRunner method runQuery.

public void runQuery() throws InterruptedException {
    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());
    }
    // 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.");
    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 35 with TimeSeries

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

Aggregations

TimeSeries (com.google.monitoring.v3.TimeSeries)36 Test (org.junit.Test)21 ArrayList (java.util.ArrayList)18 ProjectName (com.google.monitoring.v3.ProjectName)17 TimeInterval (com.google.monitoring.v3.TimeInterval)14 ListTimeSeriesPagedResponse (com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse)11 CreateTimeSeriesRequest (com.google.monitoring.v3.CreateTimeSeriesRequest)11 TimeSeries (org.spf4j.tsdb2.TimeSeries)11 ListTimeSeriesRequest (com.google.monitoring.v3.ListTimeSeriesRequest)10 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)8 ListTimeSeriesResponse (com.google.monitoring.v3.ListTimeSeriesResponse)8 AbstractMessage (com.google.protobuf.AbstractMessage)8 Point (com.google.monitoring.v3.Point)6 MonitoredResource (com.google.api.MonitoredResource)5 Empty (com.google.protobuf.Empty)5 StatusRuntimeException (io.grpc.StatusRuntimeException)5 HashMap (java.util.HashMap)5 Metric (com.google.api.Metric)4 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)4 DateTime (org.joda.time.DateTime)4