How to add User IP address to NewRelic transaction for Django?

How to add User IP address to NewRelic transaction for Django?

NewRelic out of the box would ignore collecting User IP, due to privacy concerns. But for a lot of web applications, it is important to capture user IP so that performance can be monitored by categorizing by different physical user IPs.

To pass user IP to a transaction of the trace object of NewRelic, you should implement a custom middleware and add the user IP detection code. If you don't know how to write custom middleware, then head to the below blog.

How to create custom middleware in Django?
The TLDR answer: * Create a file custom_middleware.py file in the same folder as where django settings.py file is present * Add the following code in custom_middleware.py class CustomMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(s…
from newrelic import agent

class CustomMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):

        agent.add_custom_parameters([("ClientIpAddress", get_client_ip(request))])

        """
        From here the current middleware request is sent to the next middleware
        """

        response = self.get_response(request)
        return response


def get_client_ip(request):
    """Get IP address of the client from the request object."""

    x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
    if x_forwarded_for:
        ip = x_forwarded_for.split(",")[0]
    else:
        ip = request.META.get("REMOTE_ADDR")
    return ip
custom_middleware.py
Here the get_client_ip function is extracting the IP address from the request header and returning back.  Please check before hand whether the IP address returned from this function is the correct user IP. Since modern web applications have multiple layers, like ALB, API Gateway, etc, so it might be possible the IP added to the header is in a different position to what I have added in the given example.

Once the IP address is extracted, we are appending the property using newrelic.agent SDK. You can modify the name ClientIpAddress to any desired name which you want, just make sure to not give space.

agent.add_custom_parameters([("ClientIpAddress", get_client_ip(request))])


The above integration would now be sending the User IP for every transaction and one can write queries to identify them. Go to the query builder and run the below query to see your captured user IP address

SELECT ClientIpAddress FROM Transaction WHERE entityGuid = <your application> SINCE 30 MINUTES AGO

If you are not aware of how to integrate NewRelic into your Django application you can follow the below blog

How to integrate New Relic (APM) to Django?
Application Performance Monitoring (APM) is a must have tool for any of theapplications which is live for external users. I prefer using NewRelic as an APMfor all of my applications. Before jumping into the installation please create anew account with NewRelic. Deliver more perfect softwareWork …