<commit_before>from __future__ import print_function
import opentracing as ot
from instana import tracer, options
import opentracing.ext.tags as ext


DJ19_INSTANA_MIDDLEWARE = 'instana.django19.InstanaMiddleware19'


class InstanaMiddleware19(object):
    """ Django 1.9 Middleware to provide request tracing for Instana """
    def __init__(self):
        opts = options.Options(service="Django")
        ot.global_tracer = tracer.InstanaTracer(opts)
        self.span = None
        self

    def process_request(self, request):
        env = request.environ
        if 'HTTP_X_INSTANA_T' in env and 'HTTP_X_INSTANA_S' in env:
            ctx = ot.global_tracer.extract(ot.Format.HTTP_HEADERS, env)
            span = ot.global_tracer.start_span("django", child_of=ctx)
        else:
            span = ot.global_tracer.start_span("django")

        span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
        span.set_tag("http.params", env['QUERY_STRING'])
        span.set_tag(ext.HTTP_METHOD, request.method)
        span.set_tag("http.host", env['HTTP_HOST'])
        self.span = span

    def process_response(self, request, response):
        if self.span:
            if 500 <= response.status_code <= 511:
                self.span.set_tag("error", True)
                ec = self.span.tags.get('ec', 0)
                self.span.set_tag("ec", ec+1)

            self.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
            ot.global_tracer.inject(self.span.context, ot.Format.HTTP_HEADERS, response)
            self.span.finish()
            self.span = None
        return response



def hook(module):
    """ Hook method to install the Instana middleware into Django """
    if DJ19_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE_CLASSES:
        return

    if type(module.settings.MIDDLEWARE_CLASSES) is tuple:<commit_msg>Add debug msg to django19 instrumentation<commit_after>from __future__ import print_function
import opentracing as ot
from instana import tracer, options
import opentracing.ext.tags as ext
import os


DJ19_INSTANA_MIDDLEWARE = 'instana.django19.InstanaMiddleware19'


class InstanaMiddleware19(object):
    """ Django 1.9 Middleware to provide request tracing for Instana """
    def __init__(self):
        opts = options.Options(service="Django")
        ot.global_tracer = tracer.InstanaTracer(opts)
        self.span = None
        self

    def process_request(self, request):
        env = request.environ
        if 'HTTP_X_INSTANA_T' in env and 'HTTP_X_INSTANA_S' in env:
            ctx = ot.global_tracer.extract(ot.Format.HTTP_HEADERS, env)
            span = ot.global_tracer.start_span("django", child_of=ctx)
        else:
            span = ot.global_tracer.start_span("django")

        span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
        span.set_tag("http.params", env['QUERY_STRING'])
        span.set_tag(ext.HTTP_METHOD, request.method)
        span.set_tag("http.host", env['HTTP_HOST'])
        self.span = span

    def process_response(self, request, response):
        if self.span:
            if 500 <= response.status_code <= 511:
                self.span.set_tag("error", True)
                ec = self.span.tags.get('ec', 0)
                self.span.set_tag("ec", ec+1)

            self.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
            ot.global_tracer.inject(self.span.context, ot.Format.HTTP_HEADERS, response)
            self.span.finish()
            self.span = None
        return response



def hook(module):
    """ Hook method to install the Instana middleware into Django """
    if "INSTANA_DEV" in os.environ:
        print("==============================================================")
        print("Instana: Running django19 hook")
        print("==============================================================")


    if DJ19_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE_CLASSES:
        return

    if type(module.settings.MIDDLEWARE_CLASSES) is tuple:
{'fraction_non_alphanumeric': 0.13083877448518333, 'fraction_numerical': 0.011049723756906077, 'mean_word_length': 2.855759922555663, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '3055733', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1190, 'n_words': 252}