Skip to content

Commit c7bdd9a

Browse files
committed
chore: samples migration from java-monitoring
1 parent fd996cc commit c7bdd9a

35 files changed

+2599
-79
lines changed

monitoring/v3/pom.xml

+66-74
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
12
<!--
23
Copyright 2018 Google Inc. All Rights Reserved.
34
@@ -13,80 +14,71 @@
1314
See the License for the specific language governing permissions and
1415
limitations under the License.
1516
-->
16-
<project>
17-
<modelVersion>4.0.0</modelVersion>
18-
<groupId>com.example.monitoring</groupId>
19-
<artifactId>monitoring-google-cloud-v3-samples</artifactId>
20-
<version>0.1-SNAPSHOT</version>
21-
<packaging>jar</packaging>
22-
23-
<!--
24-
The parent pom defines common style checks and testing strategies for our samples.
25-
Removing or replacing it should not affect the execution of the samples in anyway.
26-
-->
27-
<parent>
28-
<groupId>com.google.cloud.samples</groupId>
29-
<artifactId>shared-configuration</artifactId>
30-
<version>1.2.0</version>
31-
</parent>
32-
33-
<properties>
34-
<maven.compiler.target>1.8</maven.compiler.target>
35-
<maven.compiler.source>1.8</maven.compiler.source>
36-
</properties>
37-
38-
<dependencyManagement>
39-
<dependencies>
40-
<dependency>
41-
<groupId>com.google.cloud</groupId>
42-
<artifactId>libraries-bom</artifactId>
43-
<version>26.1.4</version>
44-
<type>pom</type>
45-
<scope>import</scope>
46-
</dependency>
47-
</dependencies>
48-
</dependencyManagement>
49-
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
<groupId>com.example.monitoring</groupId>
20+
<artifactId>monitoring-google-cloud-v3-samples</artifactId>
21+
<version>0.1-SNAPSHOT</version>
22+
<packaging>jar</packaging>
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not affect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.2.0</version>
31+
</parent>
32+
<properties>
33+
<maven.compiler.target>1.8</maven.compiler.target>
34+
<maven.compiler.source>1.8</maven.compiler.source>
35+
</properties>
36+
<!-- [START monitoring_install_with_bom] -->
37+
<dependencyManagement>
5038
<dependencies>
51-
<dependency>
52-
<groupId>com.google.cloud</groupId>
53-
<artifactId>google-cloud-monitoring</artifactId>
54-
</dependency>
55-
<dependency>
56-
<groupId>commons-cli</groupId>
57-
<artifactId>commons-cli</artifactId>
58-
<version>1.5.0</version>
59-
</dependency>
60-
<dependency>
61-
<groupId>com.google.code.gson</groupId>
62-
<artifactId>gson</artifactId>
63-
<version>2.8.9</version>
64-
</dependency>
65-
66-
<!-- FIXME: remove after client fixes depenency issue => BOM 9.0.0 -->
67-
<dependency>
68-
<groupId>com.google.auth</groupId>
69-
<artifactId>google-auth-library-credentials</artifactId>
70-
<version>1.12.1</version>
71-
</dependency>
72-
<dependency>
73-
<groupId>com.google.auth</groupId>
74-
<artifactId>google-auth-library-oauth2-http</artifactId>
75-
<version>1.8.1</version>
76-
</dependency>
77-
78-
<!-- Test dependencies -->
79-
<dependency>
80-
<groupId>junit</groupId>
81-
<artifactId>junit</artifactId>
82-
<version>4.13.2</version>
83-
<scope>test</scope>
84-
</dependency>
85-
<dependency>
86-
<groupId>com.google.truth</groupId>
87-
<artifactId>truth</artifactId>
88-
<version>1.1.3</version>
89-
<scope>test</scope>
90-
</dependency>
39+
<dependency>
40+
<groupId>com.google.cloud</groupId>
41+
<artifactId>libraries-bom</artifactId>
42+
<version>26.1.4</version>
43+
<type>pom</type>
44+
<scope>import</scope>
45+
</dependency>
9146
</dependencies>
47+
</dependencyManagement>
48+
<dependencies>
49+
<dependency>
50+
<groupId>com.google.cloud</groupId>
51+
<artifactId>google-cloud-monitoring</artifactId>
52+
</dependency>
53+
<!-- [START_EXCLUDE] -->
54+
<dependency>
55+
<groupId>commons-cli</groupId>
56+
<artifactId>commons-cli</artifactId>
57+
<version>1.5.0</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.google.code.gson</groupId>
61+
<artifactId>gson</artifactId>
62+
<version>2.8.9</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>com.google.protobuf</groupId>
66+
<artifactId>protobuf-java-util</artifactId>
67+
</dependency>
68+
<!-- Test dependencies -->
69+
<dependency>
70+
<groupId>junit</groupId>
71+
<artifactId>junit</artifactId>
72+
<version>4.13.2</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>com.google.truth</groupId>
77+
<artifactId>truth</artifactId>
78+
<version>1.1.3</version>
79+
<scope>test</scope>
80+
</dependency>
81+
<!-- [END_EXCLUDE] -->
82+
</dependencies>
83+
<!-- [END monitoring_install_with_bom] -->
9284
</project>

monitoring/v3/src/main/java/com/example/AlertSample.java renamed to monitoring/v3/src/main/java/com/example/monitoring/AlertSample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example;
17+
package com.example.monitoring;
1818

1919
import com.google.cloud.monitoring.v3.AlertPolicyServiceClient;
2020
import com.google.cloud.monitoring.v3.AlertPolicyServiceClient.ListAlertPoliciesPagedResponse;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.monitoring;
18+
19+
// [START monitoring_create_alert_policy]
20+
import com.google.api.gax.rpc.ApiException;
21+
import com.google.cloud.monitoring.v3.AlertPolicyServiceClient;
22+
import com.google.monitoring.v3.Aggregation;
23+
import com.google.monitoring.v3.AlertPolicy;
24+
import com.google.monitoring.v3.ComparisonType;
25+
import com.google.monitoring.v3.ProjectName;
26+
import com.google.protobuf.Duration;
27+
import java.io.IOException;
28+
29+
// Sample to create an alert policy
30+
public class CreateAlertPolicy {
31+
32+
public static void main(String[] args) throws ApiException, IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "your-project-id";
35+
String alertPolicyName = "your-policy-name";
36+
createAlertPolicy(projectId, alertPolicyName);
37+
}
38+
39+
public static void createAlertPolicy(String projectId, String alertPolicyName)
40+
throws ApiException, IOException {
41+
// Initialize client that will be used to send requests. This client only needs to be created
42+
// once, and can be reused for multiple requests.
43+
try (AlertPolicyServiceClient alertPolicyServiceClient = AlertPolicyServiceClient.create()) {
44+
ProjectName name = ProjectName.of(projectId);
45+
String metricType = "compute.googleapis.com/instance/cpu/utilization";
46+
String resourceType = "gce_instance";
47+
48+
// A Filter that identifies which time series should be compared with the threshold
49+
String metricFilter =
50+
"metric.type="
51+
+ '"'
52+
+ metricType
53+
+ '"'
54+
+ " AND "
55+
+ "resource.type="
56+
+ '"'
57+
+ resourceType
58+
+ '"';
59+
60+
// Build Duration
61+
Duration aggregationDuration = Duration.newBuilder().setSeconds(60).build();
62+
63+
// Build Aggregation
64+
Aggregation aggregation =
65+
Aggregation.newBuilder()
66+
.setAlignmentPeriod(aggregationDuration)
67+
.setCrossSeriesReducer(Aggregation.Reducer.REDUCE_MEAN)
68+
.setPerSeriesAligner(Aggregation.Aligner.ALIGN_MAX)
69+
.build();
70+
71+
// Build MetricThreshold
72+
AlertPolicy.Condition.MetricThreshold metricThreshold =
73+
AlertPolicy.Condition.MetricThreshold.newBuilder()
74+
.setComparison(ComparisonType.COMPARISON_GT)
75+
.addAggregations(aggregation)
76+
.setFilter(metricFilter)
77+
.setDuration(aggregationDuration)
78+
.build();
79+
80+
// Construct Condition object
81+
AlertPolicy.Condition alertPolicyCondition =
82+
AlertPolicy.Condition.newBuilder()
83+
.setDisplayName(alertPolicyName)
84+
.setConditionThreshold(metricThreshold)
85+
.build();
86+
87+
// Build an alert policy
88+
AlertPolicy alertPolicy =
89+
AlertPolicy.newBuilder()
90+
.setDisplayName(alertPolicyName)
91+
.addConditions(alertPolicyCondition)
92+
.setCombiner(AlertPolicy.ConditionCombinerType.AND)
93+
.build();
94+
95+
// Create an alert policy
96+
AlertPolicy actualAlertPolicy = alertPolicyServiceClient.createAlertPolicy(name, alertPolicy);
97+
System.out.format("alert policy created:%s", actualAlertPolicy.getName());
98+
}
99+
}
100+
}
101+
// [END monitoring_create_alert_policy]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.monitoring;
18+
19+
// [START monitoring_create_metric]
20+
import com.google.api.LabelDescriptor;
21+
import com.google.api.MetricDescriptor;
22+
import com.google.api.gax.rpc.ApiException;
23+
import com.google.cloud.monitoring.v3.MetricServiceClient;
24+
import com.google.monitoring.v3.CreateMetricDescriptorRequest;
25+
import com.google.monitoring.v3.ProjectName;
26+
import java.io.IOException;
27+
28+
// Sample to create metric descriptor
29+
public class CreateMetricDescriptor {
30+
31+
public static void main(String[] args) throws ApiException, IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "your-project-id";
34+
String type = "metric-descriptor-type";
35+
String metricType = "custom.googleapis.com/" + type;
36+
createMetricDescriptor(projectId, metricType);
37+
}
38+
39+
public static void createMetricDescriptor(String projectId, String metricType)
40+
throws ApiException, IOException {
41+
// Initialize client that will be used to send requests. This client only needs to be created
42+
// once, and can be reused for multiple requests.
43+
try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) {
44+
ProjectName name = ProjectName.of(projectId);
45+
46+
// Define a metric type and its schema
47+
MetricDescriptor descriptor =
48+
MetricDescriptor.newBuilder()
49+
.setType(metricType)
50+
.addLabels(
51+
LabelDescriptor.newBuilder()
52+
.setKey("store_id")
53+
.setValueType(LabelDescriptor.ValueType.STRING))
54+
.setDescription("This is a simple example of a custom metric.")
55+
.setMetricKind(MetricDescriptor.MetricKind.GAUGE)
56+
.setValueType(MetricDescriptor.ValueType.DOUBLE)
57+
.build();
58+
59+
// Construct CreateMetricDescriptor request
60+
CreateMetricDescriptorRequest request =
61+
CreateMetricDescriptorRequest.newBuilder()
62+
.setName(name.toString())
63+
.setMetricDescriptor(descriptor)
64+
.build();
65+
66+
// Send the request to create the metric descriptor
67+
MetricDescriptor metricDescriptor = metricServiceClient.createMetricDescriptor(request);
68+
69+
// Process the response
70+
System.out.println("metric descriptor created successfully: " + metricDescriptor.getName());
71+
}
72+
}
73+
}
74+
// [END monitoring_create_metric]

0 commit comments

Comments
 (0)