-
Notifications
You must be signed in to change notification settings - Fork 793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(grpc): add grpc example #326
Changes from 2 commits
af2a787
7e3f0d4
43b3c62
bb87739
38a8365
6eeb4c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,67 @@ | ||||||
# Overview | ||||||
|
||||||
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. | ||||||
|
||||||
## Installation | ||||||
|
||||||
```sh | ||||||
$ # from this directory | ||||||
$ npm install | ||||||
``` | ||||||
|
||||||
Setup [Zipkin Tracing](https://zipkin.io/pages/quickstart.html) | ||||||
or | ||||||
Setup [Jaeger Tracing](https://www.jaegertracing.io/docs/1.12/getting-started/#all-in-one) | ||||||
|
||||||
## Run the Application | ||||||
|
||||||
### Zipkin | ||||||
|
||||||
- Run the server | ||||||
|
||||||
```sh | ||||||
$ # from this directory | ||||||
$ npm run zipkin:server | ||||||
``` | ||||||
|
||||||
- Run the client | ||||||
|
||||||
```sh | ||||||
$ # from this directory | ||||||
$ npm run zipkin:client | ||||||
``` | ||||||
|
||||||
#### Zipkin UI | ||||||
`zipkin:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`). | ||||||
Go to zipkin with your browser [http://localhost:9411/zipkin/traces/(your-trace-id)]() (e.g http://localhost:9411/zipkin/traces/4815c3d576d930189725f1f1d1bdfcc6) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
<p align="center"><img src="https://user-images.githubusercontent.com/5515583/65533964-7a11a600-deb3-11e9-8af0-a91243db6fdc.png"/></p> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we store it in the repo ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, stored in |
||||||
|
||||||
### Jaeger | ||||||
|
||||||
- Run the server | ||||||
|
||||||
```sh | ||||||
$ # from this directory | ||||||
$ npm run jaeger:server | ||||||
``` | ||||||
|
||||||
- Run the client | ||||||
|
||||||
```sh | ||||||
$ # from this directory | ||||||
$ npm run jaeger:client | ||||||
``` | ||||||
#### Jaeger UI | ||||||
|
||||||
`jaeger:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`). | ||||||
Go to zipkin with your browser [http://localhost:50051/trace/(your-trace-id)]() (e.g http://localhost:50051/trace/4815c3d576d930189725f1f1d1bdfcc6) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/zipkin/jaeger/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
|
||||||
## Useful links | ||||||
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/> | ||||||
- For more information on OpenTelemetry for Node.js, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-node-sdk> | ||||||
|
||||||
## LICENSE | ||||||
|
||||||
Apache License 2.0 |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,48 @@ | ||||||
'use strict'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably, we can move this example in a |
||||||
|
||||||
const opentelemetry = require('@opentelemetry/core'); | ||||||
const config = require('./setup'); | ||||||
|
||||||
/** | ||||||
* The trace instance needs to be initialized first, if you want to enable | ||||||
* automatic tracing for built-in plugins (HTTP in this case). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
config.setupTracerAndExporters('grpc-client-service'); | ||||||
|
||||||
const grpc = require('grpc'); | ||||||
|
||||||
const messages = require('./helloworld_pb'); | ||||||
const services = require('./helloworld_grpc_pb'); | ||||||
const DEFAULT_PORT = 50051; | ||||||
const tracer = opentelemetry.getTracer(); | ||||||
|
||||||
/** A function which makes requests and handles response. */ | ||||||
function main(port) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to pass port here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No - moved to just use local var instead |
||||||
if (typeof port !== 'number') port = DEFAULT_PORT; | ||||||
// span corresponds to outgoing requests. Here, we have manually created | ||||||
// the span, which is created to track work that happens outside of the | ||||||
// request lifecycle entirely. | ||||||
const span = tracer.startSpan('client.js:main()'); | ||||||
tracer.withSpan(span, () => { | ||||||
console.log('Client traceId ', span.context().traceId); | ||||||
const client = new services.GreeterClient( | ||||||
`localhost:${port}`, | ||||||
grpc.credentials.createInsecure() | ||||||
); | ||||||
const request = new messages.HelloRequest(); | ||||||
let user; | ||||||
if (process.argv.length >= 3) { | ||||||
user = process.argv[2]; | ||||||
} else { | ||||||
user = 'world'; | ||||||
} | ||||||
request.setName(user); | ||||||
client.sayHello(request, function(err, response) { | ||||||
span.end(); | ||||||
if (err) throw err; | ||||||
console.log('Greeting:', response.getMessage()); | ||||||
}); | ||||||
}); | ||||||
} | ||||||
|
||||||
main(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// GENERATED CODE -- DO NOT EDIT! | ||
|
||
// Original file comments: | ||
// Copyright 2015 gRPC 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. | ||
// | ||
'use strict'; | ||
var grpc = require('grpc'); | ||
var helloworld_pb = require('./helloworld_pb.js'); | ||
|
||
function serialize_HelloReply(arg) { | ||
if (!(arg instanceof helloworld_pb.HelloReply)) { | ||
throw new Error('Expected argument of type HelloReply'); | ||
} | ||
return Buffer.from(arg.serializeBinary()); | ||
} | ||
|
||
function deserialize_HelloReply(buffer_arg) { | ||
return helloworld_pb.HelloReply.deserializeBinary(new Uint8Array(buffer_arg)); | ||
} | ||
|
||
function serialize_HelloRequest(arg) { | ||
if (!(arg instanceof helloworld_pb.HelloRequest)) { | ||
throw new Error('Expected argument of type HelloRequest'); | ||
} | ||
return Buffer.from(arg.serializeBinary()); | ||
} | ||
|
||
function deserialize_HelloRequest(buffer_arg) { | ||
return helloworld_pb.HelloRequest.deserializeBinary( | ||
new Uint8Array(buffer_arg) | ||
); | ||
} | ||
|
||
// The greeting service definition. | ||
var GreeterService = (exports.GreeterService = { | ||
// Sends a greeting | ||
sayHello: { | ||
path: '/helloworld.Greeter/SayHello', | ||
requestStream: false, | ||
responseStream: false, | ||
requestType: helloworld_pb.HelloRequest, | ||
responseType: helloworld_pb.HelloReply, | ||
requestSerialize: serialize_HelloRequest, | ||
requestDeserialize: deserialize_HelloRequest, | ||
responseSerialize: serialize_HelloReply, | ||
responseDeserialize: deserialize_HelloReply | ||
} | ||
}); | ||
|
||
exports.GreeterClient = grpc.makeGenericClientConstructor(GreeterService); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.