Search in sources :

Example 1 with MetricServiceClient

use of com.google.cloud.monitoring.v3.MetricServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class Snippets method listMonitoredResources.

/**
 * Gets all monitored resource descriptors.
 */
void listMonitoredResources() throws IOException {
    // [START monitoring_list_resources]
    // Your Google Cloud Platform project ID
    String projectId = System.getProperty("projectId");
    final MetricServiceClient client = MetricServiceClient.create();
    ProjectName name = ProjectName.of(projectId);
    ListMonitoredResourceDescriptorsRequest request = ListMonitoredResourceDescriptorsRequest.newBuilder().setName(name.toString()).build();
    System.out.println("Listing monitored resource descriptors: ");
    ListMonitoredResourceDescriptorsPagedResponse response = client.listMonitoredResourceDescriptors(request);
    for (MonitoredResourceDescriptor d : response.iterateAll()) {
        System.out.println(d.getType());
    }
// [END monitoring_list_resources]
}
Also used : MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) ProjectName(com.google.monitoring.v3.ProjectName) ListMonitoredResourceDescriptorsRequest(com.google.monitoring.v3.ListMonitoredResourceDescriptorsRequest) MonitoredResourceDescriptor(com.google.api.MonitoredResourceDescriptor) ListMonitoredResourceDescriptorsPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListMonitoredResourceDescriptorsPagedResponse)

Example 2 with MetricServiceClient

use of com.google.cloud.monitoring.v3.MetricServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class Snippets method describeMonitoredResources.

/**
 * Gets full information for a monitored resource.
 *
 * @param type The resource type
 */
void describeMonitoredResources(String type) throws IOException {
    // [START monitoring_get_descriptor]
    // Your Google Cloud Platform project ID
    String projectId = System.getProperty("projectId");
    final MetricServiceClient client = MetricServiceClient.create();
    MonitoredResourceDescriptorName name = MonitoredResourceDescriptorName.of(projectId, type);
    MonitoredResourceDescriptor response = client.getMonitoredResourceDescriptor(name);
    System.out.println("Printing monitored resource descriptor: ");
    System.out.println(response);
// [END monitoring_get_descriptor]
}
Also used : MonitoredResourceDescriptorName(com.google.monitoring.v3.MonitoredResourceDescriptorName) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) MonitoredResourceDescriptor(com.google.api.MonitoredResourceDescriptor)

Example 3 with MetricServiceClient

use of com.google.cloud.monitoring.v3.MetricServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class Snippets method deleteMetricDescriptor.

/**
 * Delete a metric descriptor.
 *
 * @param name Name of metric descriptor to delete
 */
void deleteMetricDescriptor(String name) throws IOException {
    // [START monitoring_delete_metric]
    String projectId = System.getProperty("projectId");
    final MetricServiceClient client = MetricServiceClient.create();
    MetricDescriptorName metricName = MetricDescriptorName.of(projectId, name);
    client.deleteMetricDescriptor(metricName);
    System.out.println("Deleted descriptor " + name);
// [END monitoring_delete_metric]
}
Also used : MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) MetricDescriptorName(com.google.monitoring.v3.MetricDescriptorName)

Example 4 with MetricServiceClient

use of com.google.cloud.monitoring.v3.MetricServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class Snippets method listTimeSeriesAggregrate.

/**
 * Demonstrates listing time series and aggregating them.
 */
void listTimeSeriesAggregrate() throws IOException {
    // [START monitoring_read_timeseries_align]
    MetricServiceClient metricServiceClient = MetricServiceClient.create();
    String projectId = System.getProperty("projectId");
    ProjectName name = 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();
    Aggregation aggregation = Aggregation.newBuilder().setAlignmentPeriod(Duration.newBuilder().setSeconds(600).build()).setPerSeriesAligner(Aggregation.Aligner.ALIGN_MEAN).build();
    ListTimeSeriesRequest.Builder requestBuilder = ListTimeSeriesRequest.newBuilder().setName(name.toString()).setFilter("metric.type=\"compute.googleapis.com/instance/cpu/utilization\"").setInterval(interval).setAggregation(aggregation);
    ListTimeSeriesRequest request = requestBuilder.build();
    ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(request);
    System.out.println("Got timeseries: ");
    for (TimeSeries ts : response.iterateAll()) {
        System.out.println(ts);
    }
// [END monitoring_read_timeseries_align]
}
Also used : Aggregation(com.google.monitoring.v3.Aggregation) 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) ListTimeSeriesPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest)

Example 5 with MetricServiceClient

use of com.google.cloud.monitoring.v3.MetricServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class Snippets method createMetricDescriptor.

/**
 * Creates a metric descriptor.
 * <p>
 * See: https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors/create
 *
 * @param type The metric type
 */
void createMetricDescriptor(String type) throws IOException {
    // [START monitoring_create_metric]
    // Your Google Cloud Platform project ID
    String projectId = System.getProperty("projectId");
    String metricType = CUSTOM_METRIC_DOMAIN + "/" + type;
    final MetricServiceClient client = MetricServiceClient.create();
    ProjectName name = ProjectName.of(projectId);
    MetricDescriptor descriptor = MetricDescriptor.newBuilder().setType(metricType).setDescription("This is a simple example of a custom metric.").setMetricKind(MetricDescriptor.MetricKind.GAUGE).setValueType(MetricDescriptor.ValueType.DOUBLE).build();
    CreateMetricDescriptorRequest request = CreateMetricDescriptorRequest.newBuilder().setName(name.toString()).setMetricDescriptor(descriptor).build();
    client.createMetricDescriptor(request);
// [END monitoring_create_metric]
}
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)15 ProjectName (com.google.monitoring.v3.ProjectName)9 TimeInterval (com.google.monitoring.v3.TimeInterval)6 TimeSeries (com.google.monitoring.v3.TimeSeries)6 ListTimeSeriesPagedResponse (com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse)4 ListTimeSeriesRequest (com.google.monitoring.v3.ListTimeSeriesRequest)4 CreateTimeSeriesRequest (com.google.monitoring.v3.CreateTimeSeriesRequest)3 Metric (com.google.api.Metric)2 MetricDescriptor (com.google.api.MetricDescriptor)2 MonitoredResource (com.google.api.MonitoredResource)2 MonitoredResourceDescriptor (com.google.api.MonitoredResourceDescriptor)2 Aggregation (com.google.monitoring.v3.Aggregation)2 CreateMetricDescriptorRequest (com.google.monitoring.v3.CreateMetricDescriptorRequest)2 ListMetricDescriptorsRequest (com.google.monitoring.v3.ListMetricDescriptorsRequest)2 Point (com.google.monitoring.v3.Point)2 TypedValue (com.google.monitoring.v3.TypedValue)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ListMetricDescriptorsPagedResponse (com.google.cloud.monitoring.v3.MetricServiceClient.ListMetricDescriptorsPagedResponse)1