祖鲁代理请求的日志记录响应时间

问题描述:

我们想要捕获由Zuul代理服务器处理的用于性能监视的每个请求的响应时间。 Zuul过滤器似乎是这样做的合乎逻辑的方法,但考虑到Zuul过滤器的结构,似乎并不是一种简单的捕获流逝时间的方法。 任何人都可以阐明我们如何实现这一目标?祖鲁代理请求的日志记录响应时间

我也努力与Zuul过滤器来获得基本的指标。

Spring Actuator trace给出了很多信息。简单地用 “/跟踪” 来查看:

  • 时间戳
  • timeTaken
  • HTTP状态代码

这是知府Zuul

[ 
    { 
     "timestamp": 1499429507097, 
     "info": { 
      "method": "GET", 
      "path": "/index.html", 
      "headers": { 
       "request": { 
        "host": "localhost:8765", 
        "connection": "keep-alive", 
        "upgrade-insecure-requests": "1", 
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/58.0.3029.110 Chrome/58.0.3029.110 Safari/537.36", 
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 
        "accept-encoding": "gzip, deflate, sdch, br", 
        "accept-language": "en-US,en;q=0.8", 
        "cookie": "zbx_sessionid=02ac46186474dbd668d7abe0f78705c9" 
       }, 
       "response": { 
        "X-Application-Context": "application:8765", 
        "Last-Modified": "Fri, 07 Jul 2017 10:52:42 GMT", 
        "Cache-Control": "no-store", 
        "Accept-Ranges": "bytes", 
        "Content-Type": "text/html", 
        "Content-Length": "446", 
        "Date": "Fri, 07 Jul 2017 12:11:47 GMT", 
        "status": "200" 
       } 
      }, 
      "timeTaken": "3" 
     } 
    }, 

但是,标准InMemoryTraceRepository给了我关于“/健康”甚至“/跟踪”的统计数据,但我只对POSTS感兴趣去Zuul。所以我只是扩展了InMemoryTraceRepository并将其作为@Bean提供。

import org.springframework.boot.actuate.trace.InMemoryTraceRepository; 
import org.springframework.boot.actuate.trace.Trace; 

public class ZuulInMemoryTraceRepository extends InMemoryTraceRepository { 

    public ZuulInMemoryTraceRepository(int maxSize) { 
    setCapacity(maxSize); 
    } 

    public Collection<DateTimeTaken> getStats(){ 
    return fifo; 
    } 

    public void setReverse(boolean reverse) { 
    super.setReverse(reverse); 
    } 

    @Override 
    public void setCapacity(int capacity) { 
    super.setCapacity(capacity); 
    } 

    @Override 
    public List<Trace> findAll() { 
    return super.findAll(); 
    } 

    @Override 
    public void add(Map<String, Object> map) { 

    Object method = map.get("method"); 
    if (!"POST".equals(method)) { 
     return; 
    } 
    Object timeTaken = map.get("timeTaken");//timeTaken is a String 
    //log timeTaken 
    super.add(map); 
    } 
} 

在我@Configuration我说:

@Bean 
public ZuulInMemoryTraceRepository myZuulInMemoryTraceRepository() { 
    return new ZuulInMemoryTraceRepository(10000); 
} 

另外,您可以自定义对象添加到ZuulInMemoryTraceRepository一个固定大小的EvictingQueue。最后公开一个平静的Web服务来暴露像我的getStats()这样的EvictingQueue。这个json可以用来绘制一个漂亮的响应时间图。

@Override 
public void add(Map<String, Object> map) { 
    super.add(map); 
    Object method = map.get("method"); 
    if (!"POST".equals(method)) { 
    return; 
    } 
    Object timeTaken = map.get("timeTaken");//timeTaken is a String 
    if (timeTaken != null) { 
    synchronized (fifo) { 
     //Make your own DateTimeTaken 
     fifo.add(new DateTimeTaken(new Date(), timeTaken.toString())); 
    } 
    } 
} 

在我的统计@RestController:

@Autowired 
ZuulInMemoryTraceRepository zuulInMemoryTraceRepository; 

@RequestMapping("/stats") 
public Collection<DateTimeTaken> getAll() { 
    return zuulInMemoryTraceRepository.getStats(); 
}