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

Commit 0267db1

Browse files
authored
Add gRPC integration guide (#355)
* Add gRPC example * fix review comments * fix review comments
1 parent 622cc75 commit 0267db1

File tree

6 files changed

+288
-0
lines changed

6 files changed

+288
-0
lines changed

examples/grpc/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Overview
2+
3+
Our service takes in a payload containing bytes and capitalizes them.
4+
5+
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.
6+
7+
8+
## Installation
9+
10+
```sh
11+
$ # from this directory
12+
$ npm install
13+
```
14+
15+
Setup [Stackdriver Tracing and Monitoring](https://opencensus.io/codelabs/stackdriver/#0)
16+
17+
## Run the Application
18+
19+
- Run the server
20+
21+
```sh
22+
$ # from this directory
23+
$ node ./capitalize_server.js
24+
```
25+
26+
- Run the client
27+
28+
```sh
29+
$ # from this directory
30+
$ node ./capitalize_client.js
31+
```
32+
33+
## Useful links
34+
- For more information on OpenCensus, visit: <https://opencensus.io/>
35+
- To checkout the OpenCensus for Node.js, visit: <https://github.com/census-instrumentation/opencensus-node>
36+
37+
## LICENSE
38+
39+
Apache License 2.0

examples/grpc/capitalize_client.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* gRPC://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const path = require('path');
18+
const grpc = require('grpc');
19+
const protoLoader = require('@grpc/proto-loader');
20+
const tracing = require('@opencensus/nodejs');
21+
const { plugin } = require('@opencensus/instrumentation-grpc');
22+
const { StackdriverTraceExporter } =
23+
require('@opencensus/exporter-stackdriver');
24+
25+
const tracer = setupTracerAndExporters();
26+
27+
const PROTO_PATH = path.join(__dirname, 'protos/defs.proto');
28+
const PROTO_OPTIONS = { keepCase: true, enums: String, defaults: true, oneofs: true };
29+
const definition = protoLoader.loadSync(PROTO_PATH, PROTO_OPTIONS);
30+
const rpcProto = grpc.loadPackageDefinition(definition).rpc;
31+
32+
function main () {
33+
const client = new rpcProto.Fetch('localhost:50051',
34+
grpc.credentials.createInsecure());
35+
const data = process.argv[2] || 'opencensus';
36+
console.log('> ', data);
37+
38+
tracer.startRootSpan({ name: 'octutorialsClient.capitalize' }, rootSpan => {
39+
client.capitalize({ data: Buffer.from(data) }, function (err, response) {
40+
if (err) {
41+
console.log('could not get grpc response');
42+
return;
43+
}
44+
console.log('< ', response.data.toString('utf8'));
45+
rootSpan.end();
46+
});
47+
});
48+
49+
/**
50+
* The default export interval is 60 seconds. The thread with the
51+
* StackdriverStatsExporter must live for at least the interval past any
52+
* metrics that must be collected, or some risk being lost if they are
53+
* recorded after the last export.
54+
*/
55+
setTimeout(() => {
56+
console.log('done.');
57+
}, 60000);
58+
}
59+
60+
function setupTracerAndExporters () {
61+
// Enable OpenCensus exporters to export traces to Stackdriver CloudTrace.
62+
// Exporters use Application Default Credentials (ADCs) to authenticate.
63+
// See https://developers.google.com/identity/protocols/application-default-credentials
64+
// for more details.
65+
// Expects ADCs to be provided through the environment as ${GOOGLE_APPLICATION_CREDENTIALS}
66+
// A Stackdriver workspace is required and provided through the environment as ${GOOGLE_PROJECT_ID}
67+
const projectId = process.env.GOOGLE_PROJECT_ID;
68+
69+
// GOOGLE_APPLICATION_CREDENTIALS are expected by a dependency of this code
70+
// Not this code itself. Checking for existence here but not retaining (as not needed)
71+
if (!projectId || !process.env.GOOGLE_APPLICATION_CREDENTIALS) {
72+
throw Error('Unable to proceed without a Project ID');
73+
}
74+
75+
// Creates Stackdriver exporter
76+
const exporter = new StackdriverTraceExporter({ projectId: projectId });
77+
78+
// Starts Stackdriver exporter
79+
tracing.registerExporter(exporter).start();
80+
81+
// Starts tracing and set sampling rate
82+
const tracer = tracing.start({
83+
samplingRate: 1 // For demo purposes, always sample
84+
}).tracer;
85+
86+
// Defines basedir and version
87+
const basedir = path.dirname(require.resolve('grpc'));
88+
const version = require(path.join(basedir, 'package.json')).version;
89+
90+
// Enables GRPC plugin: Method that enables the instrumentation patch.
91+
plugin.enable(grpc, tracer, version, /** plugin options */{}, basedir);
92+
93+
return tracer;
94+
}
95+
96+
main();

examples/grpc/capitalize_server.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* gRPC://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const path = require('path');
18+
const grpc = require('grpc');
19+
const protoLoader = require('@grpc/proto-loader');
20+
const tracing = require('@opencensus/nodejs');
21+
const { plugin } = require('@opencensus/instrumentation-grpc');
22+
const { StackdriverTraceExporter } =
23+
require('@opencensus/exporter-stackdriver');
24+
25+
const tracer = setupTracerAndExporters();
26+
27+
const PROTO_PATH = path.join(__dirname, 'protos/defs.proto');
28+
const PROTO_OPTIONS = { keepCase: true, enums: String, defaults: true, oneofs: true };
29+
const definition = protoLoader.loadSync(PROTO_PATH, PROTO_OPTIONS);
30+
const rpcProto = grpc.loadPackageDefinition(definition).rpc;
31+
32+
/** Implements the Capitalize RPC method. */
33+
function capitalize (call, callback) {
34+
const span = tracer.startChildSpan('octutorials.FetchImpl.capitalize');
35+
const data = call.request.data.toString('utf8');
36+
const capitalized = data.toUpperCase();
37+
for (let i = 0; i < 100000000; i++) {}
38+
span.end();
39+
callback(null, { data: Buffer.from(capitalized) });
40+
}
41+
42+
/**
43+
* Starts an RPC server that receives requests for the Fetch service at the
44+
* sample server port.
45+
*/
46+
function main () {
47+
const server = new grpc.Server();
48+
server.addService(rpcProto.Fetch.service, { capitalize: capitalize });
49+
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
50+
server.start();
51+
}
52+
53+
function setupTracerAndExporters () {
54+
// Enable OpenCensus exporters to export traces to Stackdriver CloudTrace.
55+
// Exporters use Application Default Credentials (ADCs) to authenticate.
56+
// See https://developers.google.com/identity/protocols/application-default-credentials
57+
// for more details.
58+
// Expects ADCs to be provided through the environment as ${GOOGLE_APPLICATION_CREDENTIALS}
59+
// A Stackdriver workspace is required and provided through the environment as ${GOOGLE_PROJECT_ID}
60+
const projectId = process.env.GOOGLE_PROJECT_ID;
61+
62+
// GOOGLE_APPLICATION_CREDENTIALS are expected by a dependency of this code
63+
// Not this code itself. Checking for existence here but not retaining (as not needed)
64+
if (!projectId || !process.env.GOOGLE_APPLICATION_CREDENTIALS) {
65+
throw Error('Unable to proceed without a Project ID');
66+
}
67+
// Creates Stackdriver exporter
68+
const exporter = new StackdriverTraceExporter({ projectId: projectId });
69+
70+
// Starts Stackdriver exporter
71+
tracing.registerExporter(exporter).start();
72+
73+
// Starts tracing and set sampling rate
74+
const tracer = tracing.start({
75+
samplingRate: 1 // For demo purposes, always sample
76+
}).tracer;
77+
78+
// Defines basedir and version
79+
const basedir = path.dirname(require.resolve('grpc'));
80+
const version = require(path.join(basedir, 'package.json')).version;
81+
82+
// Enables GRPC plugin: Method that enables the instrumentation patch.
83+
plugin.enable(grpc, tracer, version, /** plugin options */{}, basedir);
84+
85+
return tracer;
86+
}
87+
88+
main();

examples/grpc/package.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "grpc-example",
3+
"version": "0.0.1",
4+
"description": "Example of gRPC integration with OpenCensus",
5+
"repository": "census-instrumentation/opencensus-node",
6+
"keywords": [
7+
"opencensus",
8+
"grpc",
9+
"tracing",
10+
"stats",
11+
"metrics"
12+
],
13+
"author": "OpenCensus Authors",
14+
"license": "Apache-2.0",
15+
"engines": {
16+
"node": ">=6.0"
17+
},
18+
"scripts": {
19+
"lint": "semistandard *.js",
20+
"fix": "semistandard --fix"
21+
},
22+
"dependencies": {
23+
"@grpc/proto-loader": "^0.4.0",
24+
"@opencensus/exporter-stackdriver": "^0.0.9",
25+
"@opencensus/instrumentation-grpc": "^0.0.9",
26+
"@opencensus/nodejs": "^0.0.9",
27+
"grpc": "^1.18.0"
28+
},
29+
"devDependencies": {
30+
"semistandard": "^13.0.1"
31+
}
32+
}

examples/grpc/protos/defs.proto

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
syntax = "proto3";
16+
17+
option java_multiple_files = true;
18+
option java_package = "io.grpc.examples.helloworld";
19+
option java_outer_classname = "HelloWorldProto";
20+
option objc_class_prefix = "HLW";
21+
22+
package rpc;
23+
24+
service Fetch {
25+
// Sends a capitalizes payload
26+
rpc Capitalize(Payload) returns (Payload) {}
27+
}
28+
29+
// The request and response payload containing the id and data.
30+
message Payload {
31+
int32 id = 1;
32+
bytes data = 2;
33+
}
376 KB
Loading

0 commit comments

Comments
 (0)