Skip to content

Commit e319969

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

File tree

1 file changed

+88
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)