@uoa/lambda-tracing 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/tracing.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Tracer } from "@opentelemetry/api";
1
2
  export declare function initializeTracing(serviceName: string): void;
3
+ export declare function getTracer(): Tracer;
2
4
  export declare function getTraceInfoHeader(): string;
3
5
  export declare function setTraceInfoHeader(info: string): void;
package/dist/tracing.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.setTraceInfoHeader = exports.getTraceInfoHeader = exports.initializeTracing = void 0;
3
+ exports.setTraceInfoHeader = exports.getTraceInfoHeader = exports.getTracer = exports.initializeTracing = void 0;
4
4
  const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
5
5
  const instrumentation_aws_lambda_1 = require("@opentelemetry/instrumentation-aws-lambda");
6
6
  const instrumentation_1 = require("@opentelemetry/instrumentation");
@@ -10,6 +10,8 @@ const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
10
10
  const zipkin_1 = require("./zipkin/zipkin");
11
11
  const provider = new sdk_trace_node_1.NodeTracerProvider({ sampler: new sdk_trace_base_1.AlwaysOnSampler() });
12
12
  let infoHeader;
13
+ let tracer;
14
+ let name;
13
15
  function initializeTracing(serviceName) {
14
16
  const options = {
15
17
  url: process.env.zipkinUrl ? process.env.zipkinUrl : 'http://internal-zipkin-uoa-its-nonprod-internal-1407434909.ap-southeast-2.elb.amazonaws.com:443/api/v2/spans',
@@ -36,8 +38,19 @@ function initializeTracing(serviceName) {
36
38
  })
37
39
  ]
38
40
  });
41
+ name = serviceName;
42
+ tracer = api_1.trace.getTracer(serviceName);
39
43
  }
40
44
  exports.initializeTracing = initializeTracing;
45
+ function getTracer() {
46
+ if (tracer) {
47
+ return tracer;
48
+ }
49
+ else {
50
+ return api_1.trace.getTracer(name);
51
+ }
52
+ }
53
+ exports.getTracer = getTracer;
41
54
  function getTraceInfoHeader() {
42
55
  if (infoHeader) {
43
56
  return infoHeader;
package/dist/uoaHttps.js CHANGED
@@ -34,6 +34,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
35
  const https = __importStar(require("https"));
36
36
  const api_1 = require("@opentelemetry/api");
37
+ const tracing_1 = require("./tracing");
37
38
  function request(...args) {
38
39
  if (args[2]) {
39
40
  api_1.propagation.inject(api_1.context.active(), args[1].headers);
@@ -62,7 +63,12 @@ function doGetRequest(hostname, path, headers, port = 443) {
62
63
  });
63
64
  response.on("end", function () {
64
65
  let body = Buffer.concat(chunks);
65
- body = JSON.parse(body.toString());
66
+ if (options.headers['Content-Type'] === 'application/json') {
67
+ body = JSON.parse(body.toString());
68
+ }
69
+ else {
70
+ body = body.toString();
71
+ }
66
72
  resolve(body);
67
73
  });
68
74
  response.on("error", function (e) {
@@ -73,6 +79,15 @@ function doGetRequest(hostname, path, headers, port = 443) {
73
79
  });
74
80
  });
75
81
  }
82
+ function doGetRequestExternal(hostname, path, headers, port = 443) {
83
+ return __awaiter(this, void 0, void 0, function* () {
84
+ return (0, tracing_1.getTracer)().startActiveSpan(hostname + path, (span) => __awaiter(this, void 0, void 0, function* () {
85
+ const result = yield doGetRequest(hostname, path, headers, port);
86
+ span.end();
87
+ return result;
88
+ }));
89
+ });
90
+ }
76
91
  function doPostRequest(hostname, path, headers, data, port = 443) {
77
92
  return __awaiter(this, void 0, void 0, function* () {
78
93
  return new Promise(function (resolve, reject) {
@@ -123,6 +138,15 @@ function doPostRequest(hostname, path, headers, data, port = 443) {
123
138
  });
124
139
  });
125
140
  }
141
+ function doPostRequestExternal(hostname, path, headers, data, port = 443) {
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ return (0, tracing_1.getTracer)().startActiveSpan(hostname + path, (span) => __awaiter(this, void 0, void 0, function* () {
144
+ const result = yield doPostRequest(hostname, path, headers, port);
145
+ span.end();
146
+ return result;
147
+ }));
148
+ });
149
+ }
126
150
  function doPutRequest(hostname, path, headers, data, port = 443) {
127
151
  return __awaiter(this, void 0, void 0, function* () {
128
152
  return new Promise(function (resolve, reject) {
@@ -173,6 +197,15 @@ function doPutRequest(hostname, path, headers, data, port = 443) {
173
197
  });
174
198
  });
175
199
  }
200
+ function doPutRequestExternal(hostname, path, headers, data, port = 443) {
201
+ return __awaiter(this, void 0, void 0, function* () {
202
+ return (0, tracing_1.getTracer)().startActiveSpan(hostname + path, (span) => __awaiter(this, void 0, void 0, function* () {
203
+ const result = yield doPutRequest(hostname, path, headers, port);
204
+ span.end();
205
+ return result;
206
+ }));
207
+ });
208
+ }
176
209
  function doDeleteRequest(hostname, path, headers, port = 443) {
177
210
  return __awaiter(this, void 0, void 0, function* () {
178
211
  return new Promise(function (resolve, reject) {
@@ -213,10 +246,23 @@ function doDeleteRequest(hostname, path, headers, port = 443) {
213
246
  });
214
247
  });
215
248
  }
249
+ function doDeleteRequestExternal(hostname, path, headers, data, port = 443) {
250
+ return __awaiter(this, void 0, void 0, function* () {
251
+ return (0, tracing_1.getTracer)().startActiveSpan(hostname + path, (span) => __awaiter(this, void 0, void 0, function* () {
252
+ const result = yield doDeleteRequest(hostname, path, headers, port);
253
+ span.end();
254
+ return result;
255
+ }));
256
+ });
257
+ }
216
258
  module.exports = {
217
259
  request,
218
260
  doGetRequest,
219
261
  doPostRequest,
220
262
  doPutRequest,
221
- doDeleteRequest
263
+ doDeleteRequest,
264
+ doGetRequestExternal,
265
+ doPostRequestExternal,
266
+ doPutRequestExternal,
267
+ doDeleteRequestExternal
222
268
  };
package/package.json CHANGED
@@ -1,43 +1,43 @@
1
- {
2
- "name": "@uoa/lambda-tracing",
3
- "version": "2.0.1",
4
- "description": "Library for logging & distributed tracing in UoA Lambda projects",
5
- "repository": {
6
- "type": "git",
7
- "url": "git+ssh://git@bitbucket.org/uoa/lambda-tracing.git"
8
- },
9
- "author": "Mitchell Faulconbridge <mitchell.faulconbridge@auckland.ac.nz>",
10
- "license": "MIT",
11
- "homepage": "https://bitbucket.org/uoa/lambda-tracing#readme",
12
- "keywords": [
13
- "uoa",
14
- "lambda",
15
- "logging",
16
- "distributed-tracing"
17
- ],
18
- "main": "dist/tracing.js",
19
- "types": "dist/tracing.d.ts",
20
- "scripts": {
21
- "test": "echo \"Error: no test specified\" && exit 1",
22
- "prepare": "tsc"
23
- },
24
- "exports": {
25
- ".": "./dist/tracing.js",
26
- "./tracing": "./dist/tracing.js",
27
- "./uoaHttps": "./dist/uoaHttps.js",
28
- "./logging": "./dist/logging.js"
29
- },
30
- "dependencies": {
31
- "@opentelemetry/api": "^1.1.0",
32
- "@opentelemetry/core": "^1.8.0",
33
- "@opentelemetry/instrumentation": "^0.28.0",
34
- "@opentelemetry/instrumentation-aws-lambda": "^0.31.0",
35
- "@opentelemetry/sdk-trace-node": "^1.8.0",
36
- "moment": "^2.29.3",
37
- "winston": "^3.7.2"
38
- },
39
- "devDependencies": {
40
- "@types/node": "^17.0.35",
41
- "typescript": "^4.7.2"
42
- }
43
- }
1
+ {
2
+ "name": "@uoa/lambda-tracing",
3
+ "version": "2.0.2",
4
+ "description": "Library for logging & distributed tracing in UoA Lambda projects",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+ssh://git@bitbucket.org/uoa/lambda-tracing.git"
8
+ },
9
+ "author": "Mitchell Faulconbridge <mitchell.faulconbridge@auckland.ac.nz>",
10
+ "license": "MIT",
11
+ "homepage": "https://bitbucket.org/uoa/lambda-tracing#readme",
12
+ "keywords": [
13
+ "uoa",
14
+ "lambda",
15
+ "logging",
16
+ "distributed-tracing"
17
+ ],
18
+ "main": "dist/tracing.js",
19
+ "types": "dist/tracing.d.ts",
20
+ "scripts": {
21
+ "test": "echo \"Error: no test specified\" && exit 1",
22
+ "prepare": "tsc"
23
+ },
24
+ "exports": {
25
+ ".": "./dist/tracing.js",
26
+ "./tracing": "./dist/tracing.js",
27
+ "./uoaHttps": "./dist/uoaHttps.js",
28
+ "./logging": "./dist/logging.js"
29
+ },
30
+ "dependencies": {
31
+ "@opentelemetry/api": "^1.1.0",
32
+ "@opentelemetry/core": "^1.8.0",
33
+ "@opentelemetry/instrumentation": "^0.28.0",
34
+ "@opentelemetry/instrumentation-aws-lambda": "^0.31.0",
35
+ "@opentelemetry/sdk-trace-node": "^1.8.0",
36
+ "moment": "^2.29.3",
37
+ "winston": "^3.7.2"
38
+ },
39
+ "devDependencies": {
40
+ "@types/node": "^17.0.35",
41
+ "typescript": "^4.7.2"
42
+ }
43
+ }
package/tracing.ts CHANGED
@@ -2,12 +2,14 @@ import {NodeTracerProvider, BatchSpanProcessor} from '@opentelemetry/sdk-trace-n
2
2
  import {AwsLambdaInstrumentation} from '@opentelemetry/instrumentation-aws-lambda';
3
3
  import {registerInstrumentations} from '@opentelemetry/instrumentation';
4
4
  import {B3_INFO_KEY, UoaB3Propagator} from "./UoaB3Propagator";
5
- import {context} from "@opentelemetry/api";
5
+ import {context, trace, Tracer} from "@opentelemetry/api";
6
6
  import {AlwaysOnSampler} from "@opentelemetry/sdk-trace-base";
7
7
  import {ZipkinExporter} from "./zipkin/zipkin";
8
8
 
9
9
  const provider = new NodeTracerProvider({sampler: new AlwaysOnSampler()});
10
10
  let infoHeader: string | undefined;
11
+ let tracer: Tracer | undefined;
12
+ let name : string;
11
13
 
12
14
  export function initializeTracing(serviceName: string) {
13
15
  const options = {
@@ -35,7 +37,16 @@ export function initializeTracing(serviceName: string) {
35
37
  })
36
38
  ]
37
39
  })
40
+ name = serviceName;
41
+ tracer = trace.getTracer(serviceName);
42
+ }
38
43
 
44
+ export function getTracer(): Tracer {
45
+ if (tracer) {
46
+ return tracer;
47
+ } else {
48
+ return trace.getTracer(name);
49
+ }
39
50
  }
40
51
 
41
52
  export function getTraceInfoHeader(): string {
@@ -49,3 +60,4 @@ export function getTraceInfoHeader(): string {
49
60
  export function setTraceInfoHeader(info: string) {
50
61
  infoHeader = info;
51
62
  }
63
+
package/uoaHttps.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import * as https from "https";
2
2
  import * as http from "http";
3
3
  import { URL } from "url";
4
- import {context, propagation} from "@opentelemetry/api";
4
+ import {context, propagation, trace} from "@opentelemetry/api";
5
5
  import {RequestOptions} from "https";
6
+ import {getTracer} from "./tracing";
6
7
 
7
8
  function request(options: RequestOptions | string | URL, callback?: (res: http.IncomingMessage) => void): http.ClientRequest;
8
9
  function request(url: string | URL, options: RequestOptions, callback?: (res: http.IncomingMessage) => void): http.ClientRequest;
@@ -35,8 +36,12 @@ async function doGetRequest(hostname: string, path: string, headers: any, port:
35
36
  });
36
37
 
37
38
  response.on("end", function () {
38
- let body = Buffer.concat(chunks);
39
- body = JSON.parse(body.toString());
39
+ let body: any = Buffer.concat(chunks);
40
+ if (options.headers['Content-Type'] === 'application/json') {
41
+ body = JSON.parse(body.toString());
42
+ } else {
43
+ body = body.toString();
44
+ }
40
45
  resolve(body);
41
46
  });
42
47
 
@@ -46,6 +51,15 @@ async function doGetRequest(hostname: string, path: string, headers: any, port:
46
51
  });
47
52
 
48
53
  req.end();
54
+
55
+ });
56
+ }
57
+
58
+ async function doGetRequestExternal(hostname: string, path: string, headers: any, port: number | string = 443) {
59
+ return getTracer().startActiveSpan(hostname+path, async (span) => {
60
+ const result = await doGetRequest(hostname, path, headers, port);
61
+ span.end();
62
+ return result;
49
63
  });
50
64
  }
51
65
 
@@ -100,6 +114,14 @@ async function doPostRequest(hostname: string, path: string, headers: any, data:
100
114
  });
101
115
  }
102
116
 
117
+ async function doPostRequestExternal(hostname: string, path: string, headers: any, data: any, port: number | string = 443) {
118
+ return getTracer().startActiveSpan(hostname+path, async(span) => {
119
+ const result = await doPostRequest(hostname, path, headers, port);
120
+ span.end();
121
+ return result;
122
+ });
123
+ }
124
+
103
125
  async function doPutRequest(hostname: string, path: string, headers: any, data: any, port: number | string = 443) {
104
126
  return new Promise(function (resolve, reject) {
105
127
  const options = {
@@ -151,6 +173,14 @@ async function doPutRequest(hostname: string, path: string, headers: any, data:
151
173
  });
152
174
  }
153
175
 
176
+ async function doPutRequestExternal(hostname: string, path: string, headers: any, data: any, port: number | string = 443) {
177
+ return getTracer().startActiveSpan(hostname+path, async(span) => {
178
+ const result = await doPutRequest(hostname, path, headers, port);
179
+ span.end();
180
+ return result;
181
+ });
182
+ }
183
+
154
184
  async function doDeleteRequest(hostname: string, path: string, headers: any, port: number | string = 443) {
155
185
  return new Promise(function (resolve, reject) {
156
186
  const options = {
@@ -193,10 +223,23 @@ async function doDeleteRequest(hostname: string, path: string, headers: any, por
193
223
  });
194
224
  }
195
225
 
226
+ async function doDeleteRequestExternal(hostname: string, path: string, headers: any, data: any, port: number | string = 443) {
227
+ return getTracer().startActiveSpan(hostname+path, async (span) => {
228
+ const result = await doDeleteRequest(hostname, path, headers, port);
229
+ span.end();
230
+ return result;
231
+ });
232
+ }
233
+
196
234
  module.exports = {
197
235
  request,
198
236
  doGetRequest,
199
237
  doPostRequest,
200
238
  doPutRequest,
201
- doDeleteRequest
239
+ doDeleteRequest,
240
+ doGetRequestExternal,
241
+ doPostRequestExternal,
242
+ doPutRequestExternal,
243
+ doDeleteRequestExternal
202
244
  }
245
+