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

Commit

Permalink
Node.js: Update examples (#579)
Browse files Browse the repository at this point in the history
* Node.js: Update examples

* shell->yaml, remove extra dot, HTTP 2->HTTP/2
  • Loading branch information
mayurkale22 authored Feb 15, 2019
1 parent d264671 commit 1690b8d
Show file tree
Hide file tree
Showing 14 changed files with 377 additions and 217 deletions.
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);

// 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(
/* name */ 'my/view',
measure,
AggregationType.LAST_VALUE,
[myTagKey],
/* description */ 'my view'
);
globalStats.registerView(view);
// 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.
10 changes: 5 additions & 5 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
var http2 = require('http2')
var server2 = http2.createServer();
// Now, lets create a simple HTTP/2 server
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');

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');
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
});

// 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:

```yaml
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');

// 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

0 comments on commit 1690b8d

Please sign in to comment.