Skip to content

Commit febd8ce

Browse files
committed
OCI Monitoring Post Metric example
1 parent be59a2e commit febd8ce

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* 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.
4+
*/
5+
6+
/* @param args Arguments to provide to the example. The following arguments are expected:
7+
* <ul>
8+
* <li>The first argument is the OCID of the compartment.</li>
9+
* </ul>
10+
* Refer https://docs.cloud.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/MetricData/PostMetricData for more details.
11+
*/
12+
13+
const mt = require("oci-monitoring");
14+
const common = require("oci-common");
15+
16+
const configurationFilePath = "~/.oci/config";
17+
const configProfile = "DEFAULT";
18+
19+
const provider = new common.ConfigFileAuthenticationDetailsProvider(
20+
configurationFilePath,
21+
configProfile
22+
);
23+
24+
const args = process.argv.slice(1);
25+
console.log(args);
26+
if (args.length !== 2) {
27+
console.error(
28+
"Unexpected number of arguments received. Usage : monitoring-alarm.ts <MetricCompartment>"
29+
);
30+
process.exit(-1);
31+
}
32+
33+
const compartmentId = args[1];
34+
35+
const monitoringClient = new mt.MonitoringClient({
36+
authenticationDetailsProvider: provider
37+
});
38+
monitoringClient.region = common.Region.US_PHOENIX_1;
39+
monitoringClient.endpoint = "https://telemetry-ingestion.us-phoenix-1.oraclecloud.com";
40+
41+
(async () => {
42+
try {
43+
44+
var datenow = new Date();
45+
var dateutc = new Date(datenow.toUTCString());
46+
/* The timestamp datapoint format used is defined by RFC3339.
47+
https://docs.cloud.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/datatypes/Datapoint
48+
*/
49+
50+
const MetricDataDetails = [{
51+
"namespace": "mushopnamespace",
52+
"resourceGroup": "mushop-rg",
53+
"compartmentId": compartmentId,
54+
"name": "mushop-401-http-error",
55+
"dimensions": {
56+
"appName": "Mushop",
57+
"podName": "mushop-storefront"
58+
},
59+
"metadata": {
60+
"unit": "count",
61+
"displayName": "MuShop Authorization errors"
62+
},
63+
"datapoints": [{
64+
"timestamp": dateutc,
65+
"value": 43,
66+
"count": 43
67+
}
68+
]
69+
}]
70+
71+
const PostMetricDataDetails = {
72+
"metricData" : MetricDataDetails
73+
}
74+
75+
const PostMetricDataRequest = {
76+
"postMetricDataDetails": PostMetricDataDetails
77+
};
78+
79+
const post_response = await monitoringClient.postMetricData(PostMetricDataRequest);
80+
//console.log("Retrieved :" + response.postMetricDataResponseDetails.failedMetricsCount);
81+
82+
console.log("Successfully posted custom metric with name: %s to namespace: %s in region: %s",MetricDataDetails[0].name,MetricDataDetails[0].namespace,common.Region.US_PHOENIX_1._regionId )
83+
84+
} catch (error) {
85+
console.log(" Not able to run post metric monitoring example. Error: " + error);
86+
}
87+
})();

0 commit comments

Comments
 (0)