UNPKG

3.47 kBJavaScriptView Raw
1/*
2 * Copyright 2018 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12const _ = require('lodash/fp');
13const winston = require('winston');
14const Loggly = require('./transport');
15
16/* eslint-disable no-underscore-dangle */
17
18/**
19 * Returns true when k is likely the name of disclosable key (i.e. not in ALL_CAPS)
20 * @param {String} k
21 */
22function disclosable(v, k) {
23 return !k.match(/^[A-Z0-9_]+$/);
24}
25
26function loglevel(p = {}) {
27 if (p.__ow_headers) {
28 // openwhisk transforms all headers to lowercase
29 if (p.__ow_headers['x-debug']) {
30 // use the log level set in the `X-Debug` header
31 return p.__ow_headers['x-debug'];
32 }
33 }
34 return 'debug';
35}
36
37function activationid() {
38 return process.env.__OW_ACTIVATION_ID
39 ? process.env.__OW_ACTIVATION_ID
40 : 'debug';
41}
42
43function functionname() {
44 return process.env.__OW_ACTION_NAME
45 ? process.env.__OW_ACTION_NAME.replace(/\/.+\//, '')
46 : 'debug';
47}
48
49function requestid(p = {}) {
50 if (p.__ow_headers) {
51 // openwhisk transforms all headers to lowercase
52 if (p.__ow_headers['x-cdn-request-id']) {
53 return p.__ow_headers['x-cdn-request-id'];
54 }
55 }
56 return 'debug';
57}
58
59function defaultlogger(p = {}) {
60 const token = p.LOGGLY_KEY;
61 const subdomain = p.LOGGLY_HOST;
62 const logger = winston.createLogger({
63 level: loglevel(p),
64 });
65 if (token && subdomain) {
66 const myloggly = new Loggly({
67 token,
68 subdomain,
69 tags: ['OpenWhisk', functionname(), activationid()],
70 json: true,
71 });
72 logger.add(myloggly);
73 } else {
74 logger.add(new winston.transports.Console({
75 json: false,
76 format: winston.format.simple(),
77 }));
78 }
79 return logger;
80}
81
82/**
83 * Wraps a function with proper logging, before and after.
84 * @param fn The openwhisk action to wrap
85 * @param params the action params
86 * @returns {*} the return value of the action
87 */
88function wrap(fn, params = {}) {
89 if (!params.__ow_logger) {
90 // eslint-disable-next-line no-param-reassign
91 params.__ow_logger = defaultlogger(params);
92 }
93 const disclosableParams = _.pickBy(disclosable, params);
94 const logger = params.__ow_logger;
95
96 logger.log('silly', 'before', {
97 params,
98 request: requestid(disclosableParams),
99 activation: activationid(),
100 function: functionname(),
101 });
102 try {
103 return Promise.resolve(fn(params))
104 .then((r) => {
105 logger.silly('resolved', {
106 result: r,
107 request: requestid(disclosableParams),
108 activation: activationid(),
109 function: functionname(),
110 });
111 return r;
112 })
113 .catch((e) => {
114 logger.log('debug', 'error', {
115 error: e,
116 request: requestid(disclosableParams),
117 activation: activationid(),
118 function: functionname(),
119 });
120 return e;
121 });
122 } catch (e) {
123 logger.error(e.stack);
124 return { error: e };
125 }
126}
127
128module.exports = wrap;