@Test
  public void testTimerMatch() {
    // create timer with id "testMetricAlerts" and register with metric registry, bump up value to
    // 4.
    Timer t = MetricsConfigurator.createTimer(metrics, "testTimerMatch", PIPELINE_NAME, REVISION);
    t.update(1000, TimeUnit.MILLISECONDS);
    t.update(2000, TimeUnit.MILLISECONDS);
    t.update(3000, TimeUnit.MILLISECONDS);

    MetricsRuleDefinition metricsRuleDefinition =
        new MetricsRuleDefinition(
            "testTimerMatch",
            "testTimerMatch",
            "testTimerMatch",
            MetricType.TIMER,
            MetricElement.TIMER_COUNT,
            "${value()>2}",
            false,
            true);
    MetricRuleEvaluator metricRuleEvaluator =
        new MetricRuleEvaluator(
            metricsRuleDefinition,
            metrics,
            new AlertManager(
                PIPELINE_NAME, REVISION, null, metrics, runtimeInfo, new EventListenerManager()),
            Collections.<String>emptyList());
    metricRuleEvaluator.checkForAlerts();

    // get alert gauge
    Gauge<Object> gauge =
        MetricsConfigurator.getGauge(
            metrics, AlertsUtil.getAlertGaugeName(metricsRuleDefinition.getId()));
    Assert.assertNotNull(gauge);
    Assert.assertEquals((long) 3, ((Map<String, Object>) gauge.getValue()).get("currentValue"));
  }
 @SuppressWarnings("unchecked")
 public static void alertException(
     String pipelineName,
     String revision,
     MetricRegistry metrics,
     Object value,
     RuleDefinition ruleDefinition) {
   final Map<String, Object> alertResponse = new HashMap<>();
   alertResponse.put(EmailConstants.EXCEPTION_MESSAGE, value);
   Gauge<Object> gauge =
       MetricsConfigurator.getGauge(metrics, AlertsUtil.getAlertGaugeName(ruleDefinition.getId()));
   if (gauge != null) {
     // remove existing gauge
     MetricsConfigurator.removeGauge(
         metrics, AlertsUtil.getAlertGaugeName(ruleDefinition.getId()), pipelineName, revision);
   }
   Gauge<Object> alertResponseGauge =
       new Gauge<Object>() {
         @Override
         public Object getValue() {
           return alertResponse;
         }
       };
   MetricsConfigurator.createGauge(
       metrics,
       AlertsUtil.getAlertGaugeName(ruleDefinition.getId()),
       alertResponseGauge,
       pipelineName,
       revision);
 }
  @Test
  public void testHistogramDisabled() {
    // create timer with id "testMetricAlerts" and register with metric registry, bump up value to
    // 4.
    Histogram h =
        MetricsConfigurator.createHistogram5Min(
            metrics, "testHistogramDisabled", PIPELINE_NAME, REVISION);
    h.update(1000);

    MetricsRuleDefinition metricsRuleDefinition =
        new MetricsRuleDefinition(
            "testHistogramDisabled",
            "testHistogramDisabled",
            "testHistogramDisabled",
            MetricType.HISTOGRAM,
            MetricElement.HISTOGRAM_COUNT,
            "${value()==1}",
            false,
            false);
    MetricRuleEvaluator metricRuleEvaluator =
        new MetricRuleEvaluator(
            metricsRuleDefinition,
            metrics,
            new AlertManager(
                PIPELINE_NAME, REVISION, null, metrics, runtimeInfo, new EventListenerManager()),
            Collections.<String>emptyList());
    metricRuleEvaluator.checkForAlerts();

    // get alert gauge
    Gauge<Object> gauge =
        MetricsConfigurator.getGauge(
            metrics, AlertsUtil.getAlertGaugeName(metricsRuleDefinition.getId()));
    Assert.assertNull(gauge);
  }
  @Test
  public void testMeterNoMatch() {
    // create timer with id "testMetricAlerts" and register with metric registry, bump up value to
    // 4.
    Meter m = MetricsConfigurator.createMeter(metrics, "testMeterNoMatch", PIPELINE_NAME, REVISION);
    m.mark(1000);

    MetricsRuleDefinition metricsRuleDefinition =
        new MetricsRuleDefinition(
            "testMeterNoMatch",
            "testMeterNoMatch",
            "testMeterNoMatch",
            MetricType.METER,
            MetricElement.METER_COUNT,
            "${value()>1001}",
            false,
            true);
    MetricRuleEvaluator metricRuleEvaluator =
        new MetricRuleEvaluator(
            metricsRuleDefinition,
            metrics,
            new AlertManager(
                PIPELINE_NAME, REVISION, null, metrics, runtimeInfo, new EventListenerManager()),
            Collections.<String>emptyList());
    metricRuleEvaluator.checkForAlerts();

    // get alert gauge
    Gauge<Object> gauge =
        MetricsConfigurator.getGauge(
            metrics, AlertsUtil.getAlertGaugeName(metricsRuleDefinition.getId()));
    Assert.assertNull(gauge);
  }
  public void evaluate() {

    // check for changes in metric rules
    RulesConfigurationChangeRequest tempNewChangeRequest = newChangeRequest;
    if (tempNewChangeRequest != null && tempNewChangeRequest != currentChangeRequest) {
      this.currentChangeRequest = tempNewChangeRequest;
      for (String alertId : currentChangeRequest.getMetricAlertsToRemove()) {
        MetricsConfigurator.removeGauge(metrics, AlertsUtil.getAlertGaugeName(alertId), name, rev);
      }
    }

    if (currentChangeRequest != null) {
      List<MetricsRuleDefinition> metricsRuleDefinitions =
          currentChangeRequest.getRuleDefinitions().getMetricsRuleDefinitions();
      if (metricsRuleDefinitions != null) {
        for (MetricsRuleDefinition metricsRuleDefinition : metricsRuleDefinitions) {
          MetricRuleEvaluator metricAlertsHelper =
              new MetricRuleEvaluator(
                  metricsRuleDefinition,
                  metrics,
                  alertManager,
                  currentChangeRequest.getRuleDefinitions().getEmailIds());
          metricAlertsHelper.checkForAlerts();
        }
      }
    }
  }
 public static void updateDataRuleMeter(
     MetricRegistry metrics,
     DataRuleDefinition dataRuleDefinition,
     long matchingCount,
     String pipelineName,
     String revision) {
   if (dataRuleDefinition.isMeterEnabled() && matchingCount > 0) {
     Meter meter = MetricsConfigurator.getMeter(metrics, USER_PREFIX + dataRuleDefinition.getId());
     if (meter == null) {
       meter =
           MetricsConfigurator.createMeter(
               metrics, USER_PREFIX + dataRuleDefinition.getId(), pipelineName, revision);
     }
     meter.mark(matchingCount);
   }
 }
  public static Gauge<Object> createAlertResponseGauge(
      String pipelineName,
      String revision,
      MetricRegistry metrics,
      Object value,
      RuleDefinition ruleDefinition) {
    final Map<String, Object> alertResponse = new HashMap<>();
    alertResponse.put(EmailConstants.CURRENT_VALUE, value);
    Gauge<Object> alertResponseGauge =
        new Gauge<Object>() {
          @Override
          public Object getValue() {
            return alertResponse;
          }
        };
    alertResponse.put(EmailConstants.TIMESTAMP, System.currentTimeMillis());
    List<String> alertTexts = new ArrayList<>();
    alertTexts.add(ruleDefinition.getAlertText());
    alertResponse.put(EmailConstants.ALERT_TEXTS, alertTexts);

    MetricsConfigurator.createGauge(
        metrics,
        AlertsUtil.getAlertGaugeName(ruleDefinition.getId()),
        alertResponseGauge,
        pipelineName,
        revision);
    return alertResponseGauge;
  }
Ejemplo n.º 8
0
 @Override
 public Counter createCounter(String name) {
   return MetricsConfigurator.createCounter(
       getMetrics(), CUSTOM_METRICS_PREFIX + instanceName + "." + name, pipelineName, rev);
 }