Search in sources :

Example 1 with UpdateAlertPolicyRequest

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

the class AlertPolicyServiceClientTest method updateAlertPolicyTest.

@Test
public void updateAlertPolicyTest() throws Exception {
    AlertPolicy expectedResponse = AlertPolicy.newBuilder().setName(AlertPolicyName.ofProjectAlertPolicyName("[PROJECT]", "[ALERT_POLICY]").toString()).setDisplayName("displayName1714148973").setDocumentation(AlertPolicy.Documentation.newBuilder().build()).putAllUserLabels(new HashMap<String, String>()).addAllConditions(new ArrayList<AlertPolicy.Condition>()).setEnabled(BoolValue.newBuilder().build()).setValidity(Status.newBuilder().build()).addAllNotificationChannels(new ArrayList<String>()).setCreationRecord(MutationRecord.newBuilder().build()).setMutationRecord(MutationRecord.newBuilder().build()).setAlertStrategy(AlertPolicy.AlertStrategy.newBuilder().build()).build();
    mockAlertPolicyService.addResponse(expectedResponse);
    FieldMask updateMask = FieldMask.newBuilder().build();
    AlertPolicy alertPolicy = AlertPolicy.newBuilder().build();
    AlertPolicy actualResponse = client.updateAlertPolicy(updateMask, alertPolicy);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockAlertPolicyService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    UpdateAlertPolicyRequest actualRequest = ((UpdateAlertPolicyRequest) actualRequests.get(0));
    Assert.assertEquals(updateMask, actualRequest.getUpdateMask());
    Assert.assertEquals(alertPolicy, actualRequest.getAlertPolicy());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) AlertPolicy(com.google.monitoring.v3.AlertPolicy) UpdateAlertPolicyRequest(com.google.monitoring.v3.UpdateAlertPolicyRequest) ArrayList(java.util.ArrayList) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Example 2 with UpdateAlertPolicyRequest

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

the class UpdateAlertPolicy method updateAlertPolicy.

public static void updateAlertPolicy(String alertPolicyName, String newPolicyName) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
        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();
        // build Alert policy condition
        AlertPolicy.Condition alertPolicyCondition = AlertPolicy.Condition.newBuilder().setDisplayName(alertPolicyName).setConditionThreshold(metricThreshold).build();
        // create alert policy for update
        AlertPolicy alertPolicy = AlertPolicy.newBuilder().setName(alertPolicyName).setDisplayName(newPolicyName).addConditions(alertPolicyCondition).setCombiner(AlertPolicy.ConditionCombinerType.AND).build();
        // create update alert policy request
        UpdateAlertPolicyRequest updateAlertPolicyRequest = UpdateAlertPolicyRequest.newBuilder().setAlertPolicy(alertPolicy).build();
        // process update policy request
        AlertPolicy updatedAlertPolicy = alertPolicyServiceClient.updateAlertPolicy(updateAlertPolicyRequest);
        System.out.print("alert policy updated successfully:" + updatedAlertPolicy.getName());
    }
}
Also used : Aggregation(com.google.monitoring.v3.Aggregation) AlertPolicy(com.google.monitoring.v3.AlertPolicy) UpdateAlertPolicyRequest(com.google.monitoring.v3.UpdateAlertPolicyRequest) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient) Duration(com.google.protobuf.Duration)

Example 3 with UpdateAlertPolicyRequest

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

the class EnableDisableAlertPolicy method enableDisableAlertPolicy.

public static void enableDisableAlertPolicy(String alertPolicyId, boolean status, String displayName) throws ApiException, IOException {
    // once, and can be reused for multiple requests.
    try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
        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
        AlertPolicy.Condition condition = AlertPolicy.Condition.newBuilder().setDisplayName(displayName).setConditionThreshold(metricThreshold).build();
        // Create alert policy for update
        AlertPolicy alertPolicy = AlertPolicy.newBuilder().setName(alertPolicyId).setDisplayName(displayName).addConditions(condition).setCombiner(AlertPolicy.ConditionCombinerType.AND).setEnabled(BoolValue.newBuilder().setValue((status == true) ? Boolean.TRUE : Boolean.FALSE).build()).build();
        // Create update alert policy request
        UpdateAlertPolicyRequest updateAlertPolicyRequest = UpdateAlertPolicyRequest.newBuilder().setAlertPolicy(alertPolicy).build();
        // Process update policy request
        AlertPolicy updatedAlertPolicy = alertPolicyServiceClient.updateAlertPolicy(updateAlertPolicyRequest);
        System.out.format("alert policy enable disable status:%s%n", updatedAlertPolicy.getEnabled());
    }
}
Also used : Aggregation(com.google.monitoring.v3.Aggregation) AlertPolicy(com.google.monitoring.v3.AlertPolicy) UpdateAlertPolicyRequest(com.google.monitoring.v3.UpdateAlertPolicyRequest) AlertPolicyServiceClient(com.google.cloud.monitoring.v3.AlertPolicyServiceClient) Duration(com.google.protobuf.Duration)

Aggregations

AlertPolicy (com.google.monitoring.v3.AlertPolicy)3 UpdateAlertPolicyRequest (com.google.monitoring.v3.UpdateAlertPolicyRequest)3 AlertPolicyServiceClient (com.google.cloud.monitoring.v3.AlertPolicyServiceClient)2 Aggregation (com.google.monitoring.v3.Aggregation)2 Duration (com.google.protobuf.Duration)2 AbstractMessage (com.google.protobuf.AbstractMessage)1 FieldMask (com.google.protobuf.FieldMask)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1