@moreapp/common-nodejs 0.5.0 → 0.6.0

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/README.md CHANGED
@@ -5,3 +5,17 @@ A collection of shared NodeJS code.
5
5
  # Pre-commit hooks
6
6
 
7
7
  Run `yarn prepare` once to install Git hooks, doing lints and prettier formatting
8
+
9
+ ## Usage
10
+
11
+ ### Tracing
12
+
13
+ Tracing code should always be loaded first, before any other libraries. This is because the OpenTelemetry
14
+ instrumentations monkey patch libraries to add tracing. This will break if the library has been loaded (import/require),
15
+ because the unpatched library will be cached.
16
+
17
+ Put the following snippet on the first line of the entrypoint file.
18
+
19
+ ```
20
+ require("@moreapp/common-nodejs/dist/tracer").tracer(<service_name>, {});
21
+ ```
package/dist/tracer.d.ts CHANGED
@@ -1,2 +1,8 @@
1
1
  import * as types from "@opentelemetry/instrumentation/build/src/types";
2
- export declare const tracer: (serviceName: string, extraInstrumentations: types.Instrumentation[]) => void;
2
+ export declare const tracer: (serviceName: string, { http, extraInstrumentations, debug, }: {
3
+ http: {
4
+ portsToInstrument: number[];
5
+ };
6
+ extraInstrumentations: types.Instrumentation[];
7
+ debug: boolean;
8
+ }) => void;
package/dist/tracer.js CHANGED
@@ -1,19 +1,35 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.tracer = void 0;
4
+ const api_1 = require("@opentelemetry/api");
5
+ const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
4
6
  const opentelemetry_cloud_trace_exporter_1 = require("@google-cloud/opentelemetry-cloud-trace-exporter");
5
7
  const instrumentation_1 = require("@opentelemetry/instrumentation");
6
8
  const instrumentation_dns_1 = require("@opentelemetry/instrumentation-dns");
7
9
  const instrumentation_http_1 = require("@opentelemetry/instrumentation-http");
8
10
  const instrumentation_express_1 = require("@opentelemetry/instrumentation-express");
9
11
  const instrumentation_winston_1 = require("@opentelemetry/instrumentation-winston");
10
- const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
11
12
  const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
12
13
  const resources_1 = require("@opentelemetry/resources");
13
14
  const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
14
15
  const propagator_b3_1 = require("@opentelemetry/propagator-b3");
15
16
  const utils_1 = require("./utils");
16
- const tracer = (serviceName, extraInstrumentations) => {
17
+ const tracer = (serviceName, { http = {
18
+ portsToInstrument: [3000],
19
+ }, extraInstrumentations = [], debug = false, }) => {
20
+ (0, instrumentation_1.registerInstrumentations)({
21
+ instrumentations: [
22
+ new instrumentation_dns_1.DnsInstrumentation(),
23
+ new instrumentation_http_1.HttpInstrumentation({
24
+ ignoreIncomingRequestHook: (req) => {
25
+ return !http.portsToInstrument.includes(req.socket.localPort || 0);
26
+ },
27
+ }),
28
+ new instrumentation_express_1.ExpressInstrumentation(),
29
+ new instrumentation_winston_1.WinstonInstrumentation(),
30
+ ...(extraInstrumentations || []),
31
+ ],
32
+ });
17
33
  const provider = new sdk_trace_node_1.NodeTracerProvider({
18
34
  resource: new resources_1.Resource({
19
35
  [semantic_conventions_1.SemanticResourceAttributes.SERVICE_NAME]: serviceName,
@@ -25,25 +41,15 @@ const tracer = (serviceName, extraInstrumentations) => {
25
41
  injectEncoding: propagator_b3_1.B3InjectEncoding.MULTI_HEADER,
26
42
  }),
27
43
  });
28
- (0, instrumentation_1.registerInstrumentations)({
29
- instrumentations: [
30
- new instrumentation_dns_1.DnsInstrumentation(),
31
- new instrumentation_http_1.HttpInstrumentation({
32
- ignoreIncomingRequestHook: (_req) => {
33
- // Disable instrumentation for incoming requests, handled by ExpressInstrumentation below
34
- return true;
35
- },
36
- }),
37
- new instrumentation_express_1.ExpressInstrumentation(),
38
- new instrumentation_winston_1.WinstonInstrumentation(),
39
- ...extraInstrumentations,
40
- ],
41
- });
42
44
  if (process.env["STACKDRIVER_TRACING_ENABLED"] === "true") {
43
45
  const exporter = new opentelemetry_cloud_trace_exporter_1.TraceExporter({
44
46
  resourceFilter: /^service\./,
45
47
  });
46
48
  provider.addSpanProcessor(new sdk_trace_base_1.BatchSpanProcessor(exporter));
47
49
  }
50
+ if (debug) {
51
+ api_1.diag.setLogger(new api_1.DiagConsoleLogger(), api_1.DiagLogLevel.DEBUG);
52
+ provider.addSpanProcessor(new sdk_trace_base_1.BatchSpanProcessor(new sdk_trace_node_1.ConsoleSpanExporter()));
53
+ }
48
54
  };
49
55
  exports.tracer = tracer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moreapp/common-nodejs",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "license": "UNLICENSED",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",