UNPKG

3.25 kBJavaScriptView Raw
1"use strict";
2// Copyright 2019 Google LLC
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// http://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.
15Object.defineProperty(exports, "__esModule", { value: true });
16exports.getDefaultMetadataForTracing = void 0;
17const common_1 = require("./common");
18/**
19 * Returns an object that can be passed to Winston.createLogger as defaultMeta
20 * to allow log-trace correlation with Winston 3. Log-trace correlation with
21 * Winston 3 is broken because the trace ID to be correlated with a log isn't
22 * evaluated when the log function is called, but rather when the log is
23 * written, which happens at some future point where the trace ID may no longer
24 * be accurate. To circumvent this, we take advantage of the fact that
25 * defaultMeta is copied when a log function is called, and use a dynamic
26 * property getter to evaluate the trace ID upon that copy.
27 *
28 * We apply the same principle for timestamps, which is not strictly necessary
29 * for tracing but allows for more accurate timestamps in general.
30 *
31 * If there are other default metadata fields with which the return value of
32 * this function must be merged, this object MUST be the base object. In other
33 * words, do not use the return value of this function as the non-first argument
34 * to Object.assign, or it will not work.
35 *
36 * See https://github.com/googleapis/nodejs-logging-winston/issues/287 for
37 * more information.
38 */
39function getDefaultMetadataForTracing() {
40 const agent = global._google_trace_agent;
41 // Enable log-trace correlation if the Trace Agent API is compatible.
42 const enableThunkAgent = !!(agent &&
43 agent.getCurrentContextId &&
44 agent.getWriterProjectId);
45 const defaultMeta = {};
46 // Make defaultMeta.timestamp return the current timestamp any time it's
47 // accessed.
48 Object.defineProperty(defaultMeta, 'timestamp', {
49 enumerable: true,
50 get: () => new Date(),
51 });
52 if (enableThunkAgent) {
53 // Make defaultMeta[LOGGING_TRACE_KEY] return the current trace ID any time
54 // it's accessed.
55 const loggingTraceKey = common_1.LOGGING_TRACE_KEY;
56 Object.defineProperty(defaultMeta, loggingTraceKey, {
57 enumerable: true,
58 get: () => {
59 const traceId = agent.getCurrentContextId();
60 if (!traceId) {
61 return null;
62 }
63 const traceProjectId = agent.getWriterProjectId();
64 if (!traceProjectId) {
65 return null;
66 }
67 return `projects/${traceProjectId}/traces/${traceId}`;
68 },
69 });
70 }
71 return defaultMeta;
72}
73exports.getDefaultMetadataForTracing = getDefaultMetadataForTracing;
74//# sourceMappingURL=default-metadata.js.map
\No newline at end of file