2016-10-11 59 views
0

我正在嘗試爲DropWizard指標創建一個自定義記者,但無法找到任何資源或示例來展示如何通過擴展ScheduledReporter來創建一個。我可以通過跟蹤現有的記者(CSV,SLF4J等)來擴展這個類,但是我在哪裏註冊?DropWizard Custom Reporter

這裏是習俗記者的摘錄,我嘗試註冊:

public class DbReporter extends ScheduledReporter { 
    private final Connection connection; 
    private final Clock clock; 

    public static DbReporter.Builder forRegistry(MetricRegistry registry) { 
     return new DbReporter.Builder(registry); 
    } 

    private DbReporter(MetricRegistry registry, Connection connection, Clock clock, MetricFilter filter, TimeUnit rateUnit, TimeUnit durationUnit) { 
     super(registry, "db-reporter", filter, rateUnit, durationUnit); 
     this.connection = connection; 
     this.clock = clock; 
    } 

    //... 

    public static class Builder { 
     private final MetricRegistry registry; 
     private Connection connection; 
     private TimeUnit rateUnit; 
     private TimeUnit durationUnit; 
     private MetricFilter filter; 
     private Clock clock; 

     private Builder(MetricRegistry registry) { 
      this.registry = registry; 
      this.connection = null; 
      this.rateUnit = TimeUnit.SECONDS; 
      this.durationUnit = TimeUnit.MILLISECONDS; 
      this.filter = MetricFilter.ALL; 
      this.clock = Clock.defaultClock(); 
     } 

     public DbReporter.Builder outputTo(Connection connection) { 
      this.connection = connection; 
      return this; 
     } 

     public DbReporter.Builder convertRatesTo(TimeUnit rateUnit) { 
      this.rateUnit = rateUnit; 
      return this; 
     } 

     public DbReporter.Builder convertDurationsTo(TimeUnit durationUnit) { 
      this.durationUnit = durationUnit; 
      return this; 
     } 

     public DbReporter.Builder filter(MetricFilter filter) { 
      this.filter = filter; 
      return this; 
     } 

     public DbReporter build() { 
      return new DbReporter(this.registry, this.connection, this.clock, this.filter, this.rateUnit, this.durationUnit); 
     } 
    } 
} 

而且,這是正確的方式來創建一個自定義的記者或者是有任何其他方式?

在此先感謝.-

+0

是的,這是正確的做法。你註冊他們完全相同的方式註冊其他的(見指標文檔) – pandaadb

+0

@pandaadb是的,但我沒有註冊任何東西。 –

回答

0

是的,你可以用同樣的方法實現它們。至於註冊,通常在DW的實例化Metrics之後,通常在主應用程序中完成。

例如,在您的run方法:

@Override 
    public void run(final Configuration configuration, 
        final Environment environment) { 
     log.info(String.format("Starting %s server with version: %s", getName(), configuration.getVersion())); 

     CsvReporter.forRegistry(environment.metrics()).build(new File("path/to/csv")); 
    } 

希望幫助,

阿圖爾

+0

你不明白我的問題。查看更新的問題。 –

+0

再說一次:是的,這是創建自己的自定義記者的正確方法,並查看我的答案以及註冊方式。你還想知道什麼,因爲這些是我可以在你的帖子中看到的唯一問題。 – pandaadb