1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import datetime
- import uuid
- import random
- import string
- from .models import TransactionEvent, TransactionGroup, Span
- TRANSACTIONS = [
- "generic WSGI request",
- "/admin",
- "/admin/login/",
- "/",
- "/favicon.ico",
- "/foo",
- "/bar",
- ]
- OPS = [
- "http.server",
- "pageload",
- "http",
- "browser",
- "db",
- "django.middleware",
- "django.view",
- "django.foo",
- "django.bar",
- ]
- METHODS = [
- "GET",
- "POST",
- "PATCH",
- "PUT",
- "DELETE",
- ]
- def maybe_random_string():
- if random.getrandbits(6) == 0: # small chance
- return "".join(random.choices(string.ascii_uppercase + string.digits, k=20))
- def generate_random_transaction():
- return maybe_random_string() or random.choice(TRANSACTIONS)
- def generate_random_op():
- randbits = random.getrandbits(3)
- if randbits == 0: # Favor http.server
- return "http.server"
- return maybe_random_string() or random.choice(OPS)
- def generate_random_method():
- return random.choice(METHODS)
- def generate_random_timestamp(start_timestamp):
- """
- Generate a realistic looking random time interval
- small chance between 0 and 30 seconds
- most will be between 0 and 2 seconds
- """
- if random.getrandbits(3) == 0:
- interval = random.randint(0, 30000)
- else:
- interval = random.randint(0, 2000)
- return start_timestamp + datetime.timedelta(milliseconds=interval)
- def generate_fake_transaction_event(project, start_timestamp):
- """
- Generate random transaction and return result (unsaved)
- Will get_or_create the transaction group, function will result in queries
- """
- op = generate_random_op()
- method = None
- if op == "http.server":
- method = generate_random_method()
- group, _ = TransactionGroup.objects.get_or_create(
- transaction=generate_random_transaction(),
- project=project,
- op=op,
- method=method,
- )
- timestamp=generate_random_timestamp(start_timestamp)
- return TransactionEvent(
- group=group,
- trace_id=uuid.uuid4(),
- start_timestamp=start_timestamp,
- data={},
- timestamp=timestamp,
- duration=timestamp - start_timestamp
- )
|