Search in sources :

Example 36 with AlertPolicy

use of com.google.monitoring.v3.AlertPolicy 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)33 Test (org.junit.Test)16 AbstractMessage (com.google.protobuf.AbstractMessage)11 AlertPolicyServiceClient (com.google.cloud.monitoring.v3.AlertPolicyServiceClient)9 ListAlertPoliciesPagedResponse (com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse)8 ArrayList (java.util.ArrayList)7 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)5 ListAlertPoliciesRequest (com.google.monitoring.v3.ListAlertPoliciesRequest)5 NotificationChannel (com.google.monitoring.v3.NotificationChannel)5 StatusRuntimeException (io.grpc.StatusRuntimeException)5 JsonObject (com.google.gson.JsonObject)4 CreateAlertPolicyRequest (com.google.monitoring.v3.CreateAlertPolicyRequest)4 ListAlertPoliciesResponse (com.google.monitoring.v3.ListAlertPoliciesResponse)4 ProjectName (com.google.monitoring.v3.ProjectName)4 Aggregation (com.google.monitoring.v3.Aggregation)3 FolderName (com.google.monitoring.v3.FolderName)3 OrganizationName (com.google.monitoring.v3.OrganizationName)3 UpdateAlertPolicyRequest (com.google.monitoring.v3.UpdateAlertPolicyRequest)3 Duration (com.google.protobuf.Duration)3 JsonArray (com.google.gson.JsonArray)2