<commit_before>import time
from simplekeys.verifier import verify_request
from graphene_django.views import GraphQLView
from django.conf import settings
import newrelic
from structlog import get_logger


logger = get_logger("graphapi")


class KeyedGraphQLView(GraphQLView):
    graphiql_template = "graphene/graphiql-keyed.html"

    def get_response(self, request, data, show_graphiql=False):
        log = logger.bind(
            user_agent=request.META.get("HTTP_USER_AGENT", "UNKNOWN"),
            remote_addr=request.META.get("REMOTE_ADDR"),
            api_key=request.META.get(
                getattr(settings, "SIMPLEKEYS_HEADER", "HTTP_X_API_KEY")
            ),
            data=data.get("query") or request.GET.get("query"),
        )
        start = time.time()

        # check key only if we're not handling a graphiql request
        if not show_graphiql:
            newrelic.agent.add_custom_parameter(
                "key",
                request.META.get(
                    getattr(settings, "SIMPLEKEYS_HEADER", "HTTP_X_API_KEY")
                ),
            )
            newrelic.agent.add_custom_parameter("request-data", data)
            error = verify_request(request, "graphapi")
            if error:
                log.bind(
                    error=error,
                    status_code=error.status_code,
                    duration=time.time() - start,
                )
                log.info("graphql")
                return error, error.status_code

        resp = super().get_response(request, data, show_graphiql)
        log.bind(duration=time.time() - start)
        log.info("graphql")<commit_msg>more structlog fixes<commit_after>import time
from simplekeys.verifier import verify_request
from graphene_django.views import GraphQLView
from django.conf import settings
import newrelic
from structlog import get_logger


logger = get_logger("graphapi")


class KeyedGraphQLView(GraphQLView):
    graphiql_template = "graphene/graphiql-keyed.html"

    def get_response(self, request, data, show_graphiql=False):
        log = logger.bind(
            user_agent=request.META.get("HTTP_USER_AGENT", "UNKNOWN"),
            remote_addr=request.META.get("REMOTE_ADDR"),
            api_key=request.META.get(
                getattr(settings, "SIMPLEKEYS_HEADER", "HTTP_X_API_KEY")
            ),
            query=data.get("query") or request.GET.get("query"),
        )
        start = time.time()

        # check key only if we're not handling a graphiql request
        if not show_graphiql:
            newrelic.agent.add_custom_parameter(
                "key",
                request.META.get(
                    getattr(settings, "SIMPLEKEYS_HEADER", "HTTP_X_API_KEY")
                ),
            )
            newrelic.agent.add_custom_parameter("request-data", data)
            error = verify_request(request, "graphapi")
            if error:
                log = log.bind(
                    error=error,
                    status_code=error.status_code,
                    duration=time.time() - start,
                )
                log.info("graphql")
                return error, error.status_code

        resp = super().get_response(request, data, show_graphiql)
        log = log.bind(duration=time.time() - start)
        log.info("graphql")
{'fraction_non_alphanumeric': 0.09735576923076923, 'fraction_numerical': 0.0, 'mean_word_length': 2.0513290559120074, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3170837', 'n_tokens_mistral': 983, 'n_tokens_neox': 933, 'n_words': 177}