|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | +const initTracer = require('../../lib/tracing').initTracer; |
| 5 | +const request = require('request-promise'); |
| 6 | +const { Tags, FORMAT_HTTP_HEADERS } = require('opentracing'); |
| 7 | + |
| 8 | +function sayHello(helloTo) { |
| 9 | + |
| 10 | + const span = tracer.startSpan('say-hello'); |
| 11 | + span.setTag('hello-to', helloTo); |
| 12 | + |
| 13 | + format_string(helloTo, span) |
| 14 | + .then( data => { |
| 15 | + return print_hello(data, span); |
| 16 | + }) |
| 17 | + .then( data => { |
| 18 | + span.setTag(Tags.HTTP_STATUS_CODE, 200) |
| 19 | + span.finish(); |
| 20 | + }) |
| 21 | + .catch( err => { |
| 22 | + span.setTag(Tags.ERROR, true) |
| 23 | + span.setTag(Tags.HTTP_STATUS_CODE, err.statusCode || 500); |
| 24 | + span.finish(); |
| 25 | + }); |
| 26 | + |
| 27 | +} |
| 28 | + |
| 29 | +function format_string(input, root_span) { |
| 30 | + const url = `http://localhost:8081/format?helloTo=${input}`; |
| 31 | + const fn = 'format'; |
| 32 | + |
| 33 | + const span = tracer.startSpan(fn, {childOf: root_span.context()}); |
| 34 | + span.log({ |
| 35 | + 'event': 'format-string', |
| 36 | + 'value': input |
| 37 | + }); |
| 38 | + |
| 39 | + return http_get(fn, url, span); |
| 40 | +} |
| 41 | + |
| 42 | +function print_hello(input, root_span) { |
| 43 | + const url = `http://localhost:8082/publish?helloStr=${input}`; |
| 44 | + const fn = 'publish'; |
| 45 | + |
| 46 | + const span = tracer.startSpan(fn, {childOf: root_span.context()}); |
| 47 | + span.log({ |
| 48 | + 'event': 'print-string', |
| 49 | + 'value': input |
| 50 | + }); |
| 51 | + return http_get(fn, url, span); |
| 52 | +} |
| 53 | + |
| 54 | +function http_get(fn, url, span) { |
| 55 | + const method = 'GET'; |
| 56 | + const headers = {}; |
| 57 | + |
| 58 | + span.setTag(Tags.HTTP_URL, url); |
| 59 | + span.setTag(Tags.HTTP_METHOD, method); |
| 60 | + span.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_RPC_CLIENT); |
| 61 | + // Send span context via request headers (parent id etc.) |
| 62 | + tracer.inject(span, FORMAT_HTTP_HEADERS, headers); |
| 63 | + |
| 64 | + return request({url, method, headers}) |
| 65 | + .then( data => { |
| 66 | + span.finish(); |
| 67 | + return data; |
| 68 | + }, e => { |
| 69 | + span.finish(); |
| 70 | + throw e; |
| 71 | + }); |
| 72 | + |
| 73 | +} |
| 74 | + |
| 75 | +assert.ok(process.argv.length == 3, 'expecting one argument'); |
| 76 | + |
| 77 | +const helloTo = process.argv[2]; |
| 78 | + |
| 79 | +const tracer = initTracer('hello-world'); |
| 80 | + |
| 81 | +sayHello(helloTo); |
| 82 | + |
| 83 | +setTimeout( e => {tracer.close();}, 12000); |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
0 commit comments