Repository navigation
feat: add a partition setting to Speakeasy processes #9319
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
bflad
merged 4 commits into
main
from
bflad/age-3680-feat-add-a-partition-setting-to-gram
Nov 21, 2026
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
24b0230
feat: add a partition setting to Speakeasy processes
bflad bb8d95f
fix: address review feedback on the partition setting
bflad e9517c1
feat: require the partition setting with no default
bflad c63f858
fix: require the partition when --help follows the argument terminator
bflad 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,6 @@ | ||
| --- | ||
| "server": minor | ||
| "tunnel": minor | ||
| --- | ||
|
|
||
| The Speakeasy server binary (every command, including mcp, worker, streams and admin), pystreams and the tunnel gateway require a `GRAM_PARTITION` setting (`us` or `eu`) naming the partition they run in. There is no default: each refuses to start when it is missing, empty or unknown, so a deployment cannot silently run as the wrong partition. Each tags its logs with `gram.partition`; the server and pystreams also put it on their traces and metrics. `gram version` and help output work without it. Local development sets `GRAM_PARTITION=us` in `mise.toml`. |
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,15 @@ | ||
| """The Speakeasy partition a process runs in. | ||
|
|
||
| A partition is an independent production deployment with its own data stores | ||
| and hosts. The set is closed and mirrors ``server/internal/partition`` in the Go | ||
| server: adding a partition is a code change in both, so a typo in configuration | ||
| fails at startup instead of silently naming a new partition. | ||
| """ | ||
|
|
||
| from typing import Final | ||
|
|
||
| US: Final = "us" | ||
| EU: Final = "eu" | ||
|
|
||
| # Every known partition, in the same order as ``partition.All`` in Go. | ||
| PARTITIONS: Final = (US, EU) |
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,71 @@ | ||
| """The partition option and its closed set of values. | ||
|
|
||
| The set mirrors ``server/internal/partition`` in the Go server. The golden | ||
| values below must change together with ``partition.All`` there, so the two | ||
| services never disagree about which partitions exist. | ||
| """ | ||
|
|
||
| import click | ||
| import pytest | ||
| from click.testing import CliRunner | ||
|
|
||
| from pystreams import partition | ||
| from pystreams.cmd.flags_service import service_options | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def parse(monkeypatch: pytest.MonkeyPatch): | ||
| """Parse argv/env through a command carrying only the service options. | ||
|
|
||
| Clears any ambient GRAM_PARTITION so a value exported in the shell cannot | ||
| change the outcome; tests pass the environment they need explicitly. | ||
| """ | ||
| monkeypatch.delenv("GRAM_PARTITION", raising=False) | ||
|
|
||
| def _parse(args: list[str], env: dict[str, str] | None = None) -> dict: | ||
| captured: dict = {} | ||
|
|
||
| def _record(**kwargs) -> None: | ||
| captured.update(kwargs) | ||
|
|
||
| command = click.Command( | ||
| "probe", params=list(service_options()), callback=_record | ||
| ) | ||
| CliRunner().invoke( | ||
| command, args, env=env, standalone_mode=False, catch_exceptions=False | ||
| ) | ||
| return captured | ||
|
|
||
| return _parse | ||
|
|
||
|
|
||
| def test_partitions_match_go_server(): | ||
| # Golden values from server/internal/partition (partition.All). | ||
| assert partition.PARTITIONS == ("us", "eu") | ||
|
|
||
|
|
||
| def test_partition_is_required(parse): | ||
| with pytest.raises(click.MissingParameter, match="partition"): | ||
| parse([]) | ||
|
|
||
|
|
||
| def test_partition_reads_environment_variable(parse): | ||
| assert parse([], env={"GRAM_PARTITION": "eu"})["partition"] == "eu" | ||
|
|
||
|
|
||
| def test_empty_partition_environment_variable_is_rejected(parse): | ||
| # Matches the Go server, which rejects a set-but-empty GRAM_PARTITION. | ||
| with pytest.raises(click.MissingParameter, match="partition"): | ||
| parse([], env={"GRAM_PARTITION": ""}) | ||
|
|
||
|
|
||
| def test_explicit_partition_wins_over_empty_environment_variable(parse): | ||
| # Matches the Go server, where a --partition flag takes precedence over | ||
| # the environment variable. | ||
| assert parse(["--partition", "eu"], env={"GRAM_PARTITION": ""})["partition"] == "eu" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("bad", ["apac", "US", ""]) | ||
| def test_unknown_partition_is_rejected(parse, bad): | ||
| with pytest.raises(click.UsageError): | ||
| parse(["--partition", bad]) |
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,86 @@ | ||
| package gram | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/urfave/cli/v2" | ||
|
|
||
| "github.com/speakeasy-api/gram/server/internal/partition" | ||
| ) | ||
|
|
||
| const partitionFlag = "partition" | ||
|
|
||
| // partitionCLIFlag selects the partition every command of this binary runs | ||
| // in. It has no default: a deployment that forgets it fails at startup rather | ||
| // than running as the wrong partition. It is a global flag, so a command added | ||
| // later requires it and gets the partition on its logs without registering the | ||
| // flag; commands that set up OpenTelemetry still pass PullPartition to | ||
| // o11y.SetupOTelSDK, which rejects an empty partition. | ||
| func partitionCLIFlag() cli.Flag { | ||
| return &cli.StringFlag{ | ||
| Name: partitionFlag, | ||
| Usage: fmt.Sprintf("Partition this deployment runs in, one of: %s. Required. A partition is an independent production deployment with its own data stores and hosts. As a global flag it goes before the command name.", strings.Join(partition.Names(), ", ")), | ||
| EnvVars: []string{"GRAM_PARTITION"}, | ||
| } | ||
| } | ||
|
|
||
| // partitionFromCLI parses the global partition flag. It returns an empty | ||
| // partition and no error only for invocations that print help or the version, | ||
| // which must work without deployment configuration. A GRAM_PARTITION that is | ||
| // set but empty is rejected. | ||
| func partitionFromCLI(c *cli.Context) (partition.Partition, error) { | ||
| if !c.IsSet(partitionFlag) { | ||
| if printsHelpOrVersion(c.Args().Slice()) { | ||
| return "", nil | ||
| } | ||
| return "", fmt.Errorf("--%s (GRAM_PARTITION) is required: set it to one of %s", partitionFlag, strings.Join(partition.Names(), ", ")) | ||
| } | ||
|
|
||
| p, err := partition.Parse(c.String(partitionFlag)) | ||
| if err != nil { | ||
| return "", fmt.Errorf("invalid --%s: %w", partitionFlag, err) | ||
| } | ||
|
|
||
| return p, nil | ||
| } | ||
|
|
||
| // printsHelpOrVersion reports whether the arguments after the global flags | ||
| // only print help or the version: no command, the help or version command, or | ||
| // a help flag anywhere. | ||
| func printsHelpOrVersion(args []string) bool { | ||
| if len(args) == 0 { | ||
| return true | ||
| } | ||
|
|
||
| switch args[0] { | ||
| case "help", "h", "version": | ||
| return true | ||
| } | ||
|
|
||
| return slices.ContainsFunc(args, func(arg string) bool { | ||
| return arg == "--help" || arg == "-h" | ||
| }) | ||
| } | ||
|
|
||
| type partitionKey struct{} | ||
|
|
||
| // PushPartition records the partition the root command parsed, so commands | ||
| // read the validated value instead of parsing the flag again. | ||
| func PushPartition(ctx context.Context, p partition.Partition) context.Context { | ||
| return context.WithValue(ctx, partitionKey{}, p) | ||
| } | ||
|
|
||
| // PullPartition returns the partition recorded by PushPartition. It panics | ||
| // when none was recorded, because the root command records one before any | ||
| // command other than help or version runs. | ||
| func PullPartition(ctx context.Context) partition.Partition { | ||
| p, ok := ctx.Value(partitionKey{}).(partition.Partition) | ||
| if !ok { | ||
| panic("PullPartition: partition not found in context") | ||
| } | ||
|
|
||
| return p | ||
| } | ||
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.