-
Notifications
You must be signed in to change notification settings - Fork 28
fix: no-op when API key is blank or supervisor unavailable #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ed0dd09
Disable SDK when API key is blank
marandaneto 36d16c5
address pr review feedback
marandaneto 794f25d
Merge branch 'main' into fix/noop-empty-api-key
marandaneto 0cfe84c
guard feature flags when sdk is disabled
marandaneto 6a205b4
simplify disabled sender no-op
marandaneto 8b757fd
Merge branch 'main' into fix/noop-empty-api-key
marandaneto fb17a3f
fix: no-op when supervisor is unavailable
marandaneto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| hex/posthog: patch | ||
| --- | ||
|
|
||
| Start in disabled/no-op mode instead of raising or sending events when the API key is missing, blank, or the supervisor is unavailable. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| defmodule PostHog.ApplicationConfigTest do | ||
| use ExUnit.Case, async: false | ||
|
|
||
| import ExUnit.CaptureLog | ||
| import Mox | ||
|
|
||
| setup :verify_on_exit! | ||
|
|
||
| setup do | ||
| previous_env = Application.get_all_env(:posthog) | ||
|
|
||
| on_exit(fn -> | ||
| :posthog | ||
| |> Application.get_all_env() | ||
| |> Keyword.keys() | ||
| |> Enum.each(&Application.delete_env(:posthog, &1)) | ||
|
|
||
| Enum.each(previous_env, fn {key, value} -> | ||
| Application.put_env(:posthog, key, value) | ||
| end) | ||
| end) | ||
| end | ||
|
|
||
| for {label, api_key_env} <- [ | ||
| {"missing", :missing}, | ||
| {"nil", nil} | ||
| ] do | ||
| test "read! does not raise when enabled and api_key is #{label}" do | ||
| Application.put_env(:posthog, :enable, true) | ||
| Application.put_env(:posthog, :api_client_module, PostHog.API.Mock) | ||
| Application.delete_env(:posthog, :api_host) | ||
|
|
||
| case unquote(Macro.escape(api_key_env)) do | ||
| :missing -> Application.delete_env(:posthog, :api_key) | ||
| api_key -> Application.put_env(:posthog, :api_key, api_key) | ||
| end | ||
|
|
||
| log = | ||
| capture_log(fn -> | ||
| assert {%{enable: true}, %{api_key: "", enabled: false, api_client: nil} = config} = | ||
| PostHog.Config.read!() | ||
|
|
||
| assert config.api_host == "https://us.i.posthog.com" | ||
| end) | ||
|
|
||
| assert log =~ | ||
| "posthog api_key is empty after trimming whitespace; PostHog will start in disabled/no-op mode" | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| defmodule PostHog.SupervisorTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| import ExUnit.CaptureLog | ||
|
|
||
| @supervisor_name __MODULE__ | ||
|
|
||
| test "disabled config starts without senders and captures as no-op" do | ||
| {config, _log} = | ||
| with_log(fn -> | ||
| PostHog.Config.validate!( | ||
| api_key: " \n\t ", | ||
| api_host: "https://us.i.posthog.com", | ||
| supervisor_name: @supervisor_name, | ||
| enable_source_code_context: true, | ||
| test_mode: true | ||
| ) | ||
| end) | ||
|
|
||
| start_link_supervised!({PostHog.Supervisor, config}) | ||
|
|
||
| assert [] = | ||
| @supervisor_name | ||
| |> PostHog.Registry.registry_name() | ||
| |> Registry.select([{{{PostHog.Sender, :_}, :"$1", :"$2"}, [], [:"$1"]}]) | ||
|
|
||
| assert nil == | ||
| @supervisor_name | ||
| |> PostHog.Registry.via(PostHog.ErrorTracking.Sources) | ||
| |> GenServer.whereis() | ||
|
|
||
| assert :ok = PostHog.bare_capture(@supervisor_name, "disabled capture", "distinct_id", %{}) | ||
| assert PostHog.Test.all_captured(@supervisor_name) == [] | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.