Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

----------- parentScope null #383

Closed
openainext opened this issue Nov 19, 2018 · 5 comments · Fixed by #384
Closed

----------- parentScope null #383

openainext opened this issue Nov 19, 2018 · 5 comments · Fixed by #384
Labels
bug Something isn't working community integrations

Comments

@openainext
Copy link

const parentScope = tracer.scopeManager().active()

----------- parentScope null

@rochdev
Copy link
Member

rochdev commented Nov 19, 2018

Please provide a more coherent description of the issue.

@openainext
Copy link
Author

openainext commented Nov 19, 2018

var RateLimit = require('express-rate-limit');
var RedisStore = require('rate-limit-redis');
var config = require('../config/config');
var ms = require('ms');
var utility = require('../common/utility');


function getRateLimit(max,expiry) {
  return {
    store: new RedisStore({
      expiry:expiry||60,
      client: config.redisClient
    }), //https://www.npmjs.com/package/express-rate-limit
    max: max,  
    keyGenerator:function (req) { 
      var key=utility.getClientIp(req)+req.path;
      return key;
    },
    headers:false,
    handler:function (req,res) {
      var ip=utility.getClientIp(req);
      config.report('请求过于频繁',{
        level:'debug',
        extra:{ip:ip}
      })
      return res.errorMessage('请求频繁 请稍后重试!');
    },
    delayAfter:0,
    delayMs: 0  
  };
}
var xxs = new RateLimit(getRateLimit(10));//0.5qps
var xs = new RateLimit(getRateLimit(30));//0.5qps
var sm = new RateLimit(getRateLimit(600));//10qps
var lg = new RateLimit(getRateLimit(6000));//100qps
module.exports = {
  xxs:xxs,
  xs:xs,
  sm:sm,
  lg:lg
}



router.post('/getCodeRs',limiter.sm, function(req, res, next) {

 node xhr 
  function makeRequestTrace (request) {
    return function requestTrace () {
      const args = normalizeArgs.apply(null, arguments)
      const uri = args.uri
      const options = args.options
      const callback = args.callback


      console.log('uri--------------------',uri);
      if (!config.filter(uri)) {
        return request.call(this, options, callback)
      }

      const method = (options.method || 'GET').toUpperCase()

      const parentScope = tracer.scopeManager().active()

----------- parentScope null

elastic-apm-node    pgk   normal

})


@rochdev
Copy link
Member

rochdev commented Nov 19, 2018

Thanks for the snippet. I think I understand a bit better what the problem is. Could you provide a minimal runnable snippet of code that reproduces the problem? I will use that to set up a test case and hopefully provide a fix.

Also, which versions of dd-trace, express-rate-limit and rate-limit-redis are you using?

@openainext
Copy link
Author

package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "saas account and proxy api",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.13.3",
    "compression": "^1.5.2",
    "concat-stream": "^1.5.1",
    "connect-redis": "^3.0.1",
    "cookie-parser": "^1.3.3",
    "dd-trace": "^0.7.0",
    "express": "^4.13.3",
    "express-rate-limit": "^2.9.0",
    "got": "^9.3.2",
    "rate-limit-redis": "^1.1.0",
    "redis": "^2.6.2"
  }
}

test.js

const tracer = require('dd-trace').init()
var express = require('express');
var RateLimit = require('express-rate-limit');
var RedisStore = require('rate-limit-redis');
var redis = require('redis');

var redisOpt= {
  port: 6379,
  host: "172.1.7.7",
  pass: '123123'
}
var redisClient = redis.createClient(redisOpt);

redisClient.auth(redisOpt.pass, function() {
  console.log('Redis client connected');
});
redisClient.on("error", function(err) {
  console.log("Error " + err);
});
const got = require('got');

var app = express();


function getRateLimit(max,expiry) {
  return {
    store: new RedisStore({
      expiry:expiry||60,
      client: redisClient
    }), //https://www.npmjs.com/package/express-rate-limit
    max: max, 
    keyGenerator:function (req) {
      var key=getClientIp(req)+req.path;
      return key;
    },
    headers:false,
    handler:function (req,res) {
      var ip=getClientIp(req);
      return res.send('Request too frequently!');
    },
    delayAfter:0,
    delayMs: 0 // disable delaying - full speed until the max limit is reached
  };
}
var xxs = new RateLimit(getRateLimit(10));//0.5qps
var xs = new RateLimit(getRateLimit(30));//0.5qps


function getClientIp(req) {
  var ipAddress;
  var forwardedIpsStr = req.header('x-forwarded-for');
  if (forwardedIpsStr) {
    var forwardedIps = forwardedIpsStr.split(',');
    ipAddress = forwardedIps[0];
  }
  if (!ipAddress) {
    ipAddress = req.connection.remoteAddress;
  }
  return ipAddress;
}


app.get('/',xs, async function(req, res) {  //xs delete normal 

  try {
//     const parentScope = tracer.scopeManager().active()
//
// ----------- parentScope null
    const response = await got('http://localhost:7070/ok');
    res.send(response.body)
  } catch (error) {
    console.log('got-----------------------',error.response.body);
    res.send(error)
  }
});

app.get('/ok', function(req, res) {
 res.send("ok")
});

app.listen(7070, function() {
    console.log('Example app listening on port 7070!');
  });

@rochdev
Copy link
Member

rochdev commented Nov 19, 2018

Thanks for the snippet! I'll take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working community integrations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants