• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

telefonicaid / fiware-pep-steelskin / 10808276947

11 Sep 2024 08:50AM UTC coverage: 82.787%. Remained the same
10808276947

Pull #540

github

web-flow
Merge cd262b2b4 into 04b565d2c
Pull Request #540: Bump express from 4.19.2 to 4.20.0

265 of 349 branches covered (75.93%)

Branch coverage included in aggregate %.

745 of 871 relevant lines covered (85.53%)

52.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.13
/lib/middleware/logging.js
1
/*
2
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
3
 *
4
 * This file is part of fiware-pep-steelskin
5
 *
6
 * fiware-pep-steelskin is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU Affero General Public License as
8
 * published by the Free Software Foundation, either version 3 of the License,
9
 * or (at your option) any later version.
10
 *
11
 * fiware-pep-steelskin is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 * See the GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public
17
 * License along with fiware-pep-steelskin.
18
 * If not, seehttp://www.gnu.org/licenses/.
19
 *
20
 * For those usages not covered by the GNU Affero General Public License
21
 * please contact with::[iot_support@tid.es]
22
 */
23

24
'use strict';
25

26
var uuid = require('uuid'),
1✔
27
    constants = require('../constants');
28

29
var logger = require('logops');
1✔
30

31
/**
32
 * Express middleWare that creates a domain per request
33
 * It also generates a unique request id that can be used to track requests in logs.
34
 *
35
 * @return {Function} Express middleWare.
36
 */
37

38
function requestLogger(componentName) {
1✔
39
    return function(req, res, next) {
1✔
40
        let contextSrv;
175✔
41
        if (req.headers && req.headers[constants.ORGANIZATION_HEADER]) {
175✔
42
            contextSrv = req.headers[constants.ORGANIZATION_HEADER];
159✔
43
        }
44
        let contextSubsrv;
175✔
45
        if (req.headers && req.headers[constants.PATH_HEADER]) {
175✔
46
            contextSubsrv = req.headers[constants.PATH_HEADER];
157✔
47
        }
48
        let contextFrom;
175✔
49
        // x-forwarded-for/forwarded overwrites x-real-ip
50
        if (req.headers[constants.X_REAL_IP_HEADER]) {
175!
51
            contextFrom = req.headers[constants.X_REAL_IP_HEADER];
×
52
        }
53
        if (req.headers[constants.X_FORWARDED_FOR_HEADER]) {
175✔
54
            contextFrom = req.headers[constants.X_FORWARDED_FOR_HEADER];
3✔
55
        }
56
        if (req.headers[constants.FORWARDED_HEADER]) {
175!
57
            contextFrom = req.headers[constants.FORWARDED_HEADER];
×
58
        }
59
        let contextTrans = req.requestId = uuid.v4();
175✔
60
        let contextCorr  = req.get(constants.CORRELATOR_HEADER);
175✔
61
        if (!contextCorr) {
175!
62
            contextCorr = contextTrans;
175✔
63
        }
64
        req.corr = contextCorr; // for propagate in FWD request
175✔
65
        res.set(constants.CORRELATOR_HEADER, contextCorr); // for response
175✔
66
        const contextStart = Date.now();
175✔
67
        req.logger = logger.child({
175✔
68
            corr: contextCorr,
69
            trans: contextTrans,
70
            op: req.url,
71
            from: contextFrom,
72
            srv: contextSrv,
73
            subsrv: contextSubsrv,
74
            comp: componentName
75
        });
76
        res.once('finish', function() {
175✔
77
            const responseTime = Date.now() - contextStart;
175✔
78
            req.logger.debug('response-time: ' + responseTime + ' statusCode: ' + res.statusCode);
175✔
79
        });
80
        next();
175✔
81
    };
82
}
83

84
exports.requestLogger = requestLogger;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc