|
| 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(); |
0 commit comments