Search in sources :

Example 6 with AlertPolicyName

use of com.google.monitoring.v3.AlertPolicyName in project java-monitoring by googleapis.

the class AlertPolicyServiceClientTest method deleteAlertPolicyTest.

@Test
public void deleteAlertPolicyTest() throws Exception {
    Empty expectedResponse = Empty.newBuilder().build();
    mockAlertPolicyService.addResponse(expectedResponse);
    AlertPolicyName name = AlertPolicyName.ofProjectAlertPolicyName("[PROJECT]", "[ALERT_POLICY]");
    client.deleteAlertPolicy(name);
    List<AbstractMessage> actualRequests = mockAlertPolicyService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    DeleteAlertPolicyRequest actualRequest = ((DeleteAlertPolicyRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : Empty(com.google.protobuf.Empty) AbstractMessage(com.google.protobuf.AbstractMessage) AlertPolicyName(com.google.monitoring.v3.AlertPolicyName) DeleteAlertPolicyRequest(com.google.monitoring.v3.DeleteAlertPolicyRequest) Test(org.junit.Test)

Example 7 with AlertPolicyName

use of com.google.monitoring.v3.AlertPolicyName in project java-monitoring by googleapis.

the class GetAlertPolicy method getAlertPolicy.

public static void getAlertPolicy(String alertPolicyName) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
        // Gets a single alerting policy
        AlertPolicy alertPolicy = alertPolicyServiceClient.getAlertPolicy(alertPolicyName);
        System.out.format("alert policy retrieved successfully:%s", alertPolicy.getName());
    }
}
Also used : AlertPolicy(com.google.monitoring.v3.AlertPolicy) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient)

Example 8 with AlertPolicyName

use of com.google.monitoring.v3.AlertPolicyName in project java-monitoring by googleapis.

the class CreateAlertPolicy method createAlertPolicy.

public static void createAlertPolicy(String projectId, String alertPolicyName) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
        ProjectName name = ProjectName.of(projectId);
        String metricType = "compute.googleapis.com/instance/cpu/utilization";
        String resourceType = "gce_instance";
        // A Filter that identifies which time series should be compared with the threshold
        String metricFilter = "metric.type=" + '"' + metricType + '"' + " AND " + "resource.type=" + '"' + resourceType + '"';
        // Build Duration
        Duration aggregationDuration = Duration.newBuilder().setSeconds(60).build();
        // Build Aggregation
        Aggregation aggregation = Aggregation.newBuilder().setAlignmentPeriod(aggregationDuration).setCrossSeriesReducer(Aggregation.Reducer.REDUCE_MEAN).setPerSeriesAligner(Aggregation.Aligner.ALIGN_MAX).build();
        // Build MetricThreshold
        AlertPolicy.Condition.MetricThreshold metricThreshold = AlertPolicy.Condition.MetricThreshold.newBuilder().setComparison(ComparisonType.COMPARISON_GT).addAggregations(aggregation).setFilter(metricFilter).setDuration(aggregationDuration).build();
        // Construct Condition object
        AlertPolicy.Condition alertPolicyCondition = AlertPolicy.Condition.newBuilder().setDisplayName(alertPolicyName).setConditionThreshold(metricThreshold).build();
        // Build an alert policy
        AlertPolicy alertPolicy = AlertPolicy.newBuilder().setDisplayName(alertPolicyName).addConditions(alertPolicyCondition).setCombiner(AlertPolicy.ConditionCombinerType.AND).build();
        // Create an alert policy
        AlertPolicy actualAlertPolicy = alertPolicyServiceClient.createAlertPolicy(name, alertPolicy);
        System.out.format("alert policy created:%s", actualAlertPolicy.getName());
    }
}
Also used : Aggregation(com.google.monitoring.v3.Aggregation) ProjectName(com.google.monitoring.v3.ProjectName) AlertPolicy(com.google.monitoring.v3.AlertPolicy) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient) Duration(com.google.protobuf.Duration)

Aggregations

AlertPolicy (com.google.monitoring.v3.AlertPolicy)4 AlertPolicyName (com.google.monitoring.v3.AlertPolicyName)4 Test (org.junit.Test)4 AlertPolicyServiceClient (com.google.cloud.monitoring.v3.AlertPolicyServiceClient)3 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)2 Aggregation (com.google.monitoring.v3.Aggregation)2 DeleteAlertPolicyRequest (com.google.monitoring.v3.DeleteAlertPolicyRequest)2 AbstractMessage (com.google.protobuf.AbstractMessage)2 Duration (com.google.protobuf.Duration)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 GetAlertPolicyRequest (com.google.monitoring.v3.GetAlertPolicyRequest)1 ProjectName (com.google.monitoring.v3.ProjectName)1 UpdateAlertPolicyRequest (com.google.monitoring.v3.UpdateAlertPolicyRequest)1 Empty (com.google.protobuf.Empty)1 ArrayList (java.util.ArrayList)1