Search in sources :

Example 21 with MetricServiceClient

use of com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient 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(3.14).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/my_metric").putAllLabels(metricLabels).build();
    // Prepares the monitored resource descriptor
    Map<String, String> resourceLabels = new HashMap<String, String>();
    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.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 22 with MetricServiceClient

use of com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient in project java-monitoring by googleapis.

the class ListMetricDescriptor method listMetricDescriptors.

public static void listMetricDescriptors(String projectId) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) {
        ProjectName name = ProjectName.of(projectId);
        // Construct ListMetricDescriptors request
        ListMetricDescriptorsRequest request = ListMetricDescriptorsRequest.newBuilder().setName(name.toString()).build();
        // Send the request to list the metric descriptor
        MetricServiceClient.ListMetricDescriptorsPagedResponse response = metricServiceClient.listMetricDescriptors(request);
        // Process the response
        response.iterateAll().forEach(metricDescriptor -> System.out.format("success! metric descriptor with name %s display name %s%n", metricDescriptor.getName(), metricDescriptor.getDisplayName()));
    }
}
Also used : MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) ProjectName(com.google.monitoring.v3.ProjectName) ListMetricDescriptorsRequest(com.google.monitoring.v3.ListMetricDescriptorsRequest)

Example 23 with MetricServiceClient

use of com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient in project java-monitoring by googleapis.

the class ListTimeSeries method listTimeSeries.

public static void listTimeSeries(String filter, String projectId) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) {
        ProjectName projectName = ProjectName.of(projectId);
        // Restrict time to last 20 minutes
        long startMillis = System.currentTimeMillis() - ((60 * 20) * 1000);
        TimeInterval interval = TimeInterval.newBuilder().setStartTime(Timestamps.fromMillis(startMillis)).setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build();
        // Prepares the list time series request
        ListTimeSeriesRequest request = ListTimeSeriesRequest.newBuilder().setName(projectName.toString()).setFilter(filter).setInterval(interval).build();
        // Send the request to list the time series
        MetricServiceClient.ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(request);
        // Process the response
        System.out.println("Got timeseries: ");
        response.iterateAll().forEach(timeSeries -> System.out.println(timeSeries));
    }
}
Also used : MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) TimeInterval(com.google.monitoring.v3.TimeInterval) ProjectName(com.google.monitoring.v3.ProjectName) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest)

Example 24 with MetricServiceClient

use of com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient in project java-monitoring by googleapis.

the class MonitoredResourcesList method listMonitoredResources.

public static void listMonitoredResources(String projectId) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) {
        ProjectName name = ProjectName.of(projectId);
        // Construct ListMonitoredResourceDescriptors request
        ListMonitoredResourceDescriptorsRequest request = ListMonitoredResourceDescriptorsRequest.newBuilder().setName(name.toString()).build();
        // Send the request to list the MonitoredResource descriptor
        MetricServiceClient.ListMonitoredResourceDescriptorsPagedResponse response = metricServiceClient.listMonitoredResourceDescriptors(request);
        // Process the response
        System.out.println("Listing monitored resource descriptors: ");
        response.iterateAll().forEach(metricDescriptor -> System.out.format("success! monitored resources type %s %n", metricDescriptor.getType()));
    }
}
Also used : MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) ProjectName(com.google.monitoring.v3.ProjectName) ListMonitoredResourceDescriptorsRequest(com.google.monitoring.v3.ListMonitoredResourceDescriptorsRequest)

Example 25 with MetricServiceClient

use of com.google.bigtable.repackaged.com.google.cloud.monitoring.v3.MetricServiceClient in project java-monitoring by googleapis.

the class CreateMetricDescriptor method createMetricDescriptor.

public static void createMetricDescriptor(String projectId, String metricType) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) {
        ProjectName name = ProjectName.of(projectId);
        // Define a metric type and its schema
        MetricDescriptor descriptor = MetricDescriptor.newBuilder().setType(metricType).addLabels(LabelDescriptor.newBuilder().setKey("store_id").setValueType(LabelDescriptor.ValueType.STRING)).setDescription("This is a simple example of a custom metric.").setMetricKind(MetricDescriptor.MetricKind.GAUGE).setValueType(MetricDescriptor.ValueType.DOUBLE).build();
        // Construct CreateMetricDescriptor request
        CreateMetricDescriptorRequest request = CreateMetricDescriptorRequest.newBuilder().setName(name.toString()).setMetricDescriptor(descriptor).build();
        // Send the request to create the metric descriptor
        MetricDescriptor metricDescriptor = metricServiceClient.createMetricDescriptor(request);
        // Process the response
        System.out.println("metric descriptor created successfully: " + metricDescriptor.getName());
    }
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) ProjectName(com.google.monitoring.v3.ProjectName) CreateMetricDescriptorRequest(com.google.monitoring.v3.CreateMetricDescriptorRequest)

Aggregations

MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)26 ProjectName (com.google.monitoring.v3.ProjectName)15 TimeInterval (com.google.monitoring.v3.TimeInterval)10 TimeSeries (com.google.monitoring.v3.TimeSeries)8 ListTimeSeriesRequest (com.google.monitoring.v3.ListTimeSeriesRequest)6 CreateTimeSeriesRequest (com.google.monitoring.v3.CreateTimeSeriesRequest)5 Metric (com.google.api.Metric)4 MetricDescriptor (com.google.api.MetricDescriptor)4 MonitoredResource (com.google.api.MonitoredResource)4 MonitoredResourceDescriptor (com.google.api.MonitoredResourceDescriptor)4 ListTimeSeriesPagedResponse (com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse)4 Point (com.google.monitoring.v3.Point)4 TypedValue (com.google.monitoring.v3.TypedValue)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 CreateMetricDescriptorRequest (com.google.monitoring.v3.CreateMetricDescriptorRequest)3 ListMetricDescriptorsRequest (com.google.monitoring.v3.ListMetricDescriptorsRequest)3 MonitoredResourceDescriptorName (com.google.monitoring.v3.MonitoredResourceDescriptorName)3 Test (org.junit.Test)3 Aggregation (com.google.monitoring.v3.Aggregation)2