Skip to content

Commit eb3cd50

Browse files
svrnmdyladan
andauthored
Website docs update 0821 (#2400)
Co-authored-by: Daniel Dyla <[email protected]>
1 parent 9790070 commit eb3cd50

File tree

4 files changed

+207
-9
lines changed

4 files changed

+207
-9
lines changed

website_docs/_index.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ export data.
1212

1313
## Status and Releases
1414

15-
| | Tracing | Metrics |
16-
| ----- | ------- | ------- |
17-
| API | GA | Alpha |
18-
| SDK | Beta | Alpha |
15+
| Signal | API Status | SDK Status |
16+
|---------|-------------|-------------------|
17+
| Trace | Stable | Release Candidate |
18+
| Metrics | Development | Development |
19+
| Logs | Roadmap | Roadmap |
1920

2021
You can find release information [here](https://github.com/open-telemetry/opentelemetry-js/releases)
2122

2223
## Further Reading
2324

2425
- [OpenTelemetry for JavaScript on GitHub](https://github.com/open-telemetry/opentelemetry-js)
25-
- [Getting Started](https://github.com/open-telemetry/opentelemetry-js/blob/main/getting-started/README.md)
26-
- [API Documentation](https://open-telemetry.github.io/opentelemetry-js)
27-
- [Getting In Touch (Gitter)](https://gitter.im/open-telemetry/opentelemetry-sdk-trace-node)
26+
- [API Reference](https://open-telemetry.github.io/opentelemetry-js-api)
27+
- [SDK Reference](https://open-telemetry.github.io/opentelemetry-js)

website_docs/exporters.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ title: "Exporters"
33
weight: 3
44
---
55

6-
In order to visualize and analyze your traces, you will need to export them to a tracing backend such as Jaeger or Zipkin.
7-
OpenTelemetry JS provides exporters for some common open source tracing backends.
6+
In order to visualize and analyze your traces and metrics, you will need to export them to a backend such as [Jaeger](https://www.jaegertracing.io/) or [Zipkin](https://zipkin.io/). OpenTelemetry JS provides exporters for some common open source backends.
87

98
Below you will find some introductions on how to setup backends and the matching exporters.
109

10+
- [Jaeger](#jaeger)
11+
- [Zipkin](#zipkin)
12+
- [Prometheus](#prometheus)
13+
- [OpenTelemetry Collector](#opentelemetry-collector)
14+
1115
## Jaeger
1216

1317
To set up Jaeger as quickly as possible, run it in a docker container:
@@ -64,6 +68,43 @@ const { BatchSpanProcessor } = require("@opentelemetry/tracing");
6468
provider.addSpanProcessor(new BatchSpanProcessor(new ZipkinExporter()))
6569
```
6670

71+
## Prometheus
72+
73+
To set up Prometheus as quickly as possible, run it in a docker container.
74+
You will need a `prometheus.yml` to configure the backend, use the following example
75+
and modify it to your needs:
76+
77+
```yml
78+
global:
79+
scrape_interval: 15s
80+
evaluation_interval: 15s
81+
82+
scrape_configs:
83+
- job_name: "prometheus"
84+
static_configs:
85+
- targets: ["localhost:9090"]
86+
```
87+
88+
With this file you can now start the docker container:
89+
90+
```shell
91+
docker run \
92+
-p 9090:9090 \
93+
-v ${PWD}/prometheus.yml:/etc/prometheus/prometheus.yml \
94+
prom/prometheus
95+
```
96+
97+
Update your opentelemetry configuration to use the exporter and to send data to your prometheus backend:
98+
99+
```javascript
100+
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
101+
const { MeterProvider } = require('@opentelemetry/metrics');
102+
const meter = new MeterProvider({
103+
exporter: new PrometheusExporter({port: 9090}),
104+
interval: 1000,
105+
}).getMeter('prometheus');
106+
```
107+
67108
## OpenTelemetry Collector
68109

69110
If you are looking for a vendor-agnostic way to receive, process and export your

website_docs/getting_started/nodejs.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ This guide will show you how to get started with tracing in Node.js.
1515
- [Instrumentation Modules](#instrumentation-modules)
1616
- [Setup](#setup)
1717
- [Run Application](#run-application)
18+
- [Metrics](#metrics)
19+
- [Dependencies](#dependencies-2)
20+
- [Core Dependencies](#core-dependencies-1)
21+
- [Exporter](#exporter-1)
22+
- [Setup](#setup-1)
23+
- [Run Application](#run-application-1)
1824

1925
## Example Application
2026

@@ -220,3 +226,119 @@ Now, when you open <http://localhost:8080> in your web browser, you should see t
220226
```
221227
222228
</details>
229+
230+
## Metrics
231+
232+
### Dependencies
233+
234+
The following dependencies are required to collect metrics in your Node.js application.
235+
236+
#### Core Dependencies
237+
238+
These dependencies are required to configure the tracing SDK and create spans.
239+
240+
- `@opentelemetry/metrics`
241+
242+
#### Exporter
243+
244+
In the following example, we will use the `ConsoleMetricExporter` which prints all spans to the console.
245+
246+
In order to visualize and analyze your metrics, you will need to export them to a metrics backend.
247+
Follow [these instructions](../exporters.md) for setting up a backend and exporter.
248+
249+
### Setup
250+
251+
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.
252+
253+
Create a file named `monitoring.js` and add the following code:
254+
255+
```javascript
256+
/* monitoring.js */
257+
'use strict';
258+
259+
const { MeterProvider, ConsoleMetricExporter } = require('@opentelemetry/metrics');
260+
261+
const meter = new MeterProvider({
262+
new ConsoleMetricExporter(),
263+
interval: 1000,
264+
}).getMeter('your-meter-name');
265+
```
266+
267+
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.
268+
269+
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:
270+
271+
```javascript
272+
/* monitoring.js */
273+
'use strict';
274+
275+
const { MeterProvider, ConsoleMetricExporter } = require('@opentelemetry/metrics');
276+
277+
const meter = new MeterProvider({
278+
new ConsoleMetricExporter(),
279+
interval: 1000,
280+
}).getMeter('your-meter-name');
281+
282+
const requestCount = meter.createCounter("requests", {
283+
description: "Count all incoming requests"
284+
});
285+
286+
const boundInstruments = new Map();
287+
288+
module.exports.countAllRequests = () => {
289+
return (req, res, next) => {
290+
if (!boundInstruments.has(req.path)) {
291+
const labels = { route: req.path };
292+
const boundCounter = requestCount.bind(labels);
293+
boundInstruments.set(req.path, boundCounter);
294+
}
295+
296+
boundInstruments.get(req.path).add(1);
297+
next();
298+
};
299+
};
300+
```
301+
302+
Now import and use this middleware in your application code `app.js`:
303+
304+
```javascript
305+
/* app.js */
306+
const express = require("express");
307+
const { countAllRequests } = require("./monitoring");
308+
const app = express();
309+
app.use(countAllRequests());
310+
/* ... */
311+
```
312+
313+
Now when you make requests to your service, your meter will count all requests.
314+
315+
**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.
316+
317+
### Run Application
318+
319+
First, install the dependencies as described above. Here you need to add the following:
320+
321+
```shell
322+
npm install --save @opentelemetry/metrics
323+
```
324+
325+
Now you can run your application:
326+
327+
```shell
328+
$ node app.js
329+
Listening for requests on http://localhost:8080
330+
```
331+
332+
Now, when you open <http://localhost:8080> in your web browser, you should see the metrics printed in the console by the `ConsoleMetricExporter`.
333+
334+
```json
335+
{
336+
"name": "requests",
337+
"description": "Count all incoming requests",
338+
"unit": "1",
339+
"metricKind": 0,
340+
"valueType": 1
341+
}
342+
{ "route": "/" }
343+
"value": "1"
344+
```

website_docs/instrumentation.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ weight: 3
55

66
This guide will cover creating and annotating spans, creating and annotating metrics, how to pass context, and a guide to automatic instrumentation for JavaScript. This simple example works in the browser as well as with Node.JS
77

8+
- [Example Application](#example-application)
9+
- [Creating Spans](#creating-spans)
10+
- [Attributes](#attributes)
11+
- [Semantic Attributes](#semantic-attributes)
12+
- [Span Status](#span-status)
13+
14+
## Example Application
15+
816
In the following this guide will use the following sample app:
917

1018
```javascript
@@ -161,3 +169,30 @@ function doWork(parent) {
161169
span.end();
162170
}
163171
```
172+
173+
## Span Status
174+
175+
A status can be set on a span, to indicate if the traced operation has completed successfully (`Ok`) or with an `Error`. The default status is `Unset`.
176+
177+
The status can be set at any time before the span is finished:
178+
179+
```javascript
180+
function doWork(parent) {
181+
const span = tracer.startSpan('doWork', {
182+
parent,
183+
});
184+
span.setStatus({
185+
code: opentelemetry.SpanStatusCode.OK,
186+
message: 'Ok.'
187+
})
188+
for (let i = 0; i <= Math.floor(Math.random() * 40000000); i += 1) {
189+
if(i > 10000) {
190+
span.setStatus({
191+
code: opentelemetry.SpanStatusCode.ERROR,
192+
message: 'Error.'
193+
})
194+
}
195+
}
196+
span.end();
197+
}
198+
```

0 commit comments

Comments
 (0)