Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS Docs: Temporarily get rid of metrics content in node getting started #1534

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions content/en/docs/instrumentation/js/getting-started/metrics.md.old
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
## Metrics

### Dependencies

The following dependencies are required to collect metrics in your Node.js application.

#### Core Dependencies

These dependencies are required to configure the tracing SDK and create spans.

- `@opentelemetry/sdk-metrics-base`

#### Exporter

In the following example, we will use the `ConsoleMetricExporter` which prints all spans to the console.

In order to visualize and analyze your metrics, you will need to export them to a metrics backend.
Follow [these instructions](../../exporters) for setting up a backend and exporter.

### Setup

You need a `Meter` to create and monitor metrics. A `Meter` in OpenTelemetry is the mechanism used to create and manage metrics, labels, and metric exporters.

Create a file named `monitoring.js` and add the following code:

```javascript
/* monitoring.js */
'use strict';

const { MeterProvider, ConsoleMetricExporter } = require('@opentelemetry/sdk-metrics-base');

const meter = new MeterProvider({
exporter: new ConsoleMetricExporter(),
interval: 1000,
}).getMeter('your-meter-name');
```

Now you can require this file from your application code and use the `Meter` to create and manage metrics. The simplest of these metrics is a counter.

Let's create and export from your `monitoring.js` file a middleware function that express can use to count all requests by route. Modify the `monitoring.js` file so it looks like this:

```javascript
/* monitoring.js */
'use strict';

const { MeterProvider, ConsoleMetricExporter } = require('@opentelemetry/sdk-metrics-base');

const meter = new MeterProvider({
exporter: new ConsoleMetricExporter(),
interval: 1000,
}).getMeter('your-meter-name');

const requestCount = meter.createCounter("requests", {
description: "Count all incoming requests"
});

const boundInstruments = new Map();

module.exports.countAllRequests = () => {
return (req, res, next) => {
if (!boundInstruments.has(req.path)) {
const labels = { route: req.path };
const boundCounter = requestCount.bind(labels);
boundInstruments.set(req.path, boundCounter);
}

boundInstruments.get(req.path).add(1);
next();
};
};
```

Now import and use this middleware in your application code `app.js`:

```javascript
/* app.js */
const express = require("express");
const { countAllRequests } = require("./monitoring");
const app = express();
app.use(countAllRequests());
/* ... */
```

Now when you make requests to your service, your meter will count all requests.

**Note**: Creating a new labelSet and binding on every request is not ideal because creating the labelSet can often be an expensive operation. Therefore, the instruments are created and stored in a Map according to the route key.

### Run Application

First, install the dependencies as described above. Here you need to add the following:

```shell
npm install --save @opentelemetry/sdk-metrics-base
```

Now you can run your application:

```shell
$ node app.js
Listening for requests on http://localhost:8080
```

Now, when you open <http://localhost:8080> in your web browser, you should see the metrics printed in the console by the `ConsoleMetricExporter`.

```json
{
"name": "requests",
"description": "Count all incoming requests",
"unit": "1",
"metricKind": 0,
"valueType": 1
}
{ "route": "/" }
"value": "1"
```
119 changes: 6 additions & 113 deletions content/en/docs/instrumentation/js/getting-started/nodejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,118 +198,11 @@ Open <http://localhost:8080> in your web browser and reload the page a few times

</details>

## Metrics
## Next Steps

### Dependencies

The following dependencies are required to collect metrics in your Node.js application.

#### Core Dependencies

These dependencies are required to configure the tracing SDK and create spans.

- `@opentelemetry/sdk-metrics-base`

#### Exporter

In the following example, we will use the `ConsoleMetricExporter` which prints all spans to the console.

In order to visualize and analyze your metrics, you will need to export them to a metrics backend.
Follow [these instructions](../../exporters) for setting up a backend and exporter.

### Setup

You need a `Meter` to create and monitor metrics. A `Meter` in OpenTelemetry is the mechanism used to create and manage metrics, labels, and metric exporters.

Create a file named `monitoring.js` and add the following code:

```javascript
/* monitoring.js */
'use strict';

const { MeterProvider, ConsoleMetricExporter } = require('@opentelemetry/sdk-metrics-base');

const meter = new MeterProvider({
exporter: new ConsoleMetricExporter(),
interval: 1000,
}).getMeter('your-meter-name');
```

Now you can require this file from your application code and use the `Meter` to create and manage metrics. The simplest of these metrics is a counter.

Let's create and export from your `monitoring.js` file a middleware function that express can use to count all requests by route. Modify the `monitoring.js` file so it looks like this:

```javascript
/* monitoring.js */
'use strict';

const { MeterProvider, ConsoleMetricExporter } = require('@opentelemetry/sdk-metrics-base');

const meter = new MeterProvider({
exporter: new ConsoleMetricExporter(),
interval: 1000,
}).getMeter('your-meter-name');

const requestCount = meter.createCounter("requests", {
description: "Count all incoming requests"
});

const boundInstruments = new Map();
Enrich your instrumentation generated automatically with
[manual instrumentation](/docs/instrumentation/js/instrumentation) of your own codebase. This gets you
customized observability data.

module.exports.countAllRequests = () => {
return (req, res, next) => {
if (!boundInstruments.has(req.path)) {
const labels = { route: req.path };
const boundCounter = requestCount.bind(labels);
boundInstruments.set(req.path, boundCounter);
}

boundInstruments.get(req.path).add(1);
next();
};
};
```

Now import and use this middleware in your application code `app.js`:

```javascript
/* app.js */
const express = require("express");
const { countAllRequests } = require("./monitoring");
const app = express();
app.use(countAllRequests());
/* ... */
```

Now when you make requests to your service, your meter will count all requests.

**Note**: Creating a new labelSet and binding on every request is not ideal because creating the labelSet can often be an expensive operation. Therefore, the instruments are created and stored in a Map according to the route key.

### Run Application

First, install the dependencies as described above. Here you need to add the following:

```shell
npm install --save @opentelemetry/sdk-metrics-base
```

Now you can run your application:

```shell
$ node app.js
Listening for requests on http://localhost:8080
```

Now, when you open <http://localhost:8080> in your web browser, you should see the metrics printed in the console by the `ConsoleMetricExporter`.

```json
{
"name": "requests",
"description": "Count all incoming requests",
"unit": "1",
"metricKind": 0,
"valueType": 1
}
{ "route": "/" }
"value": "1"
```
You'll also want to configure an appropriate exporter to [export your telemetry
data](/docs/instrumentation/js/exporters) to one or more telemetry backends.