Skip to content

Commit 0aa4fb5

Browse files
markwolffmayurkale22
authored andcommitted
docs(grpc): add grpc example (open-telemetry#326)
* docs(grpc): add grpc example * docs(readme): add zipkin ui image * fix: typos, add local images, refactor * fix: typo
1 parent d85ee88 commit 0aa4fb5

File tree

9 files changed

+637
-0
lines changed

9 files changed

+637
-0
lines changed

examples/grpc/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
`zipkin:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`).
36+
Go to Zipkin with your browser [http://localhost:9411/zipkin/traces/(your-trace-id)]() (e.g http://localhost:9411/zipkin/traces/4815c3d576d930189725f1f1d1bdfcc6)
37+
38+
<p align="center"><img src="./images/zipkin.png"/></p>
39+
40+
### Jaeger
41+
42+
- Run the server
43+
44+
```sh
45+
$ # from this directory
46+
$ npm run jaeger:server
47+
```
48+
49+
- Run the client
50+
51+
```sh
52+
$ # from this directory
53+
$ npm run jaeger:client
54+
```
55+
#### Jaeger UI
56+
57+
`jaeger:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`).
58+
Go to Jaeger with your browser [http://localhost:50051/trace/(your-trace-id)]() (e.g http://localhost:50051/trace/4815c3d576d930189725f1f1d1bdfcc6)
59+
60+
<p align="center"><img src="./images/jaeger.png"/></p>
61+
62+
## Useful links
63+
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
64+
- For more information on OpenTelemetry for Node.js, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-node-sdk>
65+
66+
## LICENSE
67+
68+
Apache License 2.0

examples/grpc/client.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const opentelemetry = require('@opentelemetry/core');
4+
const config = require('./setup');
5+
6+
/**
7+
* The trace instance needs to be initialized first, if you want to enable
8+
* automatic tracing for built-in plugins (gRPC in this case).
9+
*/
10+
config.setupTracerAndExporters('grpc-client-service');
11+
12+
const grpc = require('grpc');
13+
14+
const messages = require('./helloworld_pb');
15+
const services = require('./helloworld_grpc_pb');
16+
const PORT = 50051;
17+
const tracer = opentelemetry.getTracer();
18+
19+
/** A function which makes requests and handles response. */
20+
function main() {
21+
// span corresponds to outgoing requests. Here, we have manually created
22+
// the span, which is created to track work that happens outside of the
23+
// request lifecycle entirely.
24+
const span = tracer.startSpan('client.js:main()');
25+
tracer.withSpan(span, () => {
26+
console.log('Client traceId ', span.context().traceId);
27+
const client = new services.GreeterClient(
28+
`localhost:${PORT}`,
29+
grpc.credentials.createInsecure()
30+
);
31+
const request = new messages.HelloRequest();
32+
let user;
33+
if (process.argv.length >= 3) {
34+
user = process.argv[2];
35+
} else {
36+
user = 'world';
37+
}
38+
request.setName(user);
39+
client.sayHello(request, function(err, response) {
40+
span.end();
41+
if (err) throw err;
42+
console.log('Greeting:', response.getMessage());
43+
});
44+
});
45+
}
46+
47+
main();

examples/grpc/helloworld_grpc_pb.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
'use strict';
19+
var grpc = require('grpc');
20+
var helloworld_pb = require('./helloworld_pb.js');
21+
22+
function serialize_HelloReply(arg) {
23+
if (!(arg instanceof helloworld_pb.HelloReply)) {
24+
throw new Error('Expected argument of type HelloReply');
25+
}
26+
return Buffer.from(arg.serializeBinary());
27+
}
28+
29+
function deserialize_HelloReply(buffer_arg) {
30+
return helloworld_pb.HelloReply.deserializeBinary(new Uint8Array(buffer_arg));
31+
}
32+
33+
function serialize_HelloRequest(arg) {
34+
if (!(arg instanceof helloworld_pb.HelloRequest)) {
35+
throw new Error('Expected argument of type HelloRequest');
36+
}
37+
return Buffer.from(arg.serializeBinary());
38+
}
39+
40+
function deserialize_HelloRequest(buffer_arg) {
41+
return helloworld_pb.HelloRequest.deserializeBinary(
42+
new Uint8Array(buffer_arg)
43+
);
44+
}
45+
46+
// The greeting service definition.
47+
var GreeterService = (exports.GreeterService = {
48+
// Sends a greeting
49+
sayHello: {
50+
path: '/helloworld.Greeter/SayHello',
51+
requestStream: false,
52+
responseStream: false,
53+
requestType: helloworld_pb.HelloRequest,
54+
responseType: helloworld_pb.HelloReply,
55+
requestSerialize: serialize_HelloRequest,
56+
requestDeserialize: deserialize_HelloRequest,
57+
responseSerialize: serialize_HelloReply,
58+
responseDeserialize: deserialize_HelloReply
59+
}
60+
});
61+
62+
exports.GreeterClient = grpc.makeGenericClientConstructor(GreeterService);

0 commit comments

Comments
 (0)