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
38 changes: 38 additions & 0 deletions examples/grpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Overview

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.


## Installation

```sh
$ # from this directory
$ npm install
```


## Run the Application

- Run the server

```sh
$ # from this directory
$ node ./capitalize_server.js
```

- Run the client

```sh
$ # from this directory
$ node ./capitalize_client.js
```

## Useful links
- For more information on OpenCensus, visit: <https://opencensus.io/>
- To checkout the OpenCensus for Node.js, visit: <https://github.com/census-instrumentation/opencensus-node>

## LICENSE

Apache License 2.0
95 changes: 95 additions & 0 deletions examples/grpc/capitalize_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Copyright 2019, OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* gRPC://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const path = require('path');
const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const tracing = require('@opencensus/nodejs');
const { plugin } = require('@opencensus/instrumentation-grpc');
const { StackdriverTraceExporter } =
require('@opencensus/exporter-stackdriver');

let tracer;
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: what if you made setupOpencensusAndExporters return the tracer so that you could assign it with const. Maybe call it something like getSetupTracer or something similar.


setupOpencensusAndExporters();

const PROTO_PATH = path.join(__dirname, 'protos/defs.proto');
const PROTO_OPTIONS = { keepCase: true, enums: String, defaults: true, oneofs: true };
const definition = protoLoader.loadSync(PROTO_PATH, PROTO_OPTIONS);
const rpcProto = grpc.loadPackageDefinition(definition).rpc;

function main () {
const client = new rpcProto.Fetch('localhost:50051',
grpc.credentials.createInsecure());
let data;
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: could this just be const data = process.argv[2] || 'opencensus'? Not sure if that's actually better but feels more compact.

if (process.argv.length >= 3) {
data = process.argv[2];
} else {
data = 'opencensus';
}
console.log('> ', data);

tracer.startRootSpan({ name: 'octutorialsClient.capitalize' }, rootSpan => {
client.capitalize({ data: Buffer.from(data) }, function (err, response) {
if (err) {
console.log('could not get grpc response');
return;
}
console.log('< ', response.data.toString('utf8'));
rootSpan.end();
});
});

setTimeout(() => {
console.log('done.');
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the role of this message / is there a better way to check for doneness than waiting one minute?

Copy link
Member Author

Choose a reason for hiding this comment

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

The minimum reporting period for Stackdriver is 1 minute. The thread with the StackdriverStatsExporter must live for at least the interval past any metrics that must be collected, or some risk being lost if they are recorded after the last export. We planned to add a flush method instead of doing this.

I will enable StackdriverStatsExporter here, once the gRPC stats are available #270

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, interesting. Can you add a comment to explain this?

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.

}, 60000);
}

function setupOpencensusAndExporters () {
// 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
// for more details.
// Expects ADCs to be provided through the environment as ${GOOGLE_APPLICATION_CREDENTIALS}
// A Stackdriver workspace is required and provided through the environment as ${GOOGLE_PROJECT_ID}
const projectId = process.env.GOOGLE_PROJECT_ID;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be worth specifying above that this is intended to run on the Google Cloud Platform? Or is that already implied somewhere?


// GOOGLE_APPLICATION_CREDENTIALS are expected by a dependency of this code
// Not this code itself. Checking for existence here but not retaining (as not needed)
if (!projectId || !process.env.GOOGLE_APPLICATION_CREDENTIALS) {
throw Error('Unable to proceed without a Project ID');
}

// Creates Stackdriver exporter
const exporter = new StackdriverTraceExporter({ projectId: projectId });

// Starts Stackdriver exporter
tracing.registerExporter(exporter).start();

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

// Defines basedir and version
const basedir = path.dirname(require.resolve('grpc'));
const version = require(path.join(basedir, 'package.json')).version;

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

main();
88 changes: 88 additions & 0 deletions examples/grpc/capitalize_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright 2019, OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* gRPC://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const path = require('path');
const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const tracing = require('@opencensus/nodejs');
const { plugin } = require('@opencensus/instrumentation-grpc');
const { StackdriverTraceExporter } =
require('@opencensus/exporter-stackdriver');

let tracer;
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar optional comment here on assigning via const with the return value of the setup function.

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.


setupOpencensusAndExporters();

const PROTO_PATH = path.join(__dirname, 'protos/defs.proto');
const PROTO_OPTIONS = { keepCase: true, enums: String, defaults: true, oneofs: true };
const definition = protoLoader.loadSync(PROTO_PATH, PROTO_OPTIONS);
const rpcProto = grpc.loadPackageDefinition(definition).rpc;

/** Implements the Capitalize RPC method. */
function capitalize (call, callback) {
const span = tracer.startChildSpan('octutorials.FetchImpl.capitalize');
const data = call.request.data.toString('utf8');
const capitalized = data.toUpperCase();
for (let i = 0; i < 100000000; i++) {}
span.end();
callback(null, { data: Buffer.from(capitalized) });
}

/**
* Starts an RPC server that receives requests for the Fetch service at the
* sample server port.
*/
function main () {
const server = new grpc.Server();
server.addService(rpcProto.Fetch.service, { capitalize: capitalize });
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
server.start();
}

function setupOpencensusAndExporters () {
// 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
// for more details.
// Expects ADCs to be provided through the environment as ${GOOGLE_APPLICATION_CREDENTIALS}
// A Stackdriver workspace is required and provided through the environment as ${GOOGLE_PROJECT_ID}
const projectId = process.env.GOOGLE_PROJECT_ID;

// GOOGLE_APPLICATION_CREDENTIALS are expected by a dependency of this code
// Not this code itself. Checking for existence here but not retaining (as not needed)
if (!projectId || !process.env.GOOGLE_APPLICATION_CREDENTIALS) {
throw Error('Unable to proceed without a Project ID');
}
// Creates Stackdriver exporter
const exporter = new StackdriverTraceExporter({ projectId: projectId });

// Starts Stackdriver exporter
tracing.registerExporter(exporter).start();

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

// Defines basedir and version
const basedir = path.dirname(require.resolve('grpc'));
const version = require(path.join(basedir, 'package.json')).version;

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

main();
32 changes: 32 additions & 0 deletions examples/grpc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "grpc-example",
"version": "0.0.1",
"description": "Example of gRPC integration with OpenCensus",
"repository": "census-instrumentation/opencensus-node",
"keywords": [
"opencensus",
"grpc",
"tracing",
"stats",
"metrics"
],
"author": "OpenCensus Authors",
"license": "Apache-2.0",
"engines": {
"node": ">=6.0"
},
"scripts": {
"lint": "semistandard *.js",
"fix": "semistandard --fix"
},
"dependencies": {
"@grpc/proto-loader": "^0.4.0",
"@opencensus/exporter-stackdriver": "^0.0.9",
"@opencensus/instrumentation-grpc": "^0.0.9",
"@opencensus/nodejs": "^0.0.9",
"grpc": "^1.18.0"
},
"devDependencies": {
"semistandard": "^13.0.1"
}
}
33 changes: 33 additions & 0 deletions examples/grpc/protos/defs.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2019, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package rpc;

service Fetch {
// Sends a capitalizes payload
rpc Capitalize(Payload) returns (Payload) {}
}

// The request and response payload containing the id and data.
message Payload {
int32 id = 1;
bytes data = 2;
}
Binary file added examples/grpc/stackdriver_grpc_nodejs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.