/** * Instantiates a named scope - intended to only be called by Metrics, so locally scoped. * @param name - name of the variable */ private CodahaleMetricsScope(String name) { this.name = name; this.timer = CodahaleMetrics.this.getTimer(name); open(); }
@Override public Long incrementCounter(String name) { return incrementCounter(name, 1L); }
@Override public void addGauge(String name, final MetricsVariable variable) { Gauge gauge = new Gauge() { @Override public Object getValue() { return variable.getValue(); } }; addGaugeInternal(name, gauge); }
@Test public void testRatioIsCalculated() throws Exception { NumericVariable num = new NumericVariable(10); NumericVariable ord = new NumericVariable(5); MetricsFactory.getInstance().addRatio("rat", num, ord); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "rat", 2d); }
@Test public void testGauge() throws Exception { TestMetricsVariable testVar = new TestMetricsVariable(); testVar.setValue(20); MetricsFactory.getInstance().addGauge("gauge1", testVar); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "gauge1", testVar.getValue()); testVar.setValue(40); json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "gauge1", testVar.getValue()); }
@Before public void before() throws Exception { HiveConf conf = new HiveConf(); conf.set(CommonConfigurationKeysPublic.FS_DEFAULT_NAME_KEY, "local"); conf.setVar(HiveConf.ConfVars.HIVE_METRICS_CLASS, CodahaleMetrics.class.getCanonicalName()); conf.setVar(HiveConf.ConfVars.HIVE_METRICS_REPORTER, MetricsReporting.JSON_FILE.name() + "," + MetricsReporting.JMX.name()); conf.setVar(HiveConf.ConfVars.HIVE_METRICS_JSON_FILE_INTERVAL, "100000s"); MetricsFactory.init(conf); metricRegistry = ((CodahaleMetrics) MetricsFactory.getInstance()).getMetricRegistry(); hook = new MetricsQueryLifeTimeHook(); ctx = new QueryLifeTimeHookContextImpl(); }
@Test public void testSubmittedQueryCount() throws Exception { String json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.TIMER, MetricsConstant.HS2_SUBMITTED_QURIES, "0"); operation.onNewState(OperationState.FINISHED, OperationState.RUNNING); json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.TIMER, MetricsConstant.HS2_SUBMITTED_QURIES, "1"); }
@Override public void addRatio(String name, MetricsVariable<Integer> numerator, MetricsVariable<Integer> denominator) { Preconditions.checkArgument(numerator != null, "Numerator must not be null"); Preconditions.checkArgument(denominator != null, "Denominator must not be null"); MetricVariableRatioGauge gauge = new MetricVariableRatioGauge(numerator, denominator); addGaugeInternal(name, gauge); }
/** * Initializes reporters from HIVE_CODAHALE_METRICS_REPORTER_CLASSES or HIVE_METRICS_REPORTER if the former is not defined. * Note: if both confs are defined, only HIVE_CODAHALE_METRICS_REPORTER_CLASSES will be used. */ private void initReporting() { if (!(initCodahaleMetricsReporterClasses() || initMetricsReporter())) { LOGGER.warn("Unable to initialize metrics reporting"); } if (reporters.isEmpty()) { // log a warning incase no reporters were successfully added LOGGER.warn("No reporters configured for codahale metrics!"); } }
@Override public void startStoredScope(String name) { if (threadLocalScopes.get().containsKey(name)) { threadLocalScopes.get().get(name).open(); } else { threadLocalScopes.get().put(name, new CodahaleMetricsScope(name)); } }
@Override public Long decrementCounter(String name) { return decrementCounter(name, 1L); }
public void endScope(MetricsScope scope) { ((CodahaleMetricsScope) scope).close(); }
public MetricsScope createScope(String name) { return new CodahaleMetricsScope(name); }
@Test(expected = IllegalArgumentException.class) public void testMissingNumeratorRatio() throws Exception { MetricsFactory.getInstance().addRatio("rat", null, new NumericVariable(5)); }
@Test public void testRatioIsCalculatedNonExact() throws Exception { NumericVariable num = new NumericVariable(20); NumericVariable ord = new NumericVariable(3); MetricsFactory.getInstance().addRatio("rat", num, ord); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "rat", 6.6666d, 1e-4); }
/** * Opens scope, and makes note of the time started, increments run counter * */ public void open() { if (!isOpen) { isOpen = true; this.timerContext = timer.time(); CodahaleMetrics.this.incrementCounter(MetricsConstant.ACTIVE_CALLS + name); } else { LOGGER.warn("Scope named " + name + " is not closed, cannot be opened."); } }
/** * Closes scope, and records the time taken */ public void close() { if (isOpen) { timerContext.close(); CodahaleMetrics.this.decrementCounter(MetricsConstant.ACTIVE_CALLS + name); } else { LOGGER.warn("Scope named " + name + " is not open, cannot be closed."); } isOpen = false; } }
@Override public void endStoredScope(String name) { if (threadLocalScopes.get().containsKey(name)) { threadLocalScopes.get().get(name).close(); threadLocalScopes.get().remove(name); } }
@Test public void testEmptyRatio() throws Exception { NumericVariable num = new NumericVariable(null); NumericVariable ord = new NumericVariable(null); MetricsFactory.getInstance().addRatio("rat", num, ord); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "rat", "NaN"); }
@Test public void testZeroRatio() throws Exception { NumericVariable num = new NumericVariable(10); NumericVariable ord = new NumericVariable(0); MetricsFactory.getInstance().addRatio("rat", num, ord); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "rat", "NaN"); }