@moreapp/common-nodejs 0.3.0 → 0.4.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.
@@ -0,0 +1,3 @@
1
+ export * from "./tracer";
2
+ export * from "./logger";
3
+ export * from "./utils";
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./tracer"), exports);
18
+ __exportStar(require("./logger"), exports);
19
+ __exportStar(require("./utils"), exports);
@@ -0,0 +1 @@
1
+ export declare const logger: import("winston").Logger;
package/dist/logger.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.logger = void 0;
4
+ const winston_1 = require("winston");
5
+ const entry_1 = require("@google-cloud/logging/build/src/entry");
6
+ const stackdriverTracingFormat = (0, winston_1.format)((info) => {
7
+ if (info["trace_id"] && info["span_id"]) {
8
+ info[entry_1.TRACE_KEY] = info["trace_id"];
9
+ info[entry_1.SPAN_ID_KEY] = info["span_id"];
10
+ info[entry_1.TRACE_SAMPLED_KEY] = true;
11
+ }
12
+ return info;
13
+ });
14
+ exports.logger = (0, winston_1.createLogger)({
15
+ level: "info",
16
+ transports: [new winston_1.transports.Console()],
17
+ format: winston_1.format.combine(stackdriverTracingFormat(), winston_1.format.json()),
18
+ });
@@ -0,0 +1,2 @@
1
+ import * as types from "@opentelemetry/instrumentation/build/src/types";
2
+ export declare const tracer: (serviceName: string, instrumentations: types.Instrumentation[]) => void;
package/dist/tracer.js ADDED
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.tracer = void 0;
4
+ const opentelemetry_cloud_trace_exporter_1 = require("@google-cloud/opentelemetry-cloud-trace-exporter");
5
+ const instrumentation_1 = require("@opentelemetry/instrumentation");
6
+ const instrumentation_dns_1 = require("@opentelemetry/instrumentation-dns");
7
+ const instrumentation_http_1 = require("@opentelemetry/instrumentation-http");
8
+ const instrumentation_express_1 = require("@opentelemetry/instrumentation-express");
9
+ const instrumentation_winston_1 = require("@opentelemetry/instrumentation-winston");
10
+ const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
11
+ const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
12
+ const resources_1 = require("@opentelemetry/resources");
13
+ const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
14
+ const propagator_b3_1 = require("@opentelemetry/propagator-b3");
15
+ const utils_1 = require("./utils");
16
+ const tracer = (serviceName, instrumentations) => {
17
+ const provider = new sdk_trace_node_1.NodeTracerProvider({
18
+ resource: new resources_1.Resource({
19
+ [semantic_conventions_1.SemanticResourceAttributes.SERVICE_NAME]: serviceName,
20
+ [semantic_conventions_1.SemanticResourceAttributes.SERVICE_INSTANCE_ID]: (0, utils_1.environmentVariable)("HOSTNAME", "local"),
21
+ }),
22
+ });
23
+ provider.register({
24
+ propagator: new propagator_b3_1.B3Propagator({
25
+ injectEncoding: propagator_b3_1.B3InjectEncoding.MULTI_HEADER,
26
+ }),
27
+ });
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
+ ...instrumentations,
40
+ ],
41
+ });
42
+ if (process.env["STACKDRIVER_TRACING_ENABLED"] === "true") {
43
+ const exporter = new opentelemetry_cloud_trace_exporter_1.TraceExporter({
44
+ resourceFilter: /^service\./,
45
+ });
46
+ provider.addSpanProcessor(new sdk_trace_base_1.BatchSpanProcessor(exporter));
47
+ }
48
+ };
49
+ exports.tracer = tracer;
@@ -0,0 +1,2 @@
1
+ export declare function environmentVariable(key: string, fallback?: string | number): string;
2
+ export declare function currentTraceId(): string | undefined;
package/dist/utils.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.currentTraceId = exports.environmentVariable = void 0;
4
+ const context_utils_1 = require("@opentelemetry/api/build/src/trace/context-utils");
5
+ const api_1 = require("@opentelemetry/api");
6
+ function environmentVariable(key, fallback) {
7
+ const value = process.env[key] || fallback;
8
+ if (value) {
9
+ return value.toString();
10
+ }
11
+ throw new Error(`Missing environment variable '${key}'`);
12
+ }
13
+ exports.environmentVariable = environmentVariable;
14
+ function currentTraceId() {
15
+ return (0, context_utils_1.getSpanContext)(api_1.context.active())?.traceId;
16
+ }
17
+ exports.currentTraceId = currentTraceId;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("./utils");
4
+ describe("environmentVariable", () => {
5
+ test("Should throw when environment variable does not exist", async () => {
6
+ process.env["COMMONS_NODEJS_TEST_EXISTING"] = "SOME_VALUE";
7
+ expect((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_EXISTING")).toBe("SOME_VALUE");
8
+ });
9
+ test("Use fallback value when environment variable does not exist", async () => {
10
+ expect((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_NON_EXISTING", "DEFAULT_VALUE")).toBe("DEFAULT_VALUE");
11
+ });
12
+ test("Should throw when environment variable does not exist", async () => {
13
+ expect(() => (0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_NON_EXISTING")).toThrow("Missing environment variable 'COMMONS_NODEJS_TEST_NON_EXISTING'");
14
+ });
15
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moreapp/common-nodejs",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "license": "UNLICENSED",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",