/**
 * Copyright (c) 2015-2018, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 *  http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.component.metric;

import com.codahale.metrics.Counter;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Timer;
import io.jboot.Jboot;
import io.jboot.component.metric.annotation.EnableMetricCounter;
import io.jboot.component.metric.annotation.EnableMetricHistogram;
import io.jboot.component.metric.annotation.EnableMetricMeter;
import io.jboot.component.metric.annotation.EnableMetricTimer;
import io.jboot.utils.StringUtils;
import io.jboot.web.fixedinterceptor.FixedInterceptor;
import io.jboot.web.fixedinterceptor.FixedInvocation;

/**
 * 用于对controller的Metrics 统计
 * 注意：如果 Controller通过 @Clear 来把此 拦截器给清空，那么此方法（action）注入将会失效
 */
public class JbootMetricInterceptor implements FixedInterceptor {


    @Override
    public void intercept(FixedInvocation inv) {


        Counter counter = null;
        Timer.Context timerContext = null;


        EnableMetricCounter counterAnnotation = inv.getMethod().getAnnotation(EnableMetricCounter.class);
        if (counterAnnotation != null) {
            String name = StringUtils.isBlank(counterAnnotation.value())
                    ? inv.getController().getClass().getName() + "." + inv.getMethodName()
                    : counterAnnotation.value();


            counter = Jboot.me().getMetric().counter(name);
            counter.inc();
        }


        EnableMetricMeter meterAnnotation = inv.getMethod().getAnnotation(EnableMetricMeter.class);
        if (meterAnnotation != null) {
            String name = StringUtils.isBlank(meterAnnotation.value())
                    ? inv.getController().getClass().getName() + "." + inv.getMethodName()
                    : meterAnnotation.value();


            Meter meter = Jboot.me().getMetric().meter(name);
            meter.mark();
        }


        EnableMetricHistogram histogramAnnotation = inv.getMethod().getAnnotation(EnableMetricHistogram.class);
        if (histogramAnnotation != null) {
            String name = StringUtils.isBlank(histogramAnnotation.value())
                    ? inv.getController().getClass().getName() + "." + inv.getMethodName()
                    : histogramAnnotation.value();


            Histogram histogram = Jboot.me().getMetric().histogram(name);
            histogram.update(histogramAnnotation.update());
        }


        EnableMetricTimer timerAnnotation = inv.getMethod().getAnnotation(EnableMetricTimer.class);
        if (timerAnnotation != null) {
            String name = StringUtils.isBlank(timerAnnotation.value())
                    ? inv.getController().getClass().getName() + "." + inv.getMethodName()
                    : timerAnnotation.value();


            Timer timer = Jboot.me().getMetric().timer(name);
            timerContext = timer.time();
        }


        try {
            inv.invoke();
        } finally {
            if (counter != null) {
                counter.dec();
            }
            if (timerContext != null) {
                timerContext.stop();
            }
        }

    }


}

@@ -3509,88 +3509,8 @@
 y %7B%0A
-            if (counter != null) %7B%0A                counter.dec();%0A            %7D%0A
     

修改 @EnableMetricCounter 为记录次数而不是并发量