Skip to content

fix: generate UUID v7 identifiers#192

Draft
marandaneto wants to merge 2 commits into
PostHog:mainfrom
marandaneto:fix/use-uuid-v7
Draft

fix: generate UUID v7 identifiers#192
marandaneto wants to merge 2 commits into
PostHog:mainfrom
marandaneto:fix/use-uuid-v7

Conversation

@marandaneto

Copy link
Copy Markdown
Member

💡 Motivation and Context

SDK-generated event UUIDs and generated personless distinct IDs should use UUID v7 so generated identifiers are time-ordered. This updates the Ruby SDK-generated identifiers while preserving user-provided uuid and deprecated message_id behavior.

💚 How did you test it?

  • bundle exec rspec spec/posthog/client_spec.rb
  • bundle exec rubocop lib/posthog/uuid.rb lib/posthog/field_parser.rb lib/posthog/client.rb spec/posthog/client_spec.rb

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

An AI coding agent implemented the requested UUID v7 migration. The implementation uses a small internal helper that delegates to SecureRandom.uuid_v7 when available and falls back to a local RFC 9562-style UUID v7 generator for supported Rubies where that API is unavailable.

@marandaneto marandaneto self-assigned this Jun 19, 2026
@greptile-apps

greptile-apps Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
lib/posthog/uuid.rb:9-18
The `SecureRandom.respond_to?(:uuid_v7)` reflection check is evaluated on every single call to `v7`. Since the Ruby runtime's method table for `SecureRandom` doesn't change between calls, this is redundant overhead on the hot path for every event capture. Caching the result once at module load time avoids repeated method lookup on every UUID generation.

```suggestion
  module Uuid
    NATIVE_UUID_V7 = SecureRandom.respond_to?(:uuid_v7)
    private_constant :NATIVE_UUID_V7

    module_function

    def v7
      return SecureRandom.uuid_v7 if NATIVE_UUID_V7

      bytes = uuid_v7_bytes
      hex = bytes.pack('C*').unpack1('H*')
      "#{hex[0, 8]}-#{hex[8, 4]}-#{hex[12, 4]}-#{hex[16, 4]}-#{hex[20, 12]}"
    end
```

Reviews (1): Last reviewed commit: "fix: generate UUID v7 identifiers" | Re-trigger Greptile

Comment thread lib/posthog/uuid.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant