from appenlight_client.utils import import_module, deco_func_or_method
from appenlight_client.timing import time_trace, import_from_module, _e_trace

ignore_set = frozenset()


def add_timing(min_duration=0.15):
    module = import_module('jinja2')
    if not module:
        return

    from jinja2 import environment

    def gather_template(template, *args, **kwargs):
        return {'type': 'tmpl',
                'subtype': 'jinja2',
                'statement': 'render',
                'parameters': '',
                'count': True,
                'ignore_in': ignore_set}

    deco_func_or_method(environment, 'Template.render', time_trace,
                        gatherer=gather_template, min_duration=min_duration, is_template=True)

    environment.Environment.template_class = environment.Template
    module.Template = environment.Template

@@ -365,16 +365,130 @@
 wargs):%0A
+        try:%0A            tmpl_name = str(template.name)%0A        except Exception as e:%0A            tmpl_name = ''%0A
         
@@ -617,18 +617,25 @@
 eters': 
-''
+tmpl_name
 ,%0A      

add timing to jinja2