2017-03-04 54 views
1

我們想要捕獲由Zuul代理服務器處理的用於性能監視的每個請求的響應時間。 Zuul過濾器似乎是這樣做的合乎邏輯的方法,但考慮到Zuul過濾器的結構,似乎並不是一種簡單的捕獲流逝時間的方法。 任何人都可以闡明我們如何實現這一目標?祖魯代理請求的日誌記錄響應時間

回答

0

我也努力與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(); 
}