Skip to content

Commit 0da532c

Browse files
authored
Merge pull request #28 from naikvenu/master
OCI Monitoring Post Metric example
2 parents bc6ab63 + 666739f commit 0da532c

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
* This example uses phoenix metric endpoint https://telemetry-ingestion.us-phoenix-1.oraclecloud.com
12+
* we can change this to a different region (refer https://docs.cloud.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/).
13+
*/
14+
15+
const mt = require("oci-monitoring");
16+
const common = require("oci-common");
17+
18+
const configurationFilePath = "~/.oci/config";
19+
const configProfile = "DEFAULT";
20+
21+
const provider = new common.ConfigFileAuthenticationDetailsProvider(
22+
configurationFilePath,
23+
configProfile
24+
);
25+
26+
const args = process.argv.slice(1);
27+
console.log(args);
28+
if (args.length !== 2) {
29+
console.error(
30+
"Unexpected number of arguments received. Usage : monitoring-alarm.ts <MetricCompartment>"
31+
);
32+
process.exit(-1);
33+
}
34+
35+
const compartmentId = args[1];
36+
37+
const monitoringClient = new mt.MonitoringClient({
38+
authenticationDetailsProvider: provider
39+
});
40+
monitoringClient.region = common.Region.US_PHOENIX_1;
41+
monitoringClient.endpoint = "https://telemetry-ingestion.us-phoenix-1.oraclecloud.com";
42+
43+
(async () => {
44+
try {
45+
46+
var datenow = new Date();
47+
var dateutc = new Date(datenow.toUTCString());
48+
/* The timestamp datapoint format used is defined by RFC3339.
49+
https://docs.cloud.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/datatypes/Datapoint
50+
*/
51+
52+
const MetricDataDetails = [{
53+
"namespace": "<your_namespace_here>",
54+
"resourceGroup": "<your_resourcegroup_here>",
55+
"compartmentId": compartmentId,
56+
"name": "<your_name_of_the_metric_here>",
57+
"dimensions": {
58+
"appName": "<your_dimensions>",
59+
"podName": "<your_dimensions>"
60+
},
61+
"metadata": {
62+
"unit": "count",
63+
"displayName": "<your_display_name>"
64+
},
65+
"datapoints": [{
66+
"timestamp": dateutc,
67+
"value": "<your_datapoint_value>",
68+
"count": "<your_datapoint_count>"
69+
}
70+
]
71+
}]
72+
73+
const PostMetricDataDetails = {
74+
"metricData" : MetricDataDetails
75+
}
76+
77+
const PostMetricDataRequest = {
78+
"postMetricDataDetails": PostMetricDataDetails
79+
};
80+
81+
const post_response = await monitoringClient.postMetricData(PostMetricDataRequest);
82+
//console.log("Retrieved :" + response.postMetricDataResponseDetails.failedMetricsCount);
83+
84+
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 )
85+
86+
} catch (error) {
87+
console.log(" Not able to run post metric monitoring example. Error: " + error);
88+
}
89+
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
* This example uses phoenix metric endpoint https://telemetry-ingestion.us-phoenix-1.oraclecloud.com
12+
* we can change this to a different region (refer https://docs.cloud.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/).
13+
*/
14+
15+
import mt = require("oci-monitoring");
16+
import common = require("oci-common");
17+
import { MetricDataDetails } from "oci-monitoring/lib/model";
18+
19+
const configurationFilePath = "~/.oci/config";
20+
const configProfile = "DEFAULT";
21+
22+
const provider: common.ConfigFileAuthenticationDetailsProvider = new common.ConfigFileAuthenticationDetailsProvider(
23+
configurationFilePath,
24+
configProfile
25+
);
26+
27+
const args = process.argv.slice(1);
28+
console.log(args);
29+
if (args.length !== 2) {
30+
console.error(
31+
"Unexpected number of arguments received. Usage : monitoring-alarm.ts <MetricCompartment>"
32+
);
33+
process.exit(-1);
34+
}
35+
36+
const compartmentId: string = args[1];
37+
38+
const monitoringClient: mt.MonitoringClient = new mt.MonitoringClient({
39+
authenticationDetailsProvider: provider
40+
});
41+
monitoringClient.region = common.Region.US_PHOENIX_1;
42+
monitoringClient.endpoint = "https://telemetry-ingestion.us-phoenix-1.oraclecloud.com";
43+
44+
(async () => {
45+
try {
46+
47+
var datenow = new Date();
48+
var dateutc = new Date(datenow.toUTCString());
49+
/* The timestamp datapoint format used is defined by RFC3339.
50+
https://docs.cloud.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/datatypes/Datapoint
51+
*/
52+
53+
const MetricDataDetails: Array<mt.models.MetricDataDetails> = [{
54+
namespace: "<your_namespace_here>",
55+
resourceGroup: "<your_resourcegroup_here>",
56+
compartmentId: compartmentId,
57+
name: "<your_name_of_the_metric_here>",
58+
dimensions: {
59+
"appName": "<your_dimensions>",
60+
"podName": "<your_dimensions>"
61+
},
62+
metadata: {
63+
"unit": "count",
64+
"displayName": "<your_display_name>"
65+
},
66+
datapoints: [{
67+
"timestamp": dateutc,
68+
"value": "<your_datapoint_value>",
69+
"count": "<your_datapoint_count>"
70+
}
71+
]
72+
}]
73+
74+
const PostMetricDataDetails: mt.models.PostMetricDataDetails = {
75+
metricData : MetricDataDetails
76+
}
77+
78+
const PostMetricDataRequest: mt.requests.PostMetricDataRequest = {
79+
postMetricDataDetails: PostMetricDataDetails
80+
};
81+
82+
const response = await monitoringClient.postMetricData(PostMetricDataRequest);
83+
//console.log("Retrieved :" + response.postMetricDataResponseDetails.failedMetricsCount);
84+
85+
console.log("Successfully posted custom metric with name: %s to namespace: %s ",MetricDataDetails[0].name,MetricDataDetails[0].namespace )
86+
87+
} catch (error) {
88+
console.log(" Not able to run post metric monitoring example. Error: " + error);
89+
}
90+
})();

0 commit comments

Comments
 (0)