forked from oracle/oci-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitoringMetricSummarizeExample.java
More file actions
105 lines (92 loc) · 4.57 KB
/
MonitoringMetricSummarizeExample.java
File metadata and controls
105 lines (92 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
*/
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.ConfigFileReader.ConfigFile;
import com.oracle.bmc.Region;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.monitoring.MonitoringClient;
import com.oracle.bmc.monitoring.model.SummarizeMetricsDataDetails;
import com.oracle.bmc.monitoring.requests.SummarizeMetricsDataRequest;
import com.oracle.bmc.monitoring.responses.SummarizeMetricsDataResponse;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* This class demonstrates how to use the Monitoring api in the Java SDK. This will cover:
*
* <ul>
* <li>Retrieving a summary of metric data</li>
* </ul>
*
* This class makes the following assumptions:
* <ul>
* <li>
* The configuration file used by service clients will be sourced from the default
* location (~/.oci/config) and the DEFAULT profile will be used
* </li>
* <li>Region where the example will be run is: us-phoenix-1</li>
* <li>
* The user has the appropriate permissions to summarizeMetricsData in the compartment specified.
* </li>
* <ul>
*/
public class MonitoringMetricSummarizeExample {
private static final String CONFIG_LOCATION = "~/.oci/config";
private static final String CONFIG_PROFILE_DEFAULT = "DEFAULT";
private static final Date NOW = new Date();
private static final Date START = new Date(NOW.getTime() - TimeUnit.HOURS.toMillis(6));;
private static final Date UNTIL = NOW;
private static void printUsageAndThrowException(String message) {
throw new IllegalArgumentException(
message
+ "Usage:\n"
+ "MonitoringMetricSummarizeExample <compartment> <namespace> <query> [oci-profile-name]");
}
/**
* The entry point for the example.
*/
public static void main(String[] args) throws Exception {
if (args.length < 3) {
printUsageAndThrowException("Insufficient args\n");
}
final String compartment = args[0];
final String namespace = args[1];
final String query = args[2];
final String profile = args.length > 3 ? args[4] : CONFIG_PROFILE_DEFAULT;
try (MonitoringClient monitoringClient = getMonitoringClient(profile)) {
final SummarizeMetricsDataRequest request =
SummarizeMetricsDataRequest.builder()
.compartmentId(compartment)
.summarizeMetricsDataDetails(
SummarizeMetricsDataDetails.builder()
.namespace(namespace)
.query(query)
.startTime(START)
.endTime(UNTIL)
.build())
.build();
System.out.printf(
"Request constructed:\n%s\n\n", request.getSummarizeMetricsDataDetails());
System.out.println("Sending...");
final SummarizeMetricsDataResponse response =
monitoringClient.summarizeMetricsData(request);
System.out.printf(
"\n\nReceived response [opc-request-id: %s]\n", response.getOpcRequestId());
System.out.printf("%s\n", response.getItems().toString());
}
}
private static MonitoringClient getMonitoringClient(String profile) throws IOException {
// Configuring the AuthenticationDetailsProvider. It's assuming there is a default OCI config file
// "~/.oci/config", and a profile in that config with the name "DEFAULT". Make changes to the following
// line if needed and use ConfigFileReader.parse(CONFIG_LOCATION, CONFIG_PROFILE);
final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parseDefault();
final AuthenticationDetailsProvider provider =
new ConfigFileAuthenticationDetailsProvider(configFile);
final MonitoringClient monitoringClient = new MonitoringClient(provider);
monitoringClient.setRegion(Region.US_PHOENIX_1);
return monitoringClient;
}
}