Skip to content

fix: Handle blank API keys as disabled clients#158

Merged
marandaneto merged 3 commits into
mainfrom
fix/noop-empty-api-key
May 29, 2026
Merged

fix: Handle blank API keys as disabled clients#158
marandaneto merged 3 commits into
mainfrom
fix/noop-empty-api-key

Conversation

@marandaneto
Copy link
Copy Markdown
Member

@marandaneto marandaneto commented May 28, 2026

💡 Motivation and Context

Blank, missing, or whitespace-only API keys should behave like disabled PostHog clients instead of raising during initialization or starting background network work. This lets apps safely initialize the SDK when API key configuration is absent.

Changes:

  • Treat nil, empty, and whitespace-only API keys as disabled/no-op clients.
  • Avoid starting send workers, sync transports, and feature flag polling for disabled clients.
  • Return early from capture for disabled clients to skip unnecessary event processing.
  • Log a clearer message for missing or blank API keys.
  • Add focused specs for disabled initialization and dropped events with separate examples per invalid API key value.

💚 How did you test it?

  • bundle exec rspec spec/posthog/client_spec.rb
  • bundle exec rspec

📝 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

@marandaneto marandaneto requested a review from a team as a code owner May 28, 2026 10:39
@marandaneto marandaneto changed the title Handle blank API keys as disabled clients fix: Handle blank API keys as disabled clients May 28, 2026
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 28, 2026

Prompt To Fix All With AI
Fix the following 3 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 3
lib/posthog/client.rb:109
The error message says "empty after trimming whitespace" but is now also triggered when `@api_key` is `nil` — in that case no trimming ever happened, so the message is inaccurate and may confuse someone debugging a missing-key configuration.

```suggestion
      logger.error('api_key is missing or empty after trimming whitespace; check your project API key') if @disabled
```

### Issue 2 of 3
lib/posthog/client.rb:85
The `respond_to?(:empty?)` guard is superfluous here. By the time this line runs, `@api_key` has already been through `normalize_string_option`, which returns only `nil` or a `String`. Since `nil` is already handled by the preceding `nil?` check, `@api_key` can only be a `String` at the `empty?` call — duck-typing adds noise with no defensive value.

```suggestion
      @disabled = @api_key.nil? || @api_key.empty?
```

### Issue 3 of 3
spec/posthog/client_spec.rb:33-53
**Prefer parametrised tests over inline loops**

Several new `it` blocks (lines 33–53 and 85–97) iterate over input arrays inside the body rather than using RSpec's parametrization. With an inline `each` loop a single failure reports against the whole example rather than the specific input, making failures harder to diagnose. Consider using `where`-style tables or individual `context`/`it` blocks for each value, as the team's convention requires.

Reviews (1): Last reviewed commit: "Handle blank API keys as disabled client..." | Re-trigger Greptile

Comment thread lib/posthog/client.rb Outdated
Comment thread lib/posthog/client.rb Outdated
Comment thread spec/posthog/client_spec.rb Outdated
Comment thread lib/posthog/client.rb
@marandaneto marandaneto merged commit 999bd1c into main May 29, 2026
16 checks passed
@marandaneto marandaneto deleted the fix/noop-empty-api-key branch May 29, 2026 08:38
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.

2 participants