|
| 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 http = require('http'); |
| 19 | +const tracing = require('@opencensus/nodejs'); |
| 20 | +const { plugin } = require('@opencensus/instrumentation-http'); |
| 21 | +const { ZipkinTraceExporter } = require('@opencensus/exporter-zipkin'); |
| 22 | +const { TraceContextFormat } = require('@opencensus/propagation-tracecontext'); |
| 23 | + |
| 24 | +const tracer = setupTracerAndExporters(); |
| 25 | + |
| 26 | +/** Starts a HTTP server that receives requests on sample server port. */ |
| 27 | +function startServer (port) { |
| 28 | + // Creates a server |
| 29 | + const server = http.createServer(handleRequest); |
| 30 | + // Starts the server |
| 31 | + server.listen(port, err => { |
| 32 | + if (err) { |
| 33 | + throw err; |
| 34 | + } |
| 35 | + console.log(`Node HTTP listening on ${port}`); |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +/** A function which handles requests and send response. */ |
| 40 | +function handleRequest (request, response) { |
| 41 | + const span = tracer.startChildSpan('octutorials.handleRequest'); |
| 42 | + try { |
| 43 | + let body = []; |
| 44 | + request.on('error', err => console.log(err)); |
| 45 | + request.on('data', chunk => body.push(chunk)); |
| 46 | + request.on('end', () => { |
| 47 | + // deliberately sleeping to mock some action. |
| 48 | + setTimeout(() => { |
| 49 | + span.end(); |
| 50 | + response.end('Hello World!'); |
| 51 | + }, 5000); |
| 52 | + }); |
| 53 | + } catch (err) { |
| 54 | + console.log(err); |
| 55 | + span.end(); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function setupTracerAndExporters () { |
| 60 | + const zipkinOptions = { |
| 61 | + url: 'http://localhost:9411/api/v2/spans', |
| 62 | + serviceName: 'opencensus_tutorial' |
| 63 | + }; |
| 64 | + |
| 65 | + // Creates Zipkin exporter |
| 66 | + const exporter = new ZipkinTraceExporter(zipkinOptions); |
| 67 | + |
| 68 | + // Starts tracing and set sampling rate |
| 69 | + const tracer = tracing.registerExporter(exporter).start({ |
| 70 | + samplingRate: 1, // For demo purposes, always sample |
| 71 | + propagation: new TraceContextFormat() |
| 72 | + }).tracer; |
| 73 | + |
| 74 | + // Defines basedir and version |
| 75 | + const basedir = path.dirname(require.resolve('http')); |
| 76 | + const version = process.versions.node; |
| 77 | + |
| 78 | + // Enables HTTP plugin: Method that enables the instrumentation patch. |
| 79 | + plugin.enable(http, tracer, version, /** plugin options */{}, basedir); |
| 80 | + |
| 81 | + return tracer; |
| 82 | +} |
| 83 | + |
| 84 | +startServer(8080); |
0 commit comments