<commit_before>package grails.lightweight

import com.codahale.metrics.MetricRegistry
import com.codahale.metrics.Timer
import grails.plugin.lightweightdeploy.ExternalContext
import org.codehaus.groovy.grails.web.context.ServletContextHolder

import static com.codahale.metrics.MetricRegistry.name

class MetricsFilters {

    public static final ThreadLocal<Timer.Context> timerContextThreadLocal = new ThreadLocal<Timer.Context>()

    def filters = {
        all(controller:'*', action:'*') {
            before = {
                try {
                    startTimer(controllerName, actionName)
                } catch (Throwable e) {
                    log.warn("There was an error starting measurement", e)
                }
            }
            afterView = { Exception e ->
                try {
                    stopTimer()
                } catch (Throwable metricsE) {
                    log.warn("There was an error stopping measurement", e)
                }
            }
        }
    }

    protected void startTimer(String controllerName, String actionName) {
        MetricRegistry metricRegistry = metricRegistry
        //will be null when using just run-app
        if (metricRegistry) {
            Timer timer = metricRegistry.timer(name(controllerName, actionName?:"defaultAction"))
            Timer.Context context = timer.time();
            timerContextThreadLocal.set(context)
        }
    }

    protected void stopTimer() {
        if (timerContextThreadLocal.get() != null) {
            timerContextThreadLocal.get().stop()
            timerContextThreadLocal.remove()
        }
    }

    protected MetricRegistry getMetricRegistry() {
        ServletContextHolder.servletContext.getAttribute(ExternalContext.METRICS_REGISTRY_SERVLET_ATTRIBUTE)
    }
}<commit_msg>controllerName also cannot be null<commit_after>package grails.lightweight

import com.codahale.metrics.MetricRegistry
import com.codahale.metrics.Timer
import grails.plugin.lightweightdeploy.ExternalContext
import org.codehaus.groovy.grails.web.context.ServletContextHolder

import static com.codahale.metrics.MetricRegistry.name

class MetricsFilters {

    public static final ThreadLocal<Timer.Context> timerContextThreadLocal = new ThreadLocal<Timer.Context>()

    def filters = {
        all(controller:'*', action:'*') {
            before = {
                try {
                    startTimer(controllerName, actionName)
                } catch (Throwable e) {
                    log.warn("There was an error starting measurement", e)
                }
            }
            afterView = { Exception e ->
                try {
                    stopTimer()
                } catch (Throwable metricsE) {
                    log.warn("There was an error stopping measurement", e)
                }
            }
        }
    }

    protected void startTimer(String controllerName, String actionName) {
        MetricRegistry metricRegistry = metricRegistry
        //will be null when using just run-app
        if (metricRegistry) {
            Timer timer = metricRegistry.timer(name(controllerName?:"", actionName?:"defaultAction"))
            Timer.Context context = timer.time();
            timerContextThreadLocal.set(context)
        }
    }

    protected void stopTimer() {
        if (timerContextThreadLocal.get() != null) {
            timerContextThreadLocal.get().stop()
            timerContextThreadLocal.remove()
        }
    }

    protected MetricRegistry getMetricRegistry() {
        ServletContextHolder.servletContext.getAttribute(ExternalContext.METRICS_REGISTRY_SERVLET_ATTRIBUTE)
    }
}
{'fraction_non_alphanumeric': 0.08399670601152896, 'fraction_numerical': 0.0, 'mean_word_length': 2.193689745836985, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6033785', 'n_tokens_mistral': 920, 'n_tokens_neox': 878, 'n_words': 191}