Skip to content

Commit e056558

Browse files
feat(grpc-js): add @grpc/grpc-js plugin (#1201)
Co-authored-by: Mayur Kale <[email protected]>
1 parent 3bf4e54 commit e056558

40 files changed

+2648
-692
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ OpenTelemetry can collect tracing data automatically using plugins. Vendors/User
183183
##### Core
184184

185185
- [@opentelemetry/plugin-grpc][otel-plugin-grpc]
186+
- [@opentelemetry/plugin-grpc-js][otel-plugin-grpc-js]
186187
- [@opentelemetry/plugin-http][otel-plugin-http]
187188
- [@opentelemetry/plugin-https][otel-plugin-https]
188189

@@ -242,6 +243,7 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
242243

243244
[otel-plugin-fetch]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-fetch
244245
[otel-plugin-grpc]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-grpc
246+
[otel-plugin-grpc-js]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-grpc-js
245247
[otel-plugin-http]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-http
246248
[otel-plugin-https]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-https
247249
[otel-plugin-xml-http-request]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-xml-http-request

examples/.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
"no-console": "off",
1313
"import/no-unresolved": "off",
1414
"no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
15-
}
15+
},
16+
"ignorePatterns": "**/*_pb.js"
1617
}

examples/grpc-js/README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Overview
2+
3+
OpenTelemetry gRPC Instrumentation allows the user to automatically collect trace data and export them to the backend of choice (we can use Zipkin or Jaeger for this example), to give observability to distributed systems.
4+
5+
## Installation
6+
7+
```sh
8+
# from this directory
9+
npm install
10+
```
11+
12+
Setup [Zipkin Tracing](https://zipkin.io/pages/quickstart.html)
13+
or
14+
Setup [Jaeger Tracing](https://www.jaegertracing.io/docs/latest/getting-started/#all-in-one)
15+
16+
## Run the Application
17+
18+
### Zipkin
19+
20+
- Run the server
21+
22+
```sh
23+
# from this directory
24+
npm run zipkin:server
25+
```
26+
27+
- Run the client
28+
29+
```sh
30+
# from this directory
31+
npm run zipkin:client
32+
```
33+
34+
#### Zipkin UI
35+
36+
`zipkin:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`).
37+
Go to Zipkin with your browser <http://localhost:9411/zipkin/traces/(your-trace-id)> (e.g <http://localhost:9411/zipkin/traces/4815c3d576d930189725f1f1d1bdfcc6)>
38+
39+
<p align="center"><img src="./images/zipkin.png"/></p>
40+
41+
### Jaeger
42+
43+
- Run the server
44+
45+
```sh
46+
# from this directory
47+
npm run jaeger:server
48+
```
49+
50+
- Run the client
51+
52+
```sh
53+
# from this directory
54+
npm run jaeger:client
55+
```
56+
57+
#### Jaeger UI
58+
59+
`jaeger:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`).
60+
Go to Jaeger with your browser <http://localhost:50051/trace/(your-trace-id)> (e.g <http://localhost:50051/trace/4815c3d576d930189725f1f1d1bdfcc6)>
61+
62+
<p align="center"><img src="./images/jaeger.png"/></p>
63+
64+
## Useful links
65+
66+
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
67+
- For more information on OpenTelemetry for Node.js, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-node>
68+
69+
## LICENSE
70+
71+
Apache License 2.0

examples/grpc-js/client.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
const tracer = require('./tracer')('example-grpc-client');
4+
// eslint-disable-next-line import/order
5+
const grpc = require('@grpc/grpc-js');
6+
const messages = require('./helloworld_pb');
7+
const services = require('./helloworld_grpc_pb');
8+
9+
const PORT = 50051;
10+
11+
/** Send a test gRPC Hello Request to the Greeter Service (server.js) */
12+
function main() {
13+
// span corresponds to outgoing requests. Here, we have manually created
14+
// the span, which is created to track work that happens outside of the
15+
// request lifecycle entirely.
16+
const span = tracer.startSpan('client.js:main()');
17+
tracer.withSpan(span, () => {
18+
console.log('Client traceId ', span.context().traceId);
19+
const client = new services.GreeterClient(
20+
`localhost:${PORT}`,
21+
grpc.credentials.createInsecure(),
22+
);
23+
const request = new messages.HelloRequest();
24+
let user;
25+
if (process.argv.length >= 3) {
26+
// eslint-disable-next-line prefer-destructuring
27+
user = process.argv[2];
28+
} else {
29+
user = 'world';
30+
}
31+
request.setName(user);
32+
client.sayHello(request, (err, response) => {
33+
span.end();
34+
if (err) throw err;
35+
console.log('Greeting:', response.getMessage());
36+
});
37+
});
38+
39+
// The process must live for at least the interval past any traces that
40+
// must be exported, or some risk being lost if they are recorded after the
41+
// last export.
42+
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.');
43+
setTimeout(() => { console.log('Completed.'); }, 5000);
44+
}
45+
46+
main();
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// GENERATED CODE -- DO NOT EDIT!
2+
3+
// Original file comments:
4+
// Copyright 2015 gRPC authors.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
19+
'use strict';
20+
21+
const grpc = require('grpc');
22+
const helloworld_pb = require('./helloworld_pb.js');
23+
24+
function serialize_HelloReply(arg) {
25+
if (!(arg instanceof helloworld_pb.HelloReply)) {
26+
throw new Error('Expected argument of type HelloReply');
27+
}
28+
return Buffer.from(arg.serializeBinary());
29+
}
30+
31+
function deserialize_HelloReply(buffer_arg) {
32+
return helloworld_pb.HelloReply.deserializeBinary(new Uint8Array(buffer_arg));
33+
}
34+
35+
function serialize_HelloRequest(arg) {
36+
if (!(arg instanceof helloworld_pb.HelloRequest)) {
37+
throw new Error('Expected argument of type HelloRequest');
38+
}
39+
return Buffer.from(arg.serializeBinary());
40+
}
41+
42+
function deserialize_HelloRequest(buffer_arg) {
43+
return helloworld_pb.HelloRequest.deserializeBinary(
44+
new Uint8Array(buffer_arg),
45+
);
46+
}
47+
48+
// The greeting service definition.
49+
const GreeterService = (exports.GreeterService = {
50+
// Sends a greeting
51+
sayHello: {
52+
path: '/helloworld.Greeter/SayHello',
53+
requestStream: false,
54+
responseStream: false,
55+
requestType: helloworld_pb.HelloRequest,
56+
responseType: helloworld_pb.HelloReply,
57+
requestSerialize: serialize_HelloRequest,
58+
requestDeserialize: deserialize_HelloRequest,
59+
responseSerialize: serialize_HelloReply,
60+
responseDeserialize: deserialize_HelloReply,
61+
},
62+
});
63+
64+
exports.GreeterClient = grpc.makeGenericClientConstructor(GreeterService);

0 commit comments

Comments
 (0)