-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add metric sample and update READMEs throughout
- Loading branch information
Showing
10 changed files
with
268 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Overview | ||
|
||
This example shows how to use [@opentelemetry/sdk-metrics](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-sdk-metrics) to instrument a simple Node.js application and export metrics to [Google Cloud Monitoring](https://cloud.google.com/monitoring/). | ||
|
||
## Installation | ||
|
||
```sh | ||
# from root of repo, build all packages | ||
npm install | ||
``` | ||
|
||
## Run the Application | ||
|
||
```sh | ||
# from this directory | ||
npm start | ||
``` | ||
|
||
## View metrics | ||
|
||
https://console.cloud.google.com//monitoring/metrics-explorer?project=your-project-id | ||
|
||
1. Select the Metric from the dropdown. You should see it under the resource "Generic Task": | ||
|
||
<img width="1584" alt="choose metric type" src="images/choose-metric-type.png?raw=true"/> | ||
|
||
2. View the timeseries: | ||
|
||
<img width="1584" alt="view timeseries" src="images/metric-timeseries.png?raw=true"/> | ||
|
||
|
||
## Useful links | ||
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/> | ||
- For more information on metrics, visit: <https://opentelemetry.io/docs/concepts/signals/metrics/> | ||
|
||
## LICENSE | ||
|
||
Apache License 2.0 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const { | ||
MeterProvider, | ||
PeriodicExportingMetricReader, | ||
} = require("@opentelemetry/sdk-metrics"); | ||
const { Resource } = require("@opentelemetry/resources"); | ||
const { | ||
MetricExporter, | ||
} = require("@google-cloud/opentelemetry-cloud-monitoring-exporter"); | ||
|
||
// Create MeterProvider | ||
const meterProvider = new MeterProvider({ | ||
// Create a resource. These resources attributes will be translated to generic_task | ||
// monitoring resource labels in Cloud Monitoring | ||
resource: new Resource({ | ||
"service.name": "example-metric-service", | ||
"service.namespace": "samples", | ||
"service.instance.id": "12345", | ||
}), | ||
}); | ||
// Register the exporter | ||
meterProvider.addMetricReader( | ||
new PeriodicExportingMetricReader({ | ||
// Export metrics every 10 seconds. 5 seconds is the smallest sample period allowed by | ||
// Cloud Monitoring. | ||
exportIntervalMillis: 10_000, | ||
exporter: new MetricExporter(), | ||
}) | ||
); | ||
|
||
// Create a meter | ||
const meter = meterProvider.getMeter("metrics-sample"); | ||
|
||
// Create a counter instrument | ||
const counter = meter.createCounter("metric_name"); | ||
// Record a measurement | ||
counter.add(10, { key: "value" }); | ||
|
||
// Wait for the metric to be exported | ||
new Promise((resolve) => { | ||
setTimeout(resolve, 11_000); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "nodejs-example-google-cloud-monitoring", | ||
"version": "0.15.0", | ||
"private": true, | ||
"license": "Apache-2.0", | ||
"author": "Google Inc.", | ||
"scripts": { | ||
"start": "node ./index.js" | ||
}, | ||
"engines": { | ||
"node": ">=10" | ||
}, | ||
"repository": "GoogleCloudPlatform/opentelemetry-operations-js", | ||
"dependencies": { | ||
"@google-cloud/opentelemetry-cloud-monitoring-exporter": "0.15.0", | ||
"@opentelemetry/api": "1.3.0", | ||
"@opentelemetry/resources": "1.8.0", | ||
"@opentelemetry/sdk-metrics": "1.8.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters