This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add gRPC example * fix review comments * fix review comments
- Loading branch information
1 parent
622cc75
commit 0267db1
Showing
6 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 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 (we are using Stackdriver for this example), to give observability to our distributed systems. | ||
|
||
|
||
## Installation | ||
|
||
```sh | ||
$ # from this directory | ||
$ npm install | ||
``` | ||
|
||
Setup [Stackdriver Tracing and Monitoring](https://opencensus.io/codelabs/stackdriver/#0) | ||
|
||
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* 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'); | ||
|
||
const tracer = setupTracerAndExporters(); | ||
|
||
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()); | ||
const data = process.argv[2] || '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(); | ||
}); | ||
}); | ||
|
||
/** | ||
* The default export interval is 60 seconds. 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. | ||
*/ | ||
setTimeout(() => { | ||
console.log('done.'); | ||
}, 60000); | ||
} | ||
|
||
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 | ||
// 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 | ||
const 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); | ||
|
||
return tracer; | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
const tracer = setupTracerAndExporters(); | ||
|
||
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 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 | ||
// 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 | ||
const 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); | ||
|
||
return tracer; | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.