Skip to content

[BUG]: Django User ID Not Tracked When Using UUID Primary Keys #15543

@yardensachs

Description

@yardensachs

Tracer Version(s)

3.18.1

Python Version(s)

3.13.2

Pip Version(s)

pip 24.3.1

Bug Report

The Django integration fails to track user IDs when the user model uses a UUID primary key instead of an integer primary key. This is due to an overly restrictive type check in the user tracking logic introduced here.

The code currently checks if the user primary key is an integer before setting user ID tags:

uid = getattr(user, "pk", None)
if uid and isinstance(uid, int):
span._set_tag_str("django.user.id", str(uid))
span._set_tag_str(_user.ID, str(uid))

This means that when a Django user model uses a UUIDField as the primary key, the user ID is never tracked in spans.

Reproduction Code

  1. Create a Django project with a custom user model that uses UUID as primary key:

    from django.contrib.auth.models import AbstractUser
    import uuid
    from django.db import models
    
    class User(AbstractUser):
        id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  2. Install and configure ddtrace for Django

  3. Make an authenticated request

  4. Check the span tags - django.user.id will be missing

Error Logs

No response

Libraries in Use

No response

Operating System

Darwin Kernel Version 24.6.0

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions