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

Add gRPC integration guide #355

Merged
merged 3 commits into from
Feb 25, 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
3 changes: 2 additions & 1 deletion examples/grpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Our service takes in a payload containing bytes and capitalizes them.

Using OpenCensus Node, we can collect traces of our system and export them to the backend of our choice, to give observability to our distributed systems.
Using OpenCensus Node, we can collect traces of our system and export them to the backend of our choice(we are using Stackdriver for this example), to give observability to our distributed systems.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: add space

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



## Installation
Expand All @@ -12,6 +12,7 @@ $ # from this directory
$ npm install
```

Setup [Stackdriver Tracing and Monitoring](https://opencensus.io/codelabs/stackdriver/#0)

## Run the Application

Expand Down
17 changes: 6 additions & 11 deletions examples/grpc/capitalize_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const { plugin } = require('@opencensus/instrumentation-grpc');
const { StackdriverTraceExporter } =
require('@opencensus/exporter-stackdriver');

let tracer;

setupOpencensusAndExporters();
const tracer = setupTracerAndExporters();

const PROTO_PATH = path.join(__dirname, 'protos/defs.proto');
const PROTO_OPTIONS = { keepCase: true, enums: String, defaults: true, oneofs: true };
Expand All @@ -34,12 +32,7 @@ const rpcProto = grpc.loadPackageDefinition(definition).rpc;
function main () {
const client = new rpcProto.Fetch('localhost:50051',
grpc.credentials.createInsecure());
let data;
if (process.argv.length >= 3) {
data = process.argv[2];
} else {
data = 'opencensus';
}
const data = process.argv[2] || 'opencensus';
console.log('> ', data);

tracer.startRootSpan({ name: 'octutorialsClient.capitalize' }, rootSpan => {
Expand All @@ -58,7 +51,7 @@ function main () {
}, 60000);
}

function setupOpencensusAndExporters () {
function setupTracerAndExporters () {
// Enable OpenCensus exporters to export traces to Stackdriver CloudTrace.
// Exporters use Application Default Credentials (ADCs) to authenticate.
// See https://developers.google.com/identity/protocols/application-default-credentials
Expand All @@ -80,7 +73,7 @@ function setupOpencensusAndExporters () {
tracing.registerExporter(exporter).start();

// Starts tracing and set sampling rate
tracer = tracing.start({
const tracer = tracing.start({
samplingRate: 1 // For demo purposes, always sample
}).tracer;

Expand All @@ -90,6 +83,8 @@ function setupOpencensusAndExporters () {

// Enables GRPC plugin: Method that enables the instrumentation patch.
plugin.enable(grpc, tracer, version, /** plugin options */{}, basedir);

return tracer;
}

main();
10 changes: 5 additions & 5 deletions examples/grpc/capitalize_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const { plugin } = require('@opencensus/instrumentation-grpc');
const { StackdriverTraceExporter } =
require('@opencensus/exporter-stackdriver');

let tracer;

setupOpencensusAndExporters();
const tracer = setupTracerAndExporters();

const PROTO_PATH = path.join(__dirname, 'protos/defs.proto');
const PROTO_OPTIONS = { keepCase: true, enums: String, defaults: true, oneofs: true };
Expand Down Expand Up @@ -52,7 +50,7 @@ function main () {
server.start();
}

function setupOpencensusAndExporters () {
function setupTracerAndExporters () {
// Enable OpenCensus exporters to export traces to Stackdriver CloudTrace.
// Exporters use Application Default Credentials (ADCs) to authenticate.
// See https://developers.google.com/identity/protocols/application-default-credentials
Expand All @@ -73,7 +71,7 @@ function setupOpencensusAndExporters () {
tracing.registerExporter(exporter).start();

// Starts tracing and set sampling rate
tracer = tracing.start({
const tracer = tracing.start({
samplingRate: 1 // For demo purposes, always sample
}).tracer;

Expand All @@ -83,6 +81,8 @@ function setupOpencensusAndExporters () {

// Enables GRPC plugin: Method that enables the instrumentation patch.
plugin.enable(grpc, tracer, version, /** plugin options */{}, basedir);

return tracer;
}

main();