Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Node.js: Update examples #579

Merged
merged 2 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 19 additions & 10 deletions content/exporters/custom-exporter/Node.js/Metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,34 @@ export class MyConsoleStatsExporter implements StatsEventListener {
And now to test it out as we would in a typically linked program, let's create a `expample.js` file:

{{<highlight javascript>}}
var opencensus = require('@opencensus/core');
var stats = new opencensus.Stats();
const { globalStats, AggregationType, TagMap } = require('@opencensus/core');

// Let's create an instance of our just created exporter
var exporter = new MyConsoleStatsExporter();
const exporter = new MyConsoleStatsExporter();
// And register it
stats.registerExporter(exporter);
globalStats.registerExporter(exporter);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we changing this to globalStats? What's up with stats? Other languages use the same or is another stats used somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Earlier we used to create Stats instance (new Stats()) and used in the application. With new release, we have changed it to a global singleton Stats instance (globalStats). This behavioral change is introduced to match other language implementation. (The name globalStats is specific to Node library).


// Let's create a measure
var measure = stats.createMeasureInt64('my/measure', "1");
const measure = globalStats.createMeasureInt64('my/measure', "1");
// our tags
var tags = {myTagKey: 'myTagValue'};
// a view
var view = stats.createView('my/view', measure, 2, ['myTagKey'], 'my view');
const myTagKey = { name: "myTagKey" };
const tags = new TagMap();
tags.set(myTagKey, { value: "myTagValue" });

// Create and Register the view
const view = globalStats.createView(
'my/view',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: what would you thin about adding comments for the name and description values e.g.

const view = globalStats.createView(
    /* name */ 'my/view',
   measure,
   AggregationType.LAST_VALUE,
   [myTagKey], 
   /* description */ 'my view'
);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

measure,
AggregationType.LAST_VALUE,
[myTagKey],
'my view'
);
globalStats..registerView(view);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right globalStats.., double dots "..". Were you able to run this code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I ran the example and it works as expected.

// and our measurement
var measurement = {measure, tags, value: 10};
const measurement = {measure, value: 10};

// finaly, let's record it
stats.record(measurement);
globalStats.record([measurement], tags);
{{</highlight>}}

Now, run it with `node example.js` and you should see logs for our view beeing created and our measurement beeing recorded.
8 changes: 4 additions & 4 deletions content/exporters/custom-exporter/Node.js/Trace.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ export class MyConsoleTraceExporter implements types.Exporter {
And now to test it out as we would in a typically linked program, let's create a `expample.js` file:

```javascript
var tracing = require('@opencensus/opencensus-nodejs');
const tracing = require('@opencensus/opencensus-nodejs');

// Let's create an instance of our just created exporter
var exporter = new MyConsoleTraceExporter();
const exporter = new MyConsoleTraceExporter();
// And start tracing with it
tracing.registerExporter(exporter).start();

// Now, lets create a simple HTTP 2 server
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/HTTP 2/HTTP/2/g

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

var http2 = require('http2')
var server2 = http2.createServer();
const http2 = require('http2')
const server2 = http2.createServer();

// On every call to http://localhost:8080 we will return a Hello World message
server2.on('stream', (stream, requestHeaders) => {
Expand Down
7 changes: 3 additions & 4 deletions content/exporters/supported-exporters/Node.js/Instana.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ To use Instana as your exporter, first ensure that you have an [Instana agent ru
Now let's use the Instana exporter:

```js
var tracing = require('@opencensus/nodejs');
var instana = require('@opencensus/exporter-instana');

var exporter = new instana.InstanaTraceExporter();
const tracing = require('@opencensus/nodejs');
const { InstanaTraceExporter }= require('@opencensus/exporter-instana');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems spurious, perhaps just keep the const change and then as it was before i.e.

const instana

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think the destructuring import is fine as that is a fairly common pattern in JS and then it saves you referencing it via the namespace below (i.e. you don't need to say const exporter = new instana.InstanaTraceExporter(); you can just say const exporter = new InstanaTraceExporter();)


const exporter = new InstanaTraceExporter();
tracing.registerExporter(exporter).start();
```
25 changes: 16 additions & 9 deletions content/exporters/supported-exporters/Node.js/Jaeger.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ logo: /img/partners/jaeger_logo.svg
- [Introduction](#introduction)
- [Installing the exporter](#installing-the-exporter)
- [Creating the exporter](#creating-the-exporter)
- [Viewing your traces](#viewing-your-traces)
- [Project link](#project-link)

## Introduction
Jaeger, inspired by Dapper and OpenZipkin, is a distributed tracing system released as open source by Uber Technologies.
Expand Down Expand Up @@ -40,21 +42,26 @@ npm install @opencensus/exporter-jaeger
Now let's use the Jaeger exporter:

```js
var core = require('@opencensus/core');
var tracing = require('@opencensus/nodejs');
var jaeger = require('@opencensus/exporter-jaeger');
const { logger } = require('@opencensus/core');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps let's make this style consistent: in some place we have const { objectName } = require(<import>) and in others const objectName = require(<import>) for example

const tracing = require('@opencensus/nodejs');

while here

const { logger } = require('@opencensus/core');

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const { objectName } = require(<import>) i.e. destructuring import is preferred wherever it is feasible.

const { JaegerTraceExporter } = require('@opencensus/exporter-jaeger');
const tracing = require('@opencensus/nodejs');

var jaegerOptions = {
// Add service name and jaeger options
const jaegerOptions = {
serviceName: 'opencensus-exporter-jaeger',
host: 'localhost',
port: 6832,
tags: [{key: 'opencensus-exporter-jeager', value: '0.0.1'}],
tags: [{key: 'opencensus-exporter-jeager', value: '0.0.9'}],
bufferTimeout: 10, // time in milliseconds
logger: core.logger.logger('debug'),
maxPacketSize: 1000
logger: logger.logger('debug')
};

var exporter = new jaeger.JaegerTraceExporter(jaegerOptions);

const exporter = new JaegerTraceExporter(jaegerOptions);
tracing.registerExporter(exporter).start();
```

## Viewing your traces
Please visit the Jaeger UI endpoint [http://localhost:16686](http://localhost:16686)

## Project link
You can find out more about the Jaeger project at [https://www.jaegertracing.io/](https://www.jaegertracing.io/)
53 changes: 43 additions & 10 deletions content/exporters/supported-exporters/Node.js/Prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ logo: /img/prometheus-logo.png
- [Introduction](#introduction)
- [Installing the exporter](#installing-the-exporter)
- [Creating the exporter](#creating-the-exporter)
- [Running Prometheus](#running-prometheus)
- [Viewing your metrics](#viewing-your-metrics)
- [Project link](#project-link)

## Introduction
Prometheus is a monitoring system that collects metrics, by scraping
Expand All @@ -31,26 +34,56 @@ npm install @opencensus/exporter-prometheus
```

## Creating the exporter
To create the exporter, we'll need to:

* Import and use the Prometheus exporter package
* Define a namespace that will uniquely identify our metrics when viewed on Prometheus
* Expose a port on which we shall run a `/metrics` endpoint
* With the defined port, we'll need a Promethus configuration file so that Prometheus can scrape from this endpoint

Now let's use the Prometheus exporter:

```js
const { Stats } = require('@opencensus/core');
const { globalStats } = require('@opencensus/core');
const { PrometheusStatsExporter } = require('@opencensus/exporter-prometheus');

// Add your port and startServer to the Prometheus options
const exporter = new PrometheusStatsExporter({
port: 9464,
startServer: false
startServer: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What changed here from startServer: false to startServer: true?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So earlier, it was like two step approach

  1. Don't start the Prometheus exporter server i.e. startServer: false
  2. Explicitly call startServer method to start the Prometheus exporter server i.e.
exporter.startServer(function callback() {
  // Callback
});

This can be possible by passing startServer: true option.

});

// Our Stats manager
const stats = new Stats();

// Pass the created exporter to Stats
stats.registerExporter(exporter);
globalStats.registerExporter(exporter);
```

// Run the server
exporter.startServer(function callback() {
// Callback
});
and then for our corresponding `prometheus.yaml` file:

```shell
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff including this!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be yaml instead of shell?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

global:
scrape_interval: 10s

external_labels:
monitor: 'demo'

scrape_configs:
- job_name: 'demo'

scrape_interval: 10s

static_configs:
- targets: ['localhost:8888']
```

## Running Prometheus
And then run Prometheus with your configuration
```shell
prometheus --config.file=prometheus.yaml
```

## Viewing your metrics
Please visit [http://localhost:9090](http://localhost:9090)

## Project link
You can find out more about the Prometheus project at [https://prometheus.io/](https://prometheus.io/)

20 changes: 14 additions & 6 deletions content/exporters/supported-exporters/Node.js/Zipkin.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ logo: /img/zipkin-logo.jpg
- [Introduction](#introduction)
- [Installing the exporter](#installing-the-exporter)
- [Creating the exporter](#creating-the-exporter)
- [Viewing your traces](#viewing-your-traces)
- [Project link](#project-link)

## Introduction
Zipkin is a distributed tracing system. It helps gather timing data needed to troubleshoot latency problems in microservice architectures.
Expand All @@ -18,8 +20,8 @@ It manages both the collection and lookup of this data. Zipkin’s design is bas

OpenCensus Node.js has support for this exporter available, distributed through NPM package [@opencensus/exporter-zipkin](https://www.npmjs.com/package/@opencensus/exporter-zipkin)

{{% notice tip %}}
For assistance setting up Zipkin, [Click here](/codelabs/zipkin) for a guided codelab.
{{% notice note %}}
This guide makes use of Zipkin for visualizing your data. For assistance setting up Zipkin, [Click here](/codelabs/zipkin) for a guided codelab.
{{% /notice %}}

## Installing the exporter
Expand All @@ -34,16 +36,22 @@ npm install @opencensus/exporter-zipkin
Now let's use the Zipkin exporter:

```js
var tracing = require('@opencensus/nodejs');
var zipkin = require('@opencensus/exporter-zipkin');
const tracing = require('@opencensus/nodejs');
const { ZipkinTraceExporter } = require('@opencensus/exporter-zipkin');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixing const tracing and const { ZipkinTraceExporter }


// Add your zipkin url (ex http://localhost:9411/api/v2/spans)
// and application name to the Zipkin options
var options = {
const zipkinOptions = {
url: 'your-zipkin-url',
serviceName: 'your-application-name'
};

var exporter = new zipkin.ZipkinTraceExporter(options);
const exporter = new ZipkinTraceExporter(zipkinOptions);
tracing.registerExporter(exporter).start();
```

## Viewing your traces
Please visit the Zipkin UI endpoint [http://localhost:9411](http://localhost:9411)

## Project link
You can find out more about the Zipkin project at [https://zipkin.io/](https://zipkin.io/)
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@ Stackdriver's minimum stats reporting period must be >= 60 seconds. Find out why
{{% /notice %}}

{{<highlight javascript>}}
var opencensus = require('@opencensus/core');
var stackdriver = require('@opencensus/exporter-stackdriver');
const { globalStats } = require('@opencensus/core');
const { StackdriverStatsExporter } = require('@opencensus/exporter-stackdriver');

// Add your project id to the Stackdriver options
var exporter = new stackdriver.StackdriverStatsExporter({projectId: "your-project-id"});
const exporter = new StackdriverStatsExporter({projectId: "your-project-id"});

var stats = new opencensus.Stats();

stats.registerExporter(exporter);
globalStats.registerExporter(exporter);
{{</highlight>}}

## Viewing your metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ export GOOGLE_APPLICATION_CREDENTIALS=path/to/your/credential.json
To create the exporter, in code:

{{<highlight javascript>}}
var tracing = require('@opencensus/nodejs');
var stackdriver = require('@opencensus/exporter-stackdriver');
const tracing = require('@opencensus/nodejs');
const { StackdriverTraceExporter } = require('@opencensus/exporter-stackdriver');

// Add your project id to the Stackdriver options
var exporter = new stackdriver.StackdriverTraceExporter({projectId: "your-project-id"});
const exporter = new StackdriverTraceExporter({projectId: "your-project-id"});

tracing.registerExporter(exporter).start();
{{</highlight>}}
Expand Down
Loading