diff --git a/examples/javascript/monitoring-post-metric.js b/examples/javascript/monitoring-post-metric.js new file mode 100644 index 0000000000..6e2e3bbb7b --- /dev/null +++ b/examples/javascript/monitoring-post-metric.js @@ -0,0 +1,89 @@ +/** + * Copyright (c) 2020, 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. + */ + + /* @param args Arguments to provide to the example. The following arguments are expected: + * + * Refer https://docs.cloud.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/MetricData/PostMetricData for more details. + * This example uses phoenix metric endpoint https://telemetry-ingestion.us-phoenix-1.oraclecloud.com + * we can change this to a different region (refer https://docs.cloud.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/). + */ + +const mt = require("oci-monitoring"); +const common = require("oci-common"); + +const configurationFilePath = "~/.oci/config"; +const configProfile = "DEFAULT"; + +const provider = new common.ConfigFileAuthenticationDetailsProvider( + configurationFilePath, + configProfile +); + +const args = process.argv.slice(1); +console.log(args); +if (args.length !== 2) { + console.error( + "Unexpected number of arguments received. Usage : monitoring-alarm.ts " + ); + process.exit(-1); +} + +const compartmentId = args[1]; + +const monitoringClient = new mt.MonitoringClient({ + authenticationDetailsProvider: provider +}); +monitoringClient.region = common.Region.US_PHOENIX_1; +monitoringClient.endpoint = "https://telemetry-ingestion.us-phoenix-1.oraclecloud.com"; + +(async () => { + try { + + var datenow = new Date(); + var dateutc = new Date(datenow.toUTCString()); + /* The timestamp datapoint format used is defined by RFC3339. + https://docs.cloud.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/datatypes/Datapoint + */ + + const MetricDataDetails = [{ + "namespace": "", + "resourceGroup": "", + "compartmentId": compartmentId, + "name": "", + "dimensions": { + "appName": "", + "podName": "" + }, + "metadata": { + "unit": "count", + "displayName": "" + }, + "datapoints": [{ + "timestamp": dateutc, + "value": "", + "count": "" + } + ] + }] + + const PostMetricDataDetails = { + "metricData" : MetricDataDetails + } + + const PostMetricDataRequest = { + "postMetricDataDetails": PostMetricDataDetails + }; + + const post_response = await monitoringClient.postMetricData(PostMetricDataRequest); + //console.log("Retrieved :" + response.postMetricDataResponseDetails.failedMetricsCount); + + 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 ) + + } catch (error) { + console.log(" Not able to run post metric monitoring example. Error: " + error); + } +})(); \ No newline at end of file diff --git a/examples/typescript/monitoring-post-metric.ts b/examples/typescript/monitoring-post-metric.ts new file mode 100644 index 0000000000..a7fb35d4ac --- /dev/null +++ b/examples/typescript/monitoring-post-metric.ts @@ -0,0 +1,90 @@ +/** + * Copyright (c) 2020, 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. + */ + + /* @param args Arguments to provide to the example. The following arguments are expected: + *
    + *
  • The first argument is the OCID of the compartment.
  • + *
+ * Refer https://docs.cloud.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/MetricData/PostMetricData for more details. + * This example uses phoenix metric endpoint https://telemetry-ingestion.us-phoenix-1.oraclecloud.com + * we can change this to a different region (refer https://docs.cloud.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/). + */ + +import mt = require("oci-monitoring"); +import common = require("oci-common"); +import { MetricDataDetails } from "oci-monitoring/lib/model"; + +const configurationFilePath = "~/.oci/config"; +const configProfile = "DEFAULT"; + +const provider: common.ConfigFileAuthenticationDetailsProvider = new common.ConfigFileAuthenticationDetailsProvider( + configurationFilePath, + configProfile +); + +const args = process.argv.slice(1); +console.log(args); +if (args.length !== 2) { + console.error( + "Unexpected number of arguments received. Usage : monitoring-alarm.ts " + ); + process.exit(-1); +} + +const compartmentId: string = args[1]; + +const monitoringClient: mt.MonitoringClient = new mt.MonitoringClient({ + authenticationDetailsProvider: provider +}); +monitoringClient.region = common.Region.US_PHOENIX_1; +monitoringClient.endpoint = "https://telemetry-ingestion.us-phoenix-1.oraclecloud.com"; + +(async () => { + try { + + var datenow = new Date(); + var dateutc = new Date(datenow.toUTCString()); + /* The timestamp datapoint format used is defined by RFC3339. + https://docs.cloud.oracle.com/en-us/iaas/api/#/en/monitoring/20180401/datatypes/Datapoint + */ + + const MetricDataDetails: Array = [{ + namespace: "", + resourceGroup: "", + compartmentId: compartmentId, + name: "", + dimensions: { + "appName": "", + "podName": "" + }, + metadata: { + "unit": "count", + "displayName": "" + }, + datapoints: [{ + "timestamp": dateutc, + "value": "", + "count": "" + } + ] + }] + + const PostMetricDataDetails: mt.models.PostMetricDataDetails = { + metricData : MetricDataDetails + } + + const PostMetricDataRequest: mt.requests.PostMetricDataRequest = { + postMetricDataDetails: PostMetricDataDetails + }; + + const response = await monitoringClient.postMetricData(PostMetricDataRequest); + //console.log("Retrieved :" + response.postMetricDataResponseDetails.failedMetricsCount); + + console.log("Successfully posted custom metric with name: %s to namespace: %s ",MetricDataDetails[0].name,MetricDataDetails[0].namespace ) + + } catch (error) { + console.log(" Not able to run post metric monitoring example. Error: " + error); + } +})(); \ No newline at end of file