Search in sources :

Example 1 with Metric

use of com.google.api.Metric 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 2 with Metric

use of com.google.api.Metric 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 3 with Metric

use of com.google.api.Metric in project java-docs-samples by GoogleCloudPlatform.

the class BigQueryRunner method createMetricsIfNeeded.

// [END bigquery_logging_list_time_series]
// [START bigquery_logging_list_and_create_metrics]
private void createMetricsIfNeeded() {
    // If all required metrics already exist, no need to make service calls.
    if (REQUIRED_METRICS.stream().map(MetricDescriptor::getDisplayName).allMatch(existingMetrics::contains)) {
        return;
    }
    ListMetricDescriptorsRequest listMetricsRequest = ListMetricDescriptorsRequest.newBuilder().setName(projectName).setFilter(CUSTOM_METRIC_FILTER).build();
    ListMetricDescriptorsPagedResponse listMetricsResponse = client.listMetricDescriptors(listMetricsRequest);
    for (MetricDescriptor existingMetric : listMetricsResponse.iterateAll()) {
        existingMetrics.add(existingMetric.getDisplayName());
    }
    REQUIRED_METRICS.stream().filter(metric -> !existingMetrics.contains(metric.getDisplayName())).forEach(this::createMetric);
}
Also used : ListTimeSeriesPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse) ListMetricDescriptorsRequest(com.google.monitoring.v3.ListMetricDescriptorsRequest) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest) MetricDescriptor(com.google.api.MetricDescriptor) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) BigQuery(com.google.cloud.bigquery.BigQuery) ArrayList(java.util.ArrayList) BigQueryOptions(com.google.cloud.bigquery.BigQueryOptions) Lists(com.google.common.collect.Lists) ServiceOptions(com.google.cloud.ServiceOptions) TypedValue(com.google.monitoring.v3.TypedValue) Job(com.google.cloud.bigquery.Job) TableResult(com.google.cloud.bigquery.TableResult) Point(com.google.monitoring.v3.Point) PrintStream(java.io.PrintStream) ImmutableSet(com.google.common.collect.ImmutableSet) JobInfo(com.google.cloud.bigquery.JobInfo) ListMetricDescriptorsPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListMetricDescriptorsPagedResponse) Metric(com.google.api.Metric) TimeSeries(com.google.monitoring.v3.TimeSeries) Set(java.util.Set) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) Timestamps(com.google.protobuf.util.Timestamps) IOException(java.io.IOException) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) JobId(com.google.cloud.bigquery.JobId) CreateMetricDescriptorRequest(com.google.monitoring.v3.CreateMetricDescriptorRequest) TimeInterval(com.google.monitoring.v3.TimeInterval) MetricDescriptor(com.google.api.MetricDescriptor) ListMetricDescriptorsRequest(com.google.monitoring.v3.ListMetricDescriptorsRequest) ListMetricDescriptorsPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListMetricDescriptorsPagedResponse)

Example 4 with Metric

use of com.google.api.Metric in project java-docs-samples by GoogleCloudPlatform.

the class BigQueryRunner method prepareMetric.

// Returns a metric time series with a single int64 data point.
private TimeSeries prepareMetric(MetricDescriptor requiredMetric, long metricValue) {
    TimeInterval interval = TimeInterval.newBuilder().setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build();
    TypedValue value = TypedValue.newBuilder().setInt64Value(metricValue).build();
    Point point = Point.newBuilder().setInterval(interval).setValue(value).build();
    List<Point> pointList = Lists.newArrayList();
    pointList.add(point);
    Metric metric = Metric.newBuilder().setType(requiredMetric.getName()).build();
    return TimeSeries.newBuilder().setMetric(metric).addAllPoints(pointList).build();
}
Also used : TimeInterval(com.google.monitoring.v3.TimeInterval) Metric(com.google.api.Metric) Point(com.google.monitoring.v3.Point) TypedValue(com.google.monitoring.v3.TypedValue)

Aggregations

Metric (com.google.api.Metric)4 Point (com.google.monitoring.v3.Point)4 TimeInterval (com.google.monitoring.v3.TimeInterval)4 TypedValue (com.google.monitoring.v3.TypedValue)4 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)3 CreateTimeSeriesRequest (com.google.monitoring.v3.CreateTimeSeriesRequest)3 TimeSeries (com.google.monitoring.v3.TimeSeries)3 ArrayList (java.util.ArrayList)3 MonitoredResource (com.google.api.MonitoredResource)2 ProjectName (com.google.monitoring.v3.ProjectName)2 HashMap (java.util.HashMap)2 MetricDescriptor (com.google.api.MetricDescriptor)1 ServiceOptions (com.google.cloud.ServiceOptions)1 BigQuery (com.google.cloud.bigquery.BigQuery)1 BigQueryOptions (com.google.cloud.bigquery.BigQueryOptions)1 Job (com.google.cloud.bigquery.Job)1 JobId (com.google.cloud.bigquery.JobId)1 JobInfo (com.google.cloud.bigquery.JobInfo)1 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)1 TableResult (com.google.cloud.bigquery.TableResult)1