From b558b0d537bda66e59ec42de45fefa839636f756 Mon Sep 17 00:00:00 2001 From: Nicolas Takashi Date: Mon, 16 Nov 2026 15:28:18 +0000 Subject: [PATCH 1/6] chore(deps): bump dash0-api-client-go to v1.34.0 v1.34.0 splits profiles.Configuration into the stored schema and a ResolvedConfiguration that is read through methods. Only a Store builds one, so tests now get theirs from testutil.ResolvedConfiguration. The profile lookup no longer refreshes the OAuth token. The client now asks the token provider once when it is built, so a rejected refresh token still fails with the "run dash0 login" hint. --- cmd/dash0/main.go | 2 +- go.mod | 2 +- go.sum | 4 +- internal/client/authtokenfile_test.go | 5 +- internal/client/client.go | 72 +++++++++++++-------- internal/client/client_test.go | 56 ++++++++--------- internal/client/dataset.go | 8 +-- internal/client/rawhttp.go | 12 ++-- internal/config/config_cmd.go | 8 ++- internal/config/profile_selector.go | 2 +- internal/config/profile_selector_test.go | 16 ++--- internal/config/profiles_test.go | 80 ++++++++++++------------ internal/metrics/instant_cmd_test.go | 26 ++++---- internal/otlp/proxy_lifecycle.go | 14 ++--- internal/otlp/proxy_lifecycle_test.go | 27 ++++---- internal/testutil/profiles.go | 22 +++++++ 16 files changed, 203 insertions(+), 153 deletions(-) create mode 100644 internal/testutil/profiles.go diff --git a/cmd/dash0/main.go b/cmd/dash0/main.go index 043256e4..692d15c8 100644 --- a/cmd/dash0/main.go +++ b/cmd/dash0/main.go @@ -258,7 +258,7 @@ func skillHintSuppressed() bool { // This approach avoids the need to predict which command will run — something // that cobra's Traverse cannot reliably do when persistent flags like -X // precede the subcommand name. -func loadConfig() *profiles.Configuration { +func loadConfig() *profiles.ResolvedConfiguration { cfg, err := profiles.ResolveConfiguration("", "") if err != nil { return nil diff --git a/go.mod b/go.mod index 361686b6..a5120e4d 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.27.3 require ( github.com/cli/browser v1.3.0 - github.com/dash0hq/dash0-api-client-go v1.33.0 + github.com/dash0hq/dash0-api-client-go v1.34.0 github.com/google/uuid v1.6.0 github.com/muesli/termenv v0.16.0 github.com/pmezard/go-difflib v1.0.0 diff --git a/go.sum b/go.sum index 69f4c8ac..797b9dea 100644 --- a/go.sum +++ b/go.sum @@ -31,8 +31,8 @@ github.com/cpuguy83/dockercfg v0.3.2/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHf github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s= github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE= -github.com/dash0hq/dash0-api-client-go v1.33.0 h1:5z1DT1VLirH+eS0M5efxBxvUPqlRHWC5onmZE9JlFNk= -github.com/dash0hq/dash0-api-client-go v1.33.0/go.mod h1:r0AJKYZy8Y6hmGhsVbHbCEOEH3bIUKcHlg/KeZs6dJo= +github.com/dash0hq/dash0-api-client-go v1.34.0 h1:eU0Ej9mt8NUNBb8JK+HqyKwWrW5Qg3BUiU0W8ssu2dA= +github.com/dash0hq/dash0-api-client-go v1.34.0/go.mod h1:r0AJKYZy8Y6hmGhsVbHbCEOEH3bIUKcHlg/KeZs6dJo= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= diff --git a/internal/client/authtokenfile_test.go b/internal/client/authtokenfile_test.go index 6197611d..015e5978 100644 --- a/internal/client/authtokenfile_test.go +++ b/internal/client/authtokenfile_test.go @@ -2,6 +2,7 @@ package client import ( "context" + "github.com/dash0hq/dash0-cli/internal/testutil" "os" "path/filepath" "testing" @@ -69,10 +70,10 @@ func TestApplyAuthTokenFile_Errors(t *testing.T) { } func TestNewRawHTTPConfig_FlagTokenOverridesStaticProfile(t *testing.T) { - ctx := profiles.WithConfiguration(context.Background(), &profiles.Configuration{ + ctx := profiles.WithConfiguration(context.Background(), testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ ApiUrl: "https://api.test.dash0.com", AuthToken: "auth_profile", - }) + })) cfg, err := NewRawHTTPConfig(ctx, "", "auth_flag") require.NoError(t, err) diff --git a/internal/client/client.go b/internal/client/client.go index f1862188..24d9dd89 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -33,15 +33,15 @@ func NewClientFromContext(ctx context.Context, apiUrl, authToken string) (dash0a return nil, translateConfigError(ctx, err) } cfg = resolved - apiUrl = resolved.ApiUrl - authToken = resolved.AuthToken + apiUrl = resolved.ApiUrl() + authToken = resolved.Token() } else { // Apply flag overrides on top of context configuration if apiUrl == "" { - apiUrl = cfg.ApiUrl + apiUrl = cfg.ApiUrl() } if authToken == "" { - authToken = cfg.AuthToken + authToken = cfg.Token() } } @@ -54,9 +54,14 @@ func NewClientFromContext(ctx context.Context, apiUrl, authToken string) (dash0a return nil, err } + tokenOption, err := authTokenOption(ctx, cfg, authToken) + if err != nil { + return nil, err + } + client, err := dash0api.NewClient( dash0api.WithApiUrl(apiUrl), - authTokenOption(cfg, authToken), + tokenOption, dash0api.WithUserAgent(version.UserAgent()), dash0api.WithMaxRetries(maxRetries), dash0api.WithRetryOnConflict(), @@ -76,8 +81,8 @@ func NewOtlpClientFromContext(ctx context.Context, otlpUrl, authToken string) (d var finalOtlpUrl, finalAuthToken string if cfg != nil { - finalOtlpUrl = cfg.OtlpUrl - finalAuthToken = cfg.AuthToken + finalOtlpUrl = cfg.OtlpUrl() + finalAuthToken = cfg.Token() } // Apply flag overrides @@ -107,9 +112,14 @@ func NewOtlpClientFromContext(ctx context.Context, otlpUrl, authToken string) (d return nil, err } + tokenOption, err := authTokenOption(ctx, cfg, finalAuthToken) + if err != nil { + return nil, err + } + client, err := dash0api.NewClient( dash0api.WithOtlpEndpoint(dash0api.OtlpEncodingJson, finalOtlpUrl), - authTokenOption(cfg, finalAuthToken), + tokenOption, dash0api.WithUserAgent(version.UserAgent()), dash0api.WithMaxRetries(maxRetries), ) @@ -127,8 +137,8 @@ func ResolveApiUrl(ctx context.Context, flagApiUrl string) string { if flagApiUrl != "" { return flagApiUrl } - if cfg := profiles.FromContext(ctx); cfg != nil && cfg.ApiUrl != "" { - return cfg.ApiUrl + if cfg := profiles.FromContext(ctx); cfg != nil && cfg.ApiUrl() != "" { + return cfg.ApiUrl() } return "" } @@ -218,8 +228,8 @@ func profileNameForUpdate(_ context.Context, displayName string) string { // // TODO(dash0-api-client-go #39): once Configuration records each field's source, // this and both inline copies collapse into one check. -func oauthShadowed(cfg *profiles.Configuration, resolvedAuthToken string) bool { - if cfg == nil || cfg.OAuth == nil { +func oauthShadowed(cfg *profiles.ResolvedConfiguration, resolvedAuthToken string) bool { + if cfg == nil || cfg.OAuth() == nil { return false } if os.Getenv(profiles.EnvAuthToken) != "" { @@ -227,15 +237,15 @@ func oauthShadowed(cfg *profiles.Configuration, resolvedAuthToken string) bool { } // The resolved token differs from what the profile would have supplied, so // it came from a flag override. - return resolvedAuthToken != "" && cfg.AuthToken != resolvedAuthToken + return resolvedAuthToken != "" && cfg.Token() != resolvedAuthToken } // useProfileProvider reports whether the auth token must come from the // profile's provider: only an OAuth profile that no static token shadows. // Anything else uses the resolved token as-is, so a flag override is never // replaced by the profile's own token. -func useProfileProvider(cfg *profiles.Configuration, resolvedAuthToken string) bool { - return cfg != nil && cfg.OAuth != nil && !oauthShadowed(cfg, resolvedAuthToken) +func useProfileProvider(cfg *profiles.ResolvedConfiguration, resolvedAuthToken string) bool { + return cfg != nil && cfg.OAuth() != nil && !oauthShadowed(cfg, resolvedAuthToken) } // authTokenOption picks how the API client authenticates. @@ -245,11 +255,19 @@ func useProfileProvider(cfg *profiles.Configuration, resolvedAuthToken string) b // so a token frozen at construction breaks any command that outlives it. // Anything else authenticates with the resolved token directly, because a // static auth_* token does not expire. -func authTokenOption(cfg *profiles.Configuration, resolvedAuthToken string) dash0api.ClientOption { - if useProfileProvider(cfg, resolvedAuthToken) { - return dash0api.WithAuthTokenProvider(cfg.AuthTokenProvider()) - } - return dash0api.WithAuthToken(resolvedAuthToken) +// +// The profile lookup does not refresh the token, so the provider is asked for +// one here: a rejected refresh token then fails with the re-authentication +// hint before any request, and the client reuses the token it cached. +func authTokenOption(ctx context.Context, cfg *profiles.ResolvedConfiguration, resolvedAuthToken string) (dash0api.ClientOption, error) { + if !useProfileProvider(cfg, resolvedAuthToken) { + return dash0api.WithAuthToken(resolvedAuthToken), nil + } + provider := cfg.AuthTokenProvider() + if _, err := provider.AuthToken(ctx); err != nil { + return nil, translateConfigError(ctx, err) + } + return dash0api.WithAuthTokenProvider(provider), nil } // checkOAuthEmpty surfaces a friendly "not authenticated" error when the @@ -257,11 +275,11 @@ func authTokenOption(cfg *profiles.Configuration, resolvedAuthToken string) dash // has run `dash0 login` against it yet, or the user has just logged out). // It returns nil when the profile is fine or when an env-var auth token is // shadowing the OAuth state. -func checkOAuthEmpty(ctx context.Context, cfg *profiles.Configuration, resolvedAuthToken string) error { - if cfg == nil || cfg.OAuth == nil { +func checkOAuthEmpty(ctx context.Context, cfg *profiles.ResolvedConfiguration, resolvedAuthToken string) error { + if cfg == nil || cfg.OAuth() == nil { return nil } - if cfg.OAuth.RefreshToken != "" { + if cfg.OAuth().RefreshToken != "" { return nil } // If the user has shadowed the OAuth profile with an explicit @@ -270,7 +288,7 @@ func checkOAuthEmpty(ctx context.Context, cfg *profiles.Configuration, resolvedA if os.Getenv(profiles.EnvAuthToken) != "" { return nil } - if resolvedAuthToken != "" && cfg.AuthToken != resolvedAuthToken { + if resolvedAuthToken != "" && cfg.Token() != resolvedAuthToken { // resolvedAuthToken came from a flag override; trust it. return nil } @@ -314,8 +332,8 @@ func checkOAuthEmpty(ctx context.Context, cfg *profiles.Configuration, resolvedA // shadows the OAuth state and is trusted as-is. That keeps the // "set DASH0_AUTH_TOKEN to a static auth_* token" workaround working // without forcing the user to convert the whole profile. -func checkOAuthOnOtlp(ctx context.Context, cfg *profiles.Configuration, resolvedAuthToken string) error { - if cfg == nil || cfg.OAuth == nil { +func checkOAuthOnOtlp(ctx context.Context, cfg *profiles.ResolvedConfiguration, resolvedAuthToken string) error { + if cfg == nil || cfg.OAuth() == nil { return nil // not an OAuth profile } // Env-var override: trust the static token shadow. @@ -324,7 +342,7 @@ func checkOAuthOnOtlp(ctx context.Context, cfg *profiles.Configuration, resolved } // Flag override: the resolved token differs from what the profile // would have supplied, so the user passed --auth-token explicitly. - if resolvedAuthToken != "" && cfg.AuthToken != resolvedAuthToken { + if resolvedAuthToken != "" && cfg.Token() != resolvedAuthToken { return nil } diff --git a/internal/client/client_test.go b/internal/client/client_test.go index 6dcded9a..e654bf08 100644 --- a/internal/client/client_test.go +++ b/internal/client/client_test.go @@ -134,11 +134,11 @@ func TestResolveMaxRetries_FlagExceedsMax(t *testing.T) { } func TestResolveDataset_FlagTakesPrecedence(t *testing.T) { - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ ApiUrl: "https://api.test.dash0.com", AuthToken: "auth_test-token-12345", Dataset: "config-dataset", - } + }) ctx := profiles.WithConfiguration(context.Background(), cfg) result := ResolveDataset(ctx, "flag-dataset") @@ -147,11 +147,11 @@ func TestResolveDataset_FlagTakesPrecedence(t *testing.T) { } func TestResolveDataset_FallsBackToConfig(t *testing.T) { - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ ApiUrl: "https://api.test.dash0.com", AuthToken: "auth_test-token-12345", Dataset: "config-dataset", - } + }) ctx := profiles.WithConfiguration(context.Background(), cfg) result := ResolveDataset(ctx, "") @@ -160,10 +160,10 @@ func TestResolveDataset_FallsBackToConfig(t *testing.T) { } func TestResolveDataset_NilWhenEmpty(t *testing.T) { - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ ApiUrl: "https://api.test.dash0.com", AuthToken: "auth_test-token-12345", - } + }) ctx := profiles.WithConfiguration(context.Background(), cfg) result := ResolveDataset(ctx, "") @@ -171,11 +171,11 @@ func TestResolveDataset_NilWhenEmpty(t *testing.T) { } func TestResolveDataset_NilForDefaultInConfig(t *testing.T) { - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ ApiUrl: "https://api.test.dash0.com", AuthToken: "auth_test-token-12345", Dataset: "default", - } + }) ctx := profiles.WithConfiguration(context.Background(), cfg) result := ResolveDataset(ctx, "") @@ -188,11 +188,11 @@ func TestResolveDataset_NilForDefaultFlag(t *testing.T) { } func TestResolveDataset_DefaultFlagOverridesConfig(t *testing.T) { - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ ApiUrl: "https://api.test.dash0.com", AuthToken: "auth_test-token-12345", Dataset: "config-dataset", - } + }) ctx := profiles.WithConfiguration(context.Background(), cfg) result := ResolveDataset(ctx, "default") @@ -271,8 +271,8 @@ func TestResolveDataset_EnvCredentialsKeepProfileDataset(t *testing.T) { cfg, err := profiles.ResolveConfiguration("", "") require.NoError(t, err) - assert.Equal(t, "https://api.env.dash0.com", cfg.ApiUrl) - assert.Equal(t, "auth_env-token-12345", cfg.AuthToken) + assert.Equal(t, "https://api.env.dash0.com", cfg.ApiUrl()) + assert.Equal(t, "auth_env-token-12345", cfg.Token()) result := ResolveDataset(profiles.WithConfiguration(context.Background(), cfg), "") require.NotNil(t, result) @@ -293,11 +293,11 @@ func TestNewRawHTTPConfig_DatasetFromEnvWithoutContext(t *testing.T) { } func TestNewRawHTTPConfig_DatasetFromContext(t *testing.T) { - profileCfg := &profiles.Configuration{ + profileCfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ ApiUrl: "https://api.test.dash0.com", AuthToken: "auth_test-token-12345", Dataset: "profile-dataset", - } + }) ctx := profiles.WithConfiguration(context.Background(), profileCfg) cfg, err := NewRawHTTPConfig(ctx, "", "") @@ -429,16 +429,16 @@ func TestHandleAPIError_NotFoundWithoutContext(t *testing.T) { func TestCheckOAuthOnOtlp_RefusesOAuthProfile(t *testing.T) { os.Unsetenv("DASH0_AUTH_TOKEN") - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ ApiUrl: "https://api.eu-west-1.aws.dash0.com", AuthToken: "dash0_at_oauth_access_xyz", OAuth: &profiles.OAuthState{ ClientID: "client-abc", RefreshToken: "dash0_rt_xyz", }, - } + }) - err := checkOAuthOnOtlp(context.Background(), cfg, cfg.AuthToken) + err := checkOAuthOnOtlp(context.Background(), cfg, cfg.Token()) if err == nil { t.Fatalf("expected an error for OAuth profile + no override, got nil") } @@ -455,12 +455,12 @@ func TestCheckOAuthOnOtlp_RefusesOAuthProfile(t *testing.T) { func TestCheckOAuthOnOtlp_StaticProfilePassesThrough(t *testing.T) { os.Unsetenv("DASH0_AUTH_TOKEN") - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ ApiUrl: "https://api.eu-west-1.aws.dash0.com", AuthToken: "auth_static_xxx", - } + }) - if err := checkOAuthOnOtlp(context.Background(), cfg, cfg.AuthToken); err != nil { + if err := checkOAuthOnOtlp(context.Background(), cfg, cfg.Token()); err != nil { t.Errorf("expected nil for static profile, got %v", err) } } @@ -473,14 +473,14 @@ func TestCheckOAuthOnOtlp_EnvOverrideShadowsOAuth(t *testing.T) { os.Setenv("DASH0_AUTH_TOKEN", "auth_static_override_xxx") defer os.Unsetenv("DASH0_AUTH_TOKEN") - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ ApiUrl: "https://api.eu-west-1.aws.dash0.com", AuthToken: "dash0_at_oauth_access_xyz", OAuth: &profiles.OAuthState{ ClientID: "client-abc", RefreshToken: "dash0_rt_xyz", }, - } + }) if err := checkOAuthOnOtlp(context.Background(), cfg, "auth_static_override_xxx"); err != nil { t.Errorf("expected env-var override to suppress the gate, got %v", err) @@ -488,19 +488,19 @@ func TestCheckOAuthOnOtlp_EnvOverrideShadowsOAuth(t *testing.T) { } // TestCheckOAuthOnOtlp_FlagOverrideShadowsOAuth asserts that a per-command -// --auth-token override (resolvedAuthToken != cfg.AuthToken) bypasses the +// --auth-token override (resolvedAuthToken != cfg.Token()) bypasses the // gate. func TestCheckOAuthOnOtlp_FlagOverrideShadowsOAuth(t *testing.T) { os.Unsetenv("DASH0_AUTH_TOKEN") - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ ApiUrl: "https://api.eu-west-1.aws.dash0.com", AuthToken: "dash0_at_oauth_access_xyz", OAuth: &profiles.OAuthState{ ClientID: "client-abc", RefreshToken: "dash0_rt_xyz", }, - } + }) if err := checkOAuthOnOtlp(context.Background(), cfg, "auth_flag_override_xxx"); err != nil { t.Errorf("expected --auth-token override to suppress the gate, got %v", err) @@ -597,16 +597,16 @@ func TestCheckOAuthOnOtlp_AgentModeMessage(t *testing.T) { agentmode.Enabled = true defer func() { agentmode.Enabled = prev }() - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ ApiUrl: "https://api.eu-west-1.aws.dash0.com", AuthToken: "dash0_at_oauth_access_xyz", OAuth: &profiles.OAuthState{ ClientID: "client-abc", RefreshToken: "dash0_rt_xyz", }, - } + }) - err := checkOAuthOnOtlp(context.Background(), cfg, cfg.AuthToken) + err := checkOAuthOnOtlp(context.Background(), cfg, cfg.Token()) if err == nil { t.Fatalf("expected an error in agent mode, got nil") } diff --git a/internal/client/dataset.go b/internal/client/dataset.go index 26311d62..1bad6989 100644 --- a/internal/client/dataset.go +++ b/internal/client/dataset.go @@ -27,16 +27,16 @@ func ResolveDataset(ctx context.Context, flagDataset string) *string { return dash0api.DatasetPtr(flagDataset) } if cfg := profiles.FromContext(ctx); cfg != nil { - if cfg.Dataset != "" { - return dash0api.DatasetPtr(cfg.Dataset) + if cfg.Dataset() != "" { + return dash0api.DatasetPtr(cfg.Dataset()) } return nil } if envDataset := os.Getenv(profiles.EnvDataset); envDataset != "" { return dash0api.DatasetPtr(envDataset) } - if cfg, err := profiles.ResolveConfigurationContext(ctx, "", ""); err == nil && cfg.Dataset != "" { - return dash0api.DatasetPtr(cfg.Dataset) + if cfg, err := profiles.ResolveConfigurationContext(ctx, "", ""); err == nil && cfg.Dataset() != "" { + return dash0api.DatasetPtr(cfg.Dataset()) } return nil } diff --git a/internal/client/rawhttp.go b/internal/client/rawhttp.go index 1ba46918..bd49f165 100644 --- a/internal/client/rawhttp.go +++ b/internal/client/rawhttp.go @@ -41,20 +41,20 @@ func NewRawHTTPConfig(ctx context.Context, apiUrl, authToken string) (*RawHTTPCo if cfg := profiles.FromContext(ctx); cfg != nil { if resolvedApiUrl == "" { - resolvedApiUrl = cfg.ApiUrl + resolvedApiUrl = cfg.ApiUrl() } if resolvedAuthToken == "" { - resolvedAuthToken = cfg.AuthToken + resolvedAuthToken = cfg.Token() } - resolvedDataset = cfg.Dataset + resolvedDataset = cfg.Dataset() } else { resolved, err := profiles.ResolveConfiguration(resolvedApiUrl, resolvedAuthToken) if err != nil { return nil, fmt.Errorf("failed to resolve configuration: %w", err) } - resolvedApiUrl = resolved.ApiUrl - resolvedAuthToken = resolved.AuthToken - resolvedDataset = resolved.Dataset + resolvedApiUrl = resolved.ApiUrl() + resolvedAuthToken = resolved.Token() + resolvedDataset = resolved.Dataset() } if resolvedApiUrl == "" { diff --git a/internal/config/config_cmd.go b/internal/config/config_cmd.go index 63a75da7..eede93f9 100644 --- a/internal/config/config_cmd.go +++ b/internal/config/config_cmd.go @@ -101,7 +101,13 @@ The --config-dir flag or the DASH0_CONFIG_DIR environment variable changes the c if err != nil { return err } - config = resolved + config = &profiles.Configuration{ + ApiUrl: resolved.ApiUrl(), + AuthToken: resolved.Token(), + OtlpUrl: resolved.OtlpUrl(), + Dataset: resolved.Dataset(), + OAuth: resolved.OAuth(), + } } else { store, err := profiles.NewStore() if err != nil { diff --git a/internal/config/profile_selector.go b/internal/config/profile_selector.go index 4b00f458..e26fe0eb 100644 --- a/internal/config/profile_selector.go +++ b/internal/config/profile_selector.go @@ -91,7 +91,7 @@ func ProfileSelectorFromContext(ctx context.Context) ProfileSelector { // // The returned configuration records the profile it came from, which is what // lets the client refresh its OAuth access token per request. -func ResolveConfigurationForProfile(ctx context.Context, profileName string) (*profiles.Configuration, error) { +func ResolveConfigurationForProfile(ctx context.Context, profileName string) (*profiles.ResolvedConfiguration, error) { if profileName == "" { return nil, fmt.Errorf("profile name must not be empty") } diff --git a/internal/config/profile_selector_test.go b/internal/config/profile_selector_test.go index eba04d25..17e09c71 100644 --- a/internal/config/profile_selector_test.go +++ b/internal/config/profile_selector_test.go @@ -136,11 +136,11 @@ func TestResolveConfigurationForProfile(t *testing.T) { if err != nil { t.Fatalf("unexpected error: %v", err) } - if cfg.ApiUrl != "https://api-prod.example.com" { - t.Errorf("expected prod api url, got %q", cfg.ApiUrl) + if cfg.ApiUrl() != "https://api-prod.example.com" { + t.Errorf("expected prod api url, got %q", cfg.ApiUrl()) } - if cfg.Dataset != "prod-ds" { - t.Errorf("expected prod dataset, got %q", cfg.Dataset) + if cfg.Dataset() != "prod-ds" { + t.Errorf("expected prod dataset, got %q", cfg.Dataset()) } }) @@ -152,12 +152,12 @@ func TestResolveConfigurationForProfile(t *testing.T) { if err != nil { t.Fatalf("unexpected error: %v", err) } - if cfg.Dataset != "env-override-ds" { - t.Errorf("expected env override, got %q", cfg.Dataset) + if cfg.Dataset() != "env-override-ds" { + t.Errorf("expected env override, got %q", cfg.Dataset()) } // Other fields should still come from the profile. - if cfg.ApiUrl != "https://api-prod.example.com" { - t.Errorf("expected prod api url, got %q", cfg.ApiUrl) + if cfg.ApiUrl() != "https://api-prod.example.com" { + t.Errorf("expected prod api url, got %q", cfg.ApiUrl()) } }) diff --git a/internal/config/profiles_test.go b/internal/config/profiles_test.go index d8916294..a46f7901 100644 --- a/internal/config/profiles_test.go +++ b/internal/config/profiles_test.go @@ -436,11 +436,11 @@ func TestGetActiveConfiguration(t *testing.T) { t.Fatalf("Failed to get active configuration: %v", err) } - if config.ApiUrl != "https://env.example.com" { - t.Errorf("Expected API URL https://env.example.com, got %s", config.ApiUrl) + if config.ApiUrl() != "https://env.example.com" { + t.Errorf("Expected API URL https://env.example.com, got %s", config.ApiUrl()) } - if config.AuthToken != "env-token" { - t.Errorf("Expected auth token env-token, got %s", config.AuthToken) + if config.Token() != "env-token" { + t.Errorf("Expected auth token env-token, got %s", config.Token()) } }) @@ -475,11 +475,11 @@ func TestGetActiveConfiguration(t *testing.T) { t.Fatalf("Failed to get active configuration: %v", err) } - if config.ApiUrl != "https://test1.example.com" { - t.Errorf("Expected API URL https://test1.example.com, got %s", config.ApiUrl) + if config.ApiUrl() != "https://test1.example.com" { + t.Errorf("Expected API URL https://test1.example.com, got %s", config.ApiUrl()) } - if config.AuthToken != "token1" { - t.Errorf("Expected auth token token1, got %s", config.AuthToken) + if config.Token() != "token1" { + t.Errorf("Expected auth token token1, got %s", config.Token()) } }) @@ -503,11 +503,11 @@ func TestGetActiveConfiguration(t *testing.T) { t.Fatalf("Failed to get active configuration: %v", err) } - if config.OtlpUrl != "https://otlp.example.com" { - t.Errorf("Expected OTLP URL https://otlp.example.com, got %s", config.OtlpUrl) + if config.OtlpUrl() != "https://otlp.example.com" { + t.Errorf("Expected OTLP URL https://otlp.example.com, got %s", config.OtlpUrl()) } - if config.AuthToken != "env-token" { - t.Errorf("Expected auth token env-token, got %s", config.AuthToken) + if config.Token() != "env-token" { + t.Errorf("Expected auth token env-token, got %s", config.Token()) } }) @@ -532,8 +532,8 @@ func TestGetActiveConfiguration(t *testing.T) { t.Fatalf("Failed to get active configuration: %v", err) } - if config.Dataset != "my-dataset" { - t.Errorf("Expected dataset my-dataset, got %s", config.Dataset) + if config.Dataset() != "my-dataset" { + t.Errorf("Expected dataset my-dataset, got %s", config.Dataset()) } }) @@ -569,8 +569,8 @@ func TestGetActiveConfiguration(t *testing.T) { t.Fatalf("Failed to get active configuration: %v", err) } - if config.Dataset != "env-dataset" { - t.Errorf("Expected dataset env-dataset, got %s", config.Dataset) + if config.Dataset() != "env-dataset" { + t.Errorf("Expected dataset env-dataset, got %s", config.Dataset()) } }) @@ -605,8 +605,8 @@ func TestGetActiveConfiguration(t *testing.T) { t.Fatalf("Failed to get active configuration: %v", err) } - if config.Dataset != "profile-dataset" { - t.Errorf("Expected dataset profile-dataset, got %s", config.Dataset) + if config.Dataset() != "profile-dataset" { + t.Errorf("Expected dataset profile-dataset, got %s", config.Dataset()) } }) @@ -641,8 +641,8 @@ func TestGetActiveConfiguration(t *testing.T) { t.Fatalf("Failed to get active configuration: %v", err) } - if config.OtlpUrl != "https://otlp-override.example.com" { - t.Errorf("Expected OTLP URL https://otlp-override.example.com, got %s", config.OtlpUrl) + if config.OtlpUrl() != "https://otlp-override.example.com" { + t.Errorf("Expected OTLP URL https://otlp-override.example.com, got %s", config.OtlpUrl()) } }) } @@ -703,11 +703,11 @@ func TestResolveConfiguration(t *testing.T) { t.Fatalf("Failed to resolve configuration: %v", err) } - if config.ApiUrl != "https://env.example.com" { - t.Errorf("Expected API URL https://env.example.com, got %s", config.ApiUrl) + if config.ApiUrl() != "https://env.example.com" { + t.Errorf("Expected API URL https://env.example.com, got %s", config.ApiUrl()) } - if config.AuthToken != "env-token" { - t.Errorf("Expected auth token env-token, got %s", config.AuthToken) + if config.Token() != "env-token" { + t.Errorf("Expected auth token env-token, got %s", config.Token()) } }) @@ -762,11 +762,11 @@ func TestResolveConfiguration(t *testing.T) { t.Logf("Resolved config: %+v", resolvedCfg) // Test assertions - if resolvedCfg.ApiUrl != "https://override.example.com" { - t.Errorf("Expected API URL https://override.example.com, got %s", resolvedCfg.ApiUrl) + if resolvedCfg.ApiUrl() != "https://override.example.com" { + t.Errorf("Expected API URL https://override.example.com, got %s", resolvedCfg.ApiUrl()) } - if resolvedCfg.AuthToken != "original-token" { - t.Errorf("Expected auth token original-token, got %s", resolvedCfg.AuthToken) + if resolvedCfg.Token() != "original-token" { + t.Errorf("Expected auth token original-token, got %s", resolvedCfg.Token()) } }) @@ -799,14 +799,14 @@ func TestResolveConfiguration(t *testing.T) { t.Fatalf("Expected no error for OTLP-only config, got: %v", err) } - if config.OtlpUrl != "https://otlp.example.com" { - t.Errorf("Expected OTLP URL https://otlp.example.com, got %s", config.OtlpUrl) + if config.OtlpUrl() != "https://otlp.example.com" { + t.Errorf("Expected OTLP URL https://otlp.example.com, got %s", config.OtlpUrl()) } - if config.AuthToken != "env-token" { - t.Errorf("Expected auth token env-token, got %s", config.AuthToken) + if config.Token() != "env-token" { + t.Errorf("Expected auth token env-token, got %s", config.Token()) } - if config.ApiUrl != "" { - t.Errorf("Expected empty API URL, got %s", config.ApiUrl) + if config.ApiUrl() != "" { + t.Errorf("Expected empty API URL, got %s", config.ApiUrl()) } }) @@ -826,8 +826,8 @@ func TestResolveConfiguration(t *testing.T) { t.Fatalf("Failed to resolve configuration: %v", err) } - if config.Dataset != "env-dataset" { - t.Errorf("Expected dataset env-dataset, got %s", config.Dataset) + if config.Dataset() != "env-dataset" { + t.Errorf("Expected dataset env-dataset, got %s", config.Dataset()) } }) @@ -847,8 +847,8 @@ func TestResolveConfiguration(t *testing.T) { t.Fatalf("Failed to resolve configuration: %v", err) } - if config.Dataset != "flag-dataset" { - t.Errorf("Expected dataset flag-dataset, got %s", config.Dataset) + if config.Dataset() != "flag-dataset" { + t.Errorf("Expected dataset flag-dataset, got %s", config.Dataset()) } }) @@ -864,8 +864,8 @@ func TestResolveConfiguration(t *testing.T) { t.Fatalf("Expected no error for OTLP flag config, got: %v", err) } - if config.OtlpUrl != "https://otlp-flag.example.com" { - t.Errorf("Expected OTLP URL https://otlp-flag.example.com, got %s", config.OtlpUrl) + if config.OtlpUrl() != "https://otlp-flag.example.com" { + t.Errorf("Expected OTLP URL https://otlp-flag.example.com, got %s", config.OtlpUrl()) } }) } diff --git a/internal/metrics/instant_cmd_test.go b/internal/metrics/instant_cmd_test.go index 6e34ad6a..4eedebe1 100644 --- a/internal/metrics/instant_cmd_test.go +++ b/internal/metrics/instant_cmd_test.go @@ -3,6 +3,7 @@ package metrics import ( "context" "encoding/json" + "github.com/dash0hq/dash0-cli/internal/testutil" "io" "net/http" "net/http/httptest" @@ -23,13 +24,13 @@ func newTestInstantResponse() QueryInstantResponse { ResultType string `json:"resultType"` Result []struct { Metric map[string]string `json:"metric"` - Value []any `json:"value"` + Value []any `json:"value"` } `json:"result"` }{ ResultType: "vector", Result: []struct { Metric map[string]string `json:"metric"` - Value []any `json:"value"` + Value []any `json:"value"` }{ { Metric: map[string]string{ @@ -170,7 +171,7 @@ func TestInstantCmdCSVOutput(t *testing.T) { output := string(out) lines := strings.Split(strings.TrimSpace(output), "\n") - require.GreaterOrEqual(t, len(lines), 2) // header + at least 1 row + require.GreaterOrEqual(t, len(lines), 2) // header + at least 1 row assert.Contains(t, lines[0], "timestamp") // CSV header uses canonical keys assert.Contains(t, lines[0], "value") } @@ -195,7 +196,7 @@ func TestInstantCmdCSVSkipHeader(t *testing.T) { output := string(out) lines := strings.Split(strings.TrimSpace(output), "\n") - assert.Len(t, lines, 1) // only data, no header + assert.Len(t, lines, 1) // only data, no header assert.NotContains(t, lines[0], "__name__") // no header keys } @@ -334,17 +335,18 @@ func TestInstantCmdHonorsProfileFromContext(t *testing.T) { })) t.Cleanup(contextServer.Close) + // Resolve the profile before setting the env vars, which a Store layers on top. + ctx := profiles.WithConfiguration(context.Background(), testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ + ApiUrl: contextServer.URL, + AuthToken: "auth_context_token", + Dataset: "context-dataset", + })) + t.Setenv("DASH0_CONFIG_DIR", t.TempDir()) t.Setenv("DASH0_API_URL", envServer.URL) t.Setenv("DASH0_AUTH_TOKEN", "auth_env_token") t.Setenv("DASH0_DATASET", "env-dataset") - ctx := profiles.WithConfiguration(context.Background(), &profiles.Configuration{ - ApiUrl: contextServer.URL, - AuthToken: "auth_context_token", - Dataset: "context-dataset", - }) - cmd := newInstantCmd() cmd.SetContext(ctx) cmd.SetArgs([]string{"--promql", "up"}) @@ -372,11 +374,11 @@ func TestInstantCmdDatasetFlagOverridesContextProfile(t *testing.T) { t.Setenv("DASH0_CONFIG_DIR", t.TempDir()) - ctx := profiles.WithConfiguration(context.Background(), &profiles.Configuration{ + ctx := profiles.WithConfiguration(context.Background(), testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ ApiUrl: server.URL, AuthToken: "auth_context_token", Dataset: "context-dataset", - }) + })) cmd := newInstantCmd() cmd.SetContext(ctx) diff --git a/internal/otlp/proxy_lifecycle.go b/internal/otlp/proxy_lifecycle.go index d100c2ee..70be100d 100644 --- a/internal/otlp/proxy_lifecycle.go +++ b/internal/otlp/proxy_lifecycle.go @@ -289,7 +289,7 @@ func waitWithDeadline(wg *sync.WaitGroup, ctx context.Context) { // the resolved configuration plus a display-friendly profile name for // the banner. Errors are user-facing: missing profile gets the // `dash0 config profiles create` hint per AE7. -func resolveProxyConfig(ctx context.Context, flags *proxyFlags) (*profiles.Configuration, string, error) { +func resolveProxyConfig(ctx context.Context, flags *proxyFlags) (*profiles.ResolvedConfiguration, string, error) { cfg := profiles.FromContext(ctx) // Effective values after env-var + flag overrides. @@ -297,10 +297,10 @@ func resolveProxyConfig(ctx context.Context, flags *proxyFlags) (*profiles.Confi authToken := flags.AuthToken if cfg != nil { if otlpURL == "" { - otlpURL = cfg.OtlpUrl + otlpURL = cfg.OtlpUrl() } if authToken == "" { - authToken = cfg.AuthToken + authToken = cfg.Token() } } @@ -321,7 +321,7 @@ func resolveProxyConfig(ctx context.Context, flags *proxyFlags) (*profiles.Confi // name so the banner reads `profile: dev` instead of a placeholder. // 3. Fallback `env/flags` — only reached when no profile is on disk // and connection settings came from env vars or CLI flags. -func resolveProfileNameForBanner(ctx context.Context, cfg *profiles.Configuration) string { +func resolveProfileNameForBanner(ctx context.Context, cfg *profiles.ResolvedConfiguration) string { if sel := config.ProfileSelectorFromContext(ctx); sel.IsSet() { return sel.Name } @@ -352,12 +352,12 @@ func lookupActiveProfileName() string { // datasetLabel returns the dataset string for human-facing banners. The // resolved-dataset *string in dash0api is nil for "default"; we expand // that for readability. -func datasetLabel(cfg *profiles.Configuration, flagDataset string) string { +func datasetLabel(cfg *profiles.ResolvedConfiguration, flagDataset string) string { if flagDataset != "" { return flagDataset } - if cfg != nil && cfg.Dataset != "" { - return cfg.Dataset + if cfg != nil && cfg.Dataset() != "" { + return cfg.Dataset() } return "default" } diff --git a/internal/otlp/proxy_lifecycle_test.go b/internal/otlp/proxy_lifecycle_test.go index e5934e0e..7234eb50 100644 --- a/internal/otlp/proxy_lifecycle_test.go +++ b/internal/otlp/proxy_lifecycle_test.go @@ -2,6 +2,7 @@ package otlp import ( "context" + "github.com/dash0hq/dash0-cli/internal/testutil" "strings" "sync" "testing" @@ -18,11 +19,11 @@ func TestResolveProxyConfig_FromContextProfile(t *testing.T) { // store lookup returns nothing and we exercise the fallback branch. t.Setenv("DASH0_CONFIG_DIR", t.TempDir()) - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ OtlpUrl: "https://ingress.example.com", AuthToken: "tok", Dataset: "staging", - } + }) ctx := profiles.WithConfiguration(context.Background(), cfg) flags := &proxyFlags{} @@ -39,10 +40,10 @@ func TestResolveProxyConfig_FromContextProfile(t *testing.T) { } func TestResolveProxyConfig_FlagOverridesProfile(t *testing.T) { - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ OtlpUrl: "https://ingress.profile.com", AuthToken: "profile-tok", - } + }) ctx := profiles.WithConfiguration(context.Background(), cfg) flags := &proxyFlags{ OtlpUrl: "https://ingress.override.com", @@ -67,10 +68,10 @@ func TestResolveProxyConfig_NoProfileNoFlagsFails(t *testing.T) { } func TestResolveProxyConfig_MissingTokenFails(t *testing.T) { - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ OtlpUrl: "https://ingress.example.com", // AuthToken missing - } + }) ctx := profiles.WithConfiguration(context.Background(), cfg) flags := &proxyFlags{} @@ -81,10 +82,10 @@ func TestResolveProxyConfig_MissingTokenFails(t *testing.T) { } func TestResolveProxyConfig_MissingOtlpURLFails(t *testing.T) { - cfg := &profiles.Configuration{ + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{ AuthToken: "tok", // OtlpUrl missing - } + }) ctx := profiles.WithConfiguration(context.Background(), cfg) flags := &proxyFlags{} @@ -99,7 +100,7 @@ func TestResolveProfileNameForBanner_ExplicitSelectorWins(t *testing.T) { Name: "prod", Source: config.ProfileSourceFlag, }) - if got := resolveProfileNameForBanner(ctx, &profiles.Configuration{}); got != "prod" { + if got := resolveProfileNameForBanner(ctx, testutil.ResolvedConfiguration(t, "test", profiles.Configuration{})); got != "prod" { t.Errorf("got %q; want prod", got) } } @@ -108,7 +109,7 @@ func TestResolveProfileNameForBanner_ConfigOnly_NoStore(t *testing.T) { // Empty config dir → lookupActiveProfileName returns "" → fallback // label "active" is emitted. t.Setenv("DASH0_CONFIG_DIR", t.TempDir()) - if got := resolveProfileNameForBanner(context.Background(), &profiles.Configuration{}); got != "active" { + if got := resolveProfileNameForBanner(context.Background(), testutil.ResolvedConfiguration(t, "test", profiles.Configuration{})); got != "active" { t.Errorf("got %q; want 'active' (fallback when store has no active profile)", got) } } @@ -120,14 +121,14 @@ func TestResolveProfileNameForBanner_NoConfigNoSelector(t *testing.T) { } func TestDatasetLabel_FlagOverridesConfig(t *testing.T) { - cfg := &profiles.Configuration{Dataset: "from-config"} + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{Dataset: "from-config"}) if got := datasetLabel(cfg, "from-flag"); got != "from-flag" { t.Errorf("got %q; want from-flag", got) } } func TestDatasetLabel_ConfigUsedWhenNoFlag(t *testing.T) { - cfg := &profiles.Configuration{Dataset: "staging"} + cfg := testutil.ResolvedConfiguration(t, "test", profiles.Configuration{Dataset: "staging"}) if got := datasetLabel(cfg, ""); got != "staging" { t.Errorf("got %q; want staging", got) } @@ -137,7 +138,7 @@ func TestDatasetLabel_FallsBackToDefault(t *testing.T) { if got := datasetLabel(nil, ""); got != "default" { t.Errorf("got %q; want default", got) } - if got := datasetLabel(&profiles.Configuration{}, ""); got != "default" { + if got := datasetLabel(testutil.ResolvedConfiguration(t, "test", profiles.Configuration{}), ""); got != "default" { t.Errorf("got %q for empty cfg; want default", got) } } diff --git a/internal/testutil/profiles.go b/internal/testutil/profiles.go new file mode 100644 index 00000000..11eae1db --- /dev/null +++ b/internal/testutil/profiles.go @@ -0,0 +1,22 @@ +package testutil + +import ( + "context" + "testing" + + "github.com/dash0hq/dash0-api-client-go/profiles" + "github.com/stretchr/testify/require" +) + +// ResolvedConfiguration stores configuration as a profile in a temporary +// directory and reads it back: only a profiles.Store can build a +// profiles.ResolvedConfiguration. Environment variables still apply on top. +func ResolvedConfiguration(t *testing.T, profileName string, configuration profiles.Configuration) *profiles.ResolvedConfiguration { + t.Helper() + store, err := profiles.NewStore(profiles.WithConfigDir(t.TempDir())) + require.NoError(t, err) + require.NoError(t, store.AddProfile(profiles.Profile{Name: profileName, Configuration: configuration})) + resolved, err := store.GetConfigurationForProfile(context.Background(), profileName) + require.NoError(t, err) + return resolved +} From 209b08d8b0e9bf3c472170c405dc1446c6583336 Mon Sep 17 00:00:00 2001 From: Nicolas Takashi Date: Mon, 16 Nov 2026 15:28:18 +0000 Subject: [PATCH 2/6] feat(dashboards): upsert dashboards by the dash0.com/origin label apply, diff, create, update -f, and --since read a dashboard's key from dash0.com/origin, then metadata.dash0Extensions.id, then dash0.com/id. A dashboard kept as a Kubernetes custom resource loses dash0Extensions, so it used to become a new dashboard on every apply. An exported file carries the server UUID in dash0.com/id and now updates the dashboard it came from. A file whose origin and dash0Extensions.id differ fails validation before anything is written. Fixes #380 --- .../380-dashboards-upsert-by-origin.yaml | 9 ++ CLAUDE.md | 2 +- ...tekey_characterization_integration_test.go | 6 +- docs/commands.md | 15 +-- internal/apply/integration_test.go | 104 ++++++++++++++++++ internal/apply/since_integration_test.go | 11 ++ internal/asset/dashboard.go | 63 ++++++++++- internal/asset/document.go | 2 +- internal/asset/identifier.go | 12 +- internal/asset/identifier_test.go | 5 +- internal/asset/validate.go | 7 ++ internal/dashboards/create.go | 4 + internal/dashboards/update.go | 5 +- .../dashboards/update_integration_test.go | 41 ++++++- .../diff/upsertkey_characterization_test.go | 4 +- .../skill/content/references/dashboards.md | 6 +- 16 files changed, 266 insertions(+), 30 deletions(-) create mode 100644 .chloggen/380-dashboards-upsert-by-origin.yaml diff --git a/.chloggen/380-dashboards-upsert-by-origin.yaml b/.chloggen/380-dashboards-upsert-by-origin.yaml new file mode 100644 index 00000000..10b45fc2 --- /dev/null +++ b/.chloggen/380-dashboards-upsert-by-origin.yaml @@ -0,0 +1,9 @@ +change_type: enhancement +component: dashboards, apply +note: "Dashboards upsert by the `dash0.com/origin` label, so a dashboard kept as a Kubernetes custom resource no longer becomes a new dashboard on every apply." +issues: [380] +subtext: | + The CLI reads the identifier from `dash0.com/origin`, then `metadata.dash0Extensions.id`, then `dash0.com/id`. + A file exported with `dashboards get -o yaml` updates the dashboard it came from. + A file whose `dash0.com/origin` and `dash0Extensions.id` differ fails validation before anything is written. +change_logs: [] diff --git a/CLAUDE.md b/CLAUDE.md index fc2f8315..8ccfcec6 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -54,7 +54,7 @@ Detailed guidelines are split into focused documents: - Check rules: top-level `id` field (`PrometheusAlertRule.Id`) - Views: `metadata.labels["dash0.com/id"]` (`ViewLabels.Dash0Comid`) - Synthetic checks: `metadata.labels["dash0.com/id"]` - - Dashboards: `metadata.dash0Extensions.id` (`DashboardMetadataExtensions.Id`) + - Dashboards: `metadata.labels["dash0.com/origin"]`, then `metadata.dash0Extensions.id`, then `metadata.labels["dash0.com/id"]` (the server UUID). `asset.DashboardKeys` sets this order, because the client's `GetID` reads `dash0.com/id` before `dash0Extensions.id`. Read the key before `StripDashboardServerFields`, which clears `dash0.com/id`. An origin that differs from `dash0Extensions.id` fails validation. - PersesDashboard CRD: `metadata.labels["dash0.com/id"]` - PrometheusRule CRD (alerting and recording rules): `metadata.labels["dash0.com/id"]` - Spam filters (v1alpha1 and v1alpha2): `metadata.labels["dash0.com/id"]`, but `metadata.labels["dash0.com/origin"]` takes precedence as the upsert key when both are present diff --git a/cmd/dash0/updatekey_characterization_integration_test.go b/cmd/dash0/updatekey_characterization_integration_test.go index 7036bb26..3ddb84fa 100644 --- a/cmd/dash0/updatekey_characterization_integration_test.go +++ b/cmd/dash0/updatekey_characterization_integration_test.go @@ -183,10 +183,10 @@ func runUpdateAgainstNotFound(t *testing.T, command updateCommand, v updateIdent func TestUpdateKey_Characterization(t *testing.T) { want := map[string]map[updateIdentifiers]string{ "dashboards": { - updateWithNeither: "error: no dashboard id given: pass one as an argument, or set the id in the file", - updateWithOrigin: "error: no dashboard id given: pass one as an argument, or set the id in the file", + updateWithNeither: `error: no dashboard origin or id given: pass one as an argument, or set metadata.labels["dash0.com/origin"] in the file`, + updateWithOrigin: "GET /api/dashboards/o", updateWithID: "GET /api/dashboards/" + characterizationID, - updateWithBoth: "GET /api/dashboards/" + characterizationID, + updateWithBoth: `error: metadata.labels["dash0.com/origin"] ("o") and metadata.dash0Extensions.id ("` + characterizationID + `") must be equal; set only metadata.labels["dash0.com/origin"]`, }, "check-rules": { updateWithNeither: "error: no check rule id given: pass one as an argument, or set the id in the file", diff --git a/docs/commands.md b/docs/commands.md index 90e3a38b..f1c5f0e5 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -834,7 +834,7 @@ The identifier field location varies by asset kind: | Kind | Identifier field | Notes | |------|------------------|-------| -| `Dashboard` | `metadata.dash0Extensions.id` | | +| `Dashboard` | `metadata.labels["dash0.com/origin"]`, then `metadata.dash0Extensions.id`, then `metadata.labels["dash0.com/id"]` | `dash0.com/origin` is the recommended key, and a Kubernetes custom resource keeps it, while Kubernetes drops the unknown `dash0Extensions` field. When both `dash0.com/origin` and `dash0Extensions.id` are set, they must be equal, or the document fails validation before anything is written. `dash0.com/id` is the server UUID that `get -o yaml` exports, so an exported file updates the dashboard it came from | | `PersesDashboard` | `metadata.labels["dash0.com/id"]` | | | `CheckRule` | top-level `id` | | | `PrometheusRule` (alerting rules) | `metadata.labels["dash0.com/id"]` | For a single-alert CRD, this label is the alert's check-rule id directly. For a CRD with two or more alerting rules, each alert gets its own derived id (the CRD's label plus a slug of the alert's own composed name) instead of sharing the CRD's label directly — see [Multi-alert PrometheusRule CRDs](#multi-alert-prometheusrule-crds) | @@ -861,7 +861,8 @@ Notification channels have no user-settable ID field at all, and for SLOs origin For the remaining kinds — views, check rules, recording rules, and synthetic checks — the server treats origin as provenance only, so the CLI strips `dash0.com/origin` from outbound payloads and upserts against `dash0.com/id` (or the kind's equivalent ID field) instead. Stripping origin keeps an `apply` from claiming ownership of an asset that Terraform or the Kubernetes operator manages. -Dashboards are a third case: they use `metadata.dash0Extensions.id`, and the sibling `metadata.dash0Extensions.origin` field is deprecated in favor of it, so never use origin as a dashboard upsert key. +Dashboards upsert by `dash0.com/origin` too, and also accept `metadata.dash0Extensions.id`, which names the same identifier. +The deprecated `metadata.dash0Extensions.origin` field is never used as a key. When `list -o yaml` or `get -o yaml` exports an existing asset, the server-assigned ID is rendered into the correct field, so the export-edit-reapply workflow round-trips through the identifier automatically. @@ -1529,9 +1530,9 @@ Dashboard: apiVersion: dash0.com/v1alpha1 kind: Dashboard metadata: - name: a1b2c3d4-5678-90ab-cdef-1234567890ab - dash0Extensions: - id: a1b2c3d4-5678-90ab-cdef-1234567890ab + name: production-overview + labels: + dash0.com/origin: dash0-cli_production-overview spec: display: name: Production Overview @@ -1539,9 +1540,9 @@ spec: Pinning `apiVersion` on every user-authored file is a defense against silent migrations: today the server infers the default version when the field is omitted, but the same document with an explicit `apiVersion` will keep the same shape when a future default changes. -The user-defined identifier lives in `metadata.dash0Extensions.id` (see [asset identifiers](#asset-identifiers-and-idempotent-upsert)). +The user-defined identifier lives in `metadata.labels["dash0.com/origin"]`; files that set `metadata.dash0Extensions.id` instead keep working (see [asset identifiers](#asset-identifiers-and-idempotent-upsert)). `metadata.name` is the CRD-style metadata name and is independent of `spec.display.name`, which is the title shown in the UI. -Omitting `dash0Extensions.id` causes every `create` / `apply` to allocate a new server-assigned ID, breaking idempotency. +A document with no identifier makes every `create` / `apply` allocate a new server-assigned ID, breaking idempotency. PersesDashboard (Perses CRD, converted to a Dashboard on import): diff --git a/internal/apply/integration_test.go b/internal/apply/integration_test.go index 00a38c58..d479e1df 100644 --- a/internal/apply/integration_test.go +++ b/internal/apply/integration_test.go @@ -1342,6 +1342,110 @@ spec: } } +func TestApply_Dashboard_OriginOnly_UpdatesTheSameDashboardOnEveryApply(t *testing.T) { + testutil.SetupTestEnv(t) + + yamlFile := filepath.Join(t.TempDir(), "dashboard.yaml") + require.NoError(t, os.WriteFile(yamlFile, []byte(`kind: Dashboard +metadata: + name: checkout + labels: + dash0.com/origin: dash0-cli_checkout +spec: + display: + name: Checkout +`), 0644)) + + server := testutil.NewMockServer(t, testutil.FixturesDir()) + server.On(http.MethodGet, "/api/dashboards/dash0-cli_checkout", testutil.MockResponse{ + StatusCode: http.StatusNotFound, + BodyFile: testutil.FixtureDashboardsNotFound, + Validator: testutil.RequireHeaders, + }) + server.WithDashboardsUpdate(testutil.FixtureDashboardsImportSuccess) + + for range 2 { + cmd := NewApplyCmd() + cmd.SetArgs([]string{"-f", yamlFile, "--api-url", server.URL, "--auth-token", testAuthToken}) + var cmdErr error + testutil.CaptureStdout(t, func() { cmdErr = cmd.Execute() }) + require.NoError(t, cmdErr) + } + + var puts int + for _, req := range server.Requests() { + assert.NotEqual(t, http.MethodPost, req.Method, "an origin-only dashboard must never be created with POST") + if req.Method == http.MethodPut { + assert.Equal(t, "/api/dashboards/dash0-cli_checkout", req.Path) + puts++ + } + } + assert.Equal(t, 2, puts) +} + +func TestApply_Dashboard_ServerIDLabelOnly_UpdatesThatDashboard(t *testing.T) { + testutil.SetupTestEnv(t) + + const serverID = "0b7e5d2a-1c3f-4e8a-9b6d-2f4a8c1e7d90" + yamlFile := filepath.Join(t.TempDir(), "dashboard.yaml") + require.NoError(t, os.WriteFile(yamlFile, []byte(`kind: Dashboard +metadata: + name: checkout + labels: + dash0.com/id: `+serverID+` +spec: + display: + name: Checkout +`), 0644)) + + server := testutil.NewMockServer(t, testutil.FixturesDir()) + server.On(http.MethodGet, "/api/dashboards/"+serverID, testutil.MockResponse{ + StatusCode: http.StatusOK, + BodyFile: testutil.FixtureDashboardsImportSuccess, + Validator: testutil.RequireHeaders, + }) + server.WithDashboardsUpdate(testutil.FixtureDashboardsImportSuccess) + + cmd := NewApplyCmd() + cmd.SetArgs([]string{"-f", yamlFile, "--api-url", server.URL, "--auth-token", testAuthToken}) + var cmdErr error + testutil.CaptureStdout(t, func() { cmdErr = cmd.Execute() }) + require.NoError(t, cmdErr) + + assert.Nil(t, findRequest(server.Requests(), http.MethodPost, apiPathDashboards), "an exported dashboard must not be created a second time") + putReq := findRequest(server.Requests(), http.MethodPut, apiPathDashboards) + require.NotNil(t, putReq) + assert.Equal(t, "/api/dashboards/"+serverID, putReq.Path) +} + +func TestApply_Dashboard_OriginAndExtensionsIDDiffer_FailsBeforeAnyWrite(t *testing.T) { + testutil.SetupTestEnv(t) + + yamlFile := filepath.Join(t.TempDir(), "dashboard.yaml") + require.NoError(t, os.WriteFile(yamlFile, []byte(`kind: Dashboard +metadata: + name: checkout + labels: + dash0.com/origin: dash0-cli_checkout + dash0Extensions: + id: other-id +spec: + display: + name: Checkout +`), 0644)) + + server := testutil.NewMockServer(t, testutil.FixturesDir()) + + cmd := NewApplyCmd() + cmd.SetArgs([]string{"-f", yamlFile, "--api-url", server.URL, "--auth-token", testAuthToken}) + var cmdErr error + testutil.CaptureStdout(t, func() { cmdErr = cmd.Execute() }) + + require.Error(t, cmdErr) + assert.Contains(t, cmdErr.Error(), `metadata.labels["dash0.com/origin"] ("dash0-cli_checkout") and metadata.dash0Extensions.id ("other-id") must be equal`) + assert.Empty(t, server.Requests()) +} + func TestApply_Dashboard_RoundTrip(t *testing.T) { testutil.SetupTestEnv(t) diff --git a/internal/apply/since_integration_test.go b/internal/apply/since_integration_test.go index 3400133f..ab109f2b 100644 --- a/internal/apply/since_integration_test.go +++ b/internal/apply/since_integration_test.go @@ -648,10 +648,21 @@ metadata: spec: display: name: Shared Dashboard +` + const originYAML = `apiVersion: dash0.com/v1alpha1 +kind: Dashboard +metadata: + name: shared-dashboard + labels: + dash0.com/origin: shared-id +spec: + display: + name: Shared Dashboard ` for _, tt := range []struct{ name, before, after string }{ {"PersesDashboard to Dashboard", persesYAML, plainYAML}, {"Dashboard to PersesDashboard", plainYAML, persesYAML}, + {"dash0Extensions.id to dash0.com/origin", plainYAML, originYAML}, } { t.Run(tt.name, func(t *testing.T) { testutil.SetupTestEnv(t) diff --git a/internal/asset/dashboard.go b/internal/asset/dashboard.go index f2e4f543..975293e7 100644 --- a/internal/asset/dashboard.go +++ b/internal/asset/dashboard.go @@ -2,6 +2,7 @@ package asset import ( "context" + "fmt" "time" dash0api "github.com/dash0hq/dash0-api-client-go" @@ -15,6 +16,9 @@ import ( // A PUT to the ID of a deleted dashboard brings it back, which is reported as // ActionRestored. func ImportDashboard(ctx context.Context, apiClient dash0api.Client, dashboard *dash0api.DashboardDefinition, dataset *string) (ImportResult, error) { + if err := ValidateDashboard(dashboard); err != nil { + return ImportResult{}, err + } // A failed preflight does not block the write: the PUT reports the same error. plan, _ := PlanDashboard(ctx, apiClient, dashboard, dataset) id := plan.Key @@ -33,7 +37,7 @@ func ImportDashboard(ctx context.Context, apiClient dash0api.Client, dashboard * return ImportResult{}, err } - resultID := dash0api.GetDashboardID(result) + resultID := DashboardKey(result) if resultID != "" { id = resultID } @@ -51,12 +55,16 @@ func ImportDashboard(ctx context.Context, apiClient dash0api.Client, dashboard * // PlanDashboard reads the stored dashboard that ImportDashboard would write. // It returns any preflight error other than "not found". func PlanDashboard(ctx context.Context, apiClient dash0api.Client, dashboard *dash0api.DashboardDefinition, dataset *string) (ImportPlan, error) { + if err := ValidateDashboard(dashboard); err != nil { + return ImportPlan{}, err + } + // Read the key first: the strip clears the dash0.com/id label. + key := DashboardKey(dashboard) dash0api.StripDashboardServerFields(dashboard) - plan := ImportPlan{Action: ActionCreated, Proposed: dashboard} - if dashboard.Metadata.Dash0Extensions == nil || dashboard.Metadata.Dash0Extensions.Id == nil || *dashboard.Metadata.Dash0Extensions.Id == "" { + plan := ImportPlan{Key: key, Action: ActionCreated, Proposed: dashboard} + if key == "" { return plan, nil } - plan.Key = *dashboard.Metadata.Dash0Extensions.Id existing, err := apiClient.GetDashboard(ctx, plan.Key, dataset) if err != nil { return plan, ignoreNotFound(err) @@ -83,3 +91,50 @@ func ClearDashboardDeletedAt(dashboard *dash0api.DashboardDefinition) { } dashboard.Metadata.Annotations.Dash0ComdeletedAt = nil } + +// DashboardKeys lets the shared key rules resolve a dashboard's identifiers in +// the order apply upserts by: the dash0.com/origin label, then +// metadata.dash0Extensions.id, then the dash0.com/id label. The client's own +// GetID reads the dash0.com/id label first, which would put the server UUID +// ahead of the id the user chose. Read it before StripDashboardServerFields, +// which clears the dash0.com/id label. +func DashboardKeys(dashboard *dash0api.DashboardDefinition) dash0api.AssetMetadata { + id := dashboardExtensionsID(dashboard) + if id == "" { + id = dash0api.GetDashboardID(dashboard) + } + return dashboardKeys{origin: dashboard.GetOrigin(), id: id, name: dash0api.GetDashboardName(dashboard)} +} + +type dashboardKeys struct{ origin, id, name string } + +func (k dashboardKeys) GetOrigin() string { return k.origin } +func (k dashboardKeys) GetID() string { return k.id } +func (k dashboardKeys) GetName() string { return k.name } +func (dashboardKeys) UpsertKeys() (byOrigin, byID bool) { return true, true } + +// DashboardKey returns the identifier ImportDashboard upserts by, or "". +func DashboardKey(dashboard *dash0api.DashboardDefinition) string { + return PreferredKey(DashboardKeys(dashboard)) +} + +// ValidateDashboard rejects a dashboard whose dash0.com/origin label and +// metadata.dash0Extensions.id differ: the API refuses the create, and an +// update would address only one of the two. +func ValidateDashboard(dashboard *dash0api.DashboardDefinition) error { + origin, id := dashboard.GetOrigin(), dashboardExtensionsID(dashboard) + if origin != "" && id != "" && origin != id { + return fmt.Errorf( + "%s (%q) and metadata.dash0Extensions.id (%q) must be equal; set only %s", + OriginLabel, origin, id, OriginLabel, + ) + } + return nil +} + +func dashboardExtensionsID(dashboard *dash0api.DashboardDefinition) string { + if dashboard == nil || dashboard.Metadata.Dash0Extensions == nil || dashboard.Metadata.Dash0Extensions.Id == nil { + return "" + } + return *dashboard.Metadata.Dash0Extensions.Id +} diff --git a/internal/asset/document.go b/internal/asset/document.go index 62482b76..b43900db 100644 --- a/internal/asset/document.go +++ b/internal/asset/document.go @@ -84,7 +84,7 @@ func ParseDocumentHeader(data []byte) (kind, name, id string, err error) { if name == "" { name = dashboard.Metadata.Name } - id = dash0api.GetDashboardID(&dashboard) + id = DashboardKey(&dashboard) case "checkrule": var rule dash0api.PrometheusAlertRule diff --git a/internal/asset/identifier.go b/internal/asset/identifier.go index fc3c1a4d..e2344e73 100644 --- a/internal/asset/identifier.go +++ b/internal/asset/identifier.go @@ -33,9 +33,6 @@ type identifierProbe struct { // Values are not all strings: sampling rules nest custom labels // under labels.custom, so only string values are read. Labels map[string]any `json:"labels"` - Dash0Extensions struct { - ID string `json:"id"` - } `json:"dash0Extensions"` } `json:"metadata"` } @@ -44,7 +41,8 @@ type identifierProbe struct { // per the "Asset identifiers and idempotent upsert" table in the command // reference: // -// - Dashboard: metadata.dash0Extensions.id +// - Dashboard: dash0.com/origin, then metadata.dash0Extensions.id, then +// dash0.com/id // - CheckRule: top-level id // - Dash0NotificationChannel, Dash0TimeSeriesAggregation: dash0.com/origin // - Dash0Dataset: spec.slug, then dash0.com/id @@ -80,7 +78,11 @@ func ExtractIdentifier(data []byte) (string, error) { switch NormalizeKind(probe.Kind) { case "dashboard": - return probe.Metadata.Dash0Extensions.ID, nil + var dashboard dash0api.DashboardDefinition + if err := dash0yaml.Unmarshal(data, &dashboard); err != nil { + return "", fmt.Errorf("failed to extract identifier: %w", err) + } + return DashboardKey(&dashboard), nil case "checkrule": return probe.ID, nil case "notificationchannel", "timeseriesaggregation": diff --git a/internal/asset/identifier_test.go b/internal/asset/identifier_test.go index e9e61090..bd5e23bc 100644 --- a/internal/asset/identifier_test.go +++ b/internal/asset/identifier_test.go @@ -45,7 +45,10 @@ func TestExtractIdentifier_ReadsOnlyTheKindsUpsertField(t *testing.T) { {"SyntheticCheck ignores origin", "kind: SyntheticCheck\nmetadata:\n labels:\n dash0.com/origin: o\n", ""}, {"PersesDashboard ignores origin", "kind: PersesDashboard\nmetadata:\n labels:\n dash0.com/origin: o\n", ""}, {"PrometheusRule ignores origin", "kind: PrometheusRule\nmetadata:\n labels:\n dash0.com/origin: o\n", ""}, - {"Dashboard ignores the id label", "kind: Dashboard\nmetadata:\n labels:\n dash0.com/id: i\n", ""}, + {"Dashboard falls back to the id label", "kind: Dashboard\nmetadata:\n labels:\n dash0.com/id: i\n", "i"}, + {"Dashboard reads origin", "kind: Dashboard\nmetadata:\n labels:\n dash0.com/origin: o\n", "o"}, + {"Dashboard prefers dash0Extensions.id over the id label", "kind: Dashboard\nmetadata:\n labels:\n dash0.com/id: uuid\n dash0Extensions:\n id: i\n", "i"}, + {"Dashboard prefers origin over the id label", "kind: Dashboard\nmetadata:\n labels:\n dash0.com/id: uuid\n dash0.com/origin: o\n", "o"}, {"CheckRule ignores the id label", "kind: CheckRule\nmetadata:\n labels:\n dash0.com/id: i\n", ""}, {"Dash0NotificationChannel ignores id", "kind: Dash0NotificationChannel\nmetadata:\n labels:\n dash0.com/id: i\n", ""}, diff --git a/internal/asset/validate.go b/internal/asset/validate.go index c3a6c913..9e3f1ef5 100644 --- a/internal/asset/validate.go +++ b/internal/asset/validate.go @@ -30,6 +30,13 @@ func ValidateDocuments(documents []Document) (validationErrors, validationWarnin if _, err := DetectSpamFilterAPIVersion(doc.Raw); err != nil { validationErrors = append(validationErrors, fmt.Sprintf("%s: %s", doc.Location(), err.Error())) } + } else if NormalizeKind(doc.Kind) == "dashboard" { + var dashboard dash0api.DashboardDefinition + if err := dash0yaml.Unmarshal(doc.Raw, &dashboard); err == nil { + if err := ValidateDashboard(&dashboard); err != nil { + validationErrors = append(validationErrors, fmt.Sprintf("%s: %s", doc.Location(), err.Error())) + } + } } else if NormalizeKind(doc.Kind) == "prometheusrule" { // Catch CRDs that contain no usable rules at all up front, before // any API call. ParseAsPrometheusAlertRules already rejects diff --git a/internal/dashboards/create.go b/internal/dashboards/create.go index 835d9350..b1313444 100644 --- a/internal/dashboards/create.go +++ b/internal/dashboards/create.go @@ -54,6 +54,10 @@ func runCreate(ctx context.Context, flags *asset.FileInputFlags) error { return err } + if err := asset.ValidateDashboard(dashboard); err != nil { + return err + } + if flags.DryRun { fmt.Println("Dry run: dashboard definition is valid") return nil diff --git a/internal/dashboards/update.go b/internal/dashboards/update.go index 14af693f..0fc4dc03 100644 --- a/internal/dashboards/update.go +++ b/internal/dashboards/update.go @@ -58,9 +58,12 @@ func runUpdate(ctx context.Context, args []string, flags *asset.FileInputFlags) return err } + if err := asset.ValidateDashboard(dashboard); err != nil { + return err + } id, err := asset.ResolveUpdateKey(args, asset.UpdateKey{ Noun: "dashboard", - Upsert: dashboard, + Upsert: asset.DashboardKeys(dashboard), }) if err != nil { return err diff --git a/internal/dashboards/update_integration_test.go b/internal/dashboards/update_integration_test.go index d7edb48d..32577ad4 100644 --- a/internal/dashboards/update_integration_test.go +++ b/internal/dashboards/update_integration_test.go @@ -97,6 +97,43 @@ spec: assert.Contains(t, output, "no changes") } +func TestUpdateDashboard_WithOriginFromFile(t *testing.T) { + testutil.SetupTestEnv(t) + + yamlFile := filepath.Join(t.TempDir(), "dashboard.yaml") + require.NoError(t, os.WriteFile(yamlFile, []byte(`kind: Dashboard +metadata: + name: My Dashboard + labels: + dash0.com/origin: dash0-cli_my-dashboard +spec: + display: + name: My Dashboard +`), 0644)) + + server := testutil.NewMockServer(t, testutil.FixturesDir()) + for _, method := range []string{http.MethodGet, http.MethodPut} { + server.On(method, "/api/dashboards/dash0-cli_my-dashboard", testutil.MockResponse{ + StatusCode: http.StatusOK, + BodyFile: fixtureGetSuccess, + Validator: testutil.RequireHeaders, + }) + } + + cmd := NewDashboardsCmd() + cmd.SetArgs([]string{"update", "-f", yamlFile, "--api-url", server.URL, "--auth-token", testAuthToken}) + + var cmdErr error + testutil.CaptureStdout(t, func() { + cmdErr = cmd.Execute() + }) + + require.NoError(t, cmdErr) + assert.True(t, slices.ContainsFunc(server.Requests(), func(req testutil.RecordedRequest) bool { + return req.Method == http.MethodPut && req.Path == "/api/dashboards/dash0-cli_my-dashboard" + })) +} + func TestUpdateDashboard_IDMismatch(t *testing.T) { testutil.SetupTestEnv(t) @@ -144,7 +181,7 @@ spec: cmdErr := cmd.Execute() require.Error(t, cmdErr) - assert.Contains(t, cmdErr.Error(), "no dashboard id given: pass one as an argument") + assert.Contains(t, cmdErr.Error(), "no dashboard origin or id given: pass one as an argument") } func TestUpdateDashboard_PersesDashboardCRD_IDFromFile(t *testing.T) { @@ -254,7 +291,7 @@ spec: cmdErr := cmd.Execute() require.Error(t, cmdErr) - assert.Contains(t, cmdErr.Error(), "no dashboard id given: pass one as an argument") + assert.Contains(t, cmdErr.Error(), "no dashboard origin or id given: pass one as an argument") } func TestUpdateDashboard_PersesDashboardCRD_IDMismatch(t *testing.T) { diff --git a/internal/diff/upsertkey_characterization_test.go b/internal/diff/upsertkey_characterization_test.go index 45cebe01..c6e6a580 100644 --- a/internal/diff/upsertkey_characterization_test.go +++ b/internal/diff/upsertkey_characterization_test.go @@ -214,9 +214,9 @@ func TestUpsertKey_Characterization(t *testing.T) { want := map[string]map[identifierVariant]upsertKeyCharacterization{ "dashboard": { withNeither: {"", "", ""}, - withOrigin: {"", "", ""}, + withOrigin: {"o", "o", "GetDashboard(o)"}, withID: {"i", "i", "GetDashboard(i)"}, - withBoth: {"i", "i", "GetDashboard(i)"}, + withBoth: {"o", "o", `error: metadata.labels["dash0.com/origin"] ("o") and metadata.dash0Extensions.id ("i") must be equal; set only metadata.labels["dash0.com/origin"]`}, }, "checkrule": { withNeither: {"", "", ""}, diff --git a/internal/skill/content/references/dashboards.md b/internal/skill/content/references/dashboards.md index 290289d5..96ed34fe 100644 --- a/internal/skill/content/references/dashboards.md +++ b/internal/skill/content/references/dashboards.md @@ -29,9 +29,9 @@ Dashboard: apiVersion: dash0.com/v1alpha1 kind: Dashboard metadata: - name: a1b2c3d4-5678-90ab-cdef-1234567890ab - dash0Extensions: - id: a1b2c3d4-5678-90ab-cdef-1234567890ab + name: production-overview + labels: + dash0.com/origin: dash0-cli_production-overview spec: display: name: Production Overview From 32a4d8b99679412929fd4c2a1d8d4a63c71117c0 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:29:54 +0000 Subject: [PATCH 3/6] chore(nix): update vendorHash for dependency change --- nix/package.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nix/package.nix b/nix/package.nix index 019f7c9e..a1634bd2 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -50,7 +50,7 @@ buildGo127Module (finalAttrs: { # replaced with the real hash: build once, then copy the `got: sha256-...` # value from the resulting hash-mismatch error into this field. Re-run after # any change to go.mod / go.sum. See the README's "Install with Nix" section. - vendorHash = "sha256-SjzuN6h0U8ajll+lOKtNZPsvzGUpQNEkCSoIUPhkYCo="; + vendorHash = "sha256-PdDQZC0k55cq/IdsRWVMjPY2TiqAQjyTkIDaeGDD0QU="; # Only the CLI entrypoint is a `main` package; building it explicitly avoids # compiling test-only helpers into the output. From fdc1fdef97821eac970dc58dcfcfe84763126def Mon Sep 17 00:00:00 2001 From: Nicolas Takashi Date: Mon, 16 Nov 2026 17:20:09 +0000 Subject: [PATCH 4/6] feat(dashboards): update an exported dashboard that gains an origin When the dash0.com/origin of a dashboard is not found and the file also carries the dash0.com/id label, apply updates the dashboard at that id, which then gets the origin. apply --since treats the id as the same dashboard, so it does not delete it. The validation error for a mismatched origin and dash0Extensions.id now tells the user to move the id to the dash0.com/id label. Also sync the agent skill with the new dashboard keys, list the behavior changes in the changelog entry, and add tests for create, for a second apply with no changes, and for restoring a deleted dashboard by origin. Refs #380 --- .../380-dashboards-upsert-by-origin.yaml | 5 +- CLAUDE.md | 2 +- ...tekey_characterization_integration_test.go | 2 +- docs/commands.md | 2 +- internal/apply/integration_test.go | 132 +++++++++++++++++- internal/apply/since_integration_test.go | 26 ++++ internal/asset/dashboard.go | 38 ++++- internal/asset/identifier.go | 17 ++- .../dashboards/create_integration_test.go | 51 +++++++ .../diff/upsertkey_characterization_test.go | 2 +- internal/git/diff.go | 40 +++++- internal/git/snapshot.go | 17 ++- internal/skill/content/SKILL.md | 4 +- 13 files changed, 315 insertions(+), 23 deletions(-) create mode 100644 internal/dashboards/create_integration_test.go diff --git a/.chloggen/380-dashboards-upsert-by-origin.yaml b/.chloggen/380-dashboards-upsert-by-origin.yaml index 10b45fc2..97ce4af7 100644 --- a/.chloggen/380-dashboards-upsert-by-origin.yaml +++ b/.chloggen/380-dashboards-upsert-by-origin.yaml @@ -4,6 +4,9 @@ note: "Dashboards upsert by the `dash0.com/origin` label, so a dashboard kept as issues: [380] subtext: | The CLI reads the identifier from `dash0.com/origin`, then `metadata.dash0Extensions.id`, then `dash0.com/id`. - A file exported with `dashboards get -o yaml` updates the dashboard it came from. + A file exported with `dashboards get -o yaml` updates the dashboard it came from, also after you add a `dash0.com/origin` label to it. A file whose `dash0.com/origin` and `dash0Extensions.id` differ fails validation before anything is written. + The API already rejected such a file, so no file that worked before fails now. + A file with only the `dash0.com/id` label updates that dashboard instead of creating a new one. + Commands that use an OAuth profile now resolve the access token when they start, so a rejected refresh shows the `dash0 login` hint before any request, as before v1.34.0 of the API client. change_logs: [] diff --git a/CLAUDE.md b/CLAUDE.md index 8ccfcec6..324b7f50 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -54,7 +54,7 @@ Detailed guidelines are split into focused documents: - Check rules: top-level `id` field (`PrometheusAlertRule.Id`) - Views: `metadata.labels["dash0.com/id"]` (`ViewLabels.Dash0Comid`) - Synthetic checks: `metadata.labels["dash0.com/id"]` - - Dashboards: `metadata.labels["dash0.com/origin"]`, then `metadata.dash0Extensions.id`, then `metadata.labels["dash0.com/id"]` (the server UUID). `asset.DashboardKeys` sets this order, because the client's `GetID` reads `dash0.com/id` before `dash0Extensions.id`. Read the key before `StripDashboardServerFields`, which clears `dash0.com/id`. An origin that differs from `dash0Extensions.id` fails validation. + - Dashboards: `metadata.labels["dash0.com/origin"]`, then `metadata.dash0Extensions.id`, then `metadata.labels["dash0.com/id"]` (the server UUID). `asset.DashboardKeys` sets this order, because the client's `GetID` reads `dash0.com/id` before `dash0Extensions.id`. Read the key before `StripDashboardServerFields`, which clears `dash0.com/id`. An origin that differs from `dash0Extensions.id` fails validation. When the origin is not found and the `dash0.com/id` label is, the CLI updates the dashboard at the id, like teams; `asset.DashboardAlias` returns that label, and `apply --since` treats it as the same dashboard. - PersesDashboard CRD: `metadata.labels["dash0.com/id"]` - PrometheusRule CRD (alerting and recording rules): `metadata.labels["dash0.com/id"]` - Spam filters (v1alpha1 and v1alpha2): `metadata.labels["dash0.com/id"]`, but `metadata.labels["dash0.com/origin"]` takes precedence as the upsert key when both are present diff --git a/cmd/dash0/updatekey_characterization_integration_test.go b/cmd/dash0/updatekey_characterization_integration_test.go index 3ddb84fa..682a9be9 100644 --- a/cmd/dash0/updatekey_characterization_integration_test.go +++ b/cmd/dash0/updatekey_characterization_integration_test.go @@ -186,7 +186,7 @@ func TestUpdateKey_Characterization(t *testing.T) { updateWithNeither: `error: no dashboard origin or id given: pass one as an argument, or set metadata.labels["dash0.com/origin"] in the file`, updateWithOrigin: "GET /api/dashboards/o", updateWithID: "GET /api/dashboards/" + characterizationID, - updateWithBoth: `error: metadata.labels["dash0.com/origin"] ("o") and metadata.dash0Extensions.id ("` + characterizationID + `") must be equal; set only metadata.labels["dash0.com/origin"]`, + updateWithBoth: `error: metadata.labels["dash0.com/origin"] ("o") and metadata.dash0Extensions.id ("` + characterizationID + `") must be equal; set only metadata.labels["dash0.com/origin"], and move "` + characterizationID + `" to metadata.labels["dash0.com/id"] if it is the id of an existing dashboard`, }, "check-rules": { updateWithNeither: "error: no check rule id given: pass one as an argument, or set the id in the file", diff --git a/docs/commands.md b/docs/commands.md index f1c5f0e5..73ed866d 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -834,7 +834,7 @@ The identifier field location varies by asset kind: | Kind | Identifier field | Notes | |------|------------------|-------| -| `Dashboard` | `metadata.labels["dash0.com/origin"]`, then `metadata.dash0Extensions.id`, then `metadata.labels["dash0.com/id"]` | `dash0.com/origin` is the recommended key, and a Kubernetes custom resource keeps it, while Kubernetes drops the unknown `dash0Extensions` field. When both `dash0.com/origin` and `dash0Extensions.id` are set, they must be equal, or the document fails validation before anything is written. `dash0.com/id` is the server UUID that `get -o yaml` exports, so an exported file updates the dashboard it came from | +| `Dashboard` | `metadata.labels["dash0.com/origin"]`, then `metadata.dash0Extensions.id`, then `metadata.labels["dash0.com/id"]` | `dash0.com/origin` is the recommended key, and a Kubernetes custom resource keeps it, while Kubernetes drops the unknown `dash0Extensions` field. When both `dash0.com/origin` and `dash0Extensions.id` are set, they must be equal, or the document fails validation before anything is written. `dash0.com/id` is the server UUID that `get -o yaml` exports, so an exported file updates the dashboard it came from. When the document carries both `dash0.com/origin` and `dash0.com/id` and the origin is not found, the CLI updates the dashboard at the id, which then gets the origin, and `apply --since` does not delete it | | `PersesDashboard` | `metadata.labels["dash0.com/id"]` | | | `CheckRule` | top-level `id` | | | `PrometheusRule` (alerting rules) | `metadata.labels["dash0.com/id"]` | For a single-alert CRD, this label is the alert's check-rule id directly. For a CRD with two or more alerting rules, each alert gets its own derived id (the CRD's label plus a slug of the alert's own composed name) instead of sharing the CRD's label directly — see [Multi-alert PrometheusRule CRDs](#multi-alert-prometheusrule-crds) | diff --git a/internal/apply/integration_test.go b/internal/apply/integration_test.go index d479e1df..875381fe 100644 --- a/internal/apply/integration_test.go +++ b/internal/apply/integration_test.go @@ -1383,6 +1383,48 @@ spec: assert.Equal(t, 2, puts) } +// The second apply of an origin-only dashboard finds it at the origin and +// reports no changes, instead of creating it again. +func TestApply_Dashboard_OriginOnly_SecondApplyReportsNoChanges(t *testing.T) { + testutil.SetupTestEnv(t) + + const originPath = "/api/dashboards/dash0-cli_checkout" + yamlFile := filepath.Join(t.TempDir(), "dashboard.yaml") + require.NoError(t, os.WriteFile(yamlFile, []byte(`kind: Dashboard +metadata: + name: test-dashboard + labels: + dash0.com/origin: dash0-cli_checkout +spec: + display: + name: Test Dashboard + description: Test dashboard for apply command + layouts: + - kind: Grid + spec: + items: [] + panels: {} +`), 0644)) + + server := testutil.NewMockServer(t, testutil.FixturesDir()) + server.On(http.MethodGet, originPath, testutil.MockResponse{ + StatusCode: http.StatusOK, + BodyFile: testutil.FixtureDashboardsImportSuccess, + Validator: testutil.RequireHeaders, + }) + server.WithDashboardsUpdate(testutil.FixtureDashboardsImportSuccess) + + cmd := NewApplyCmd() + cmd.SetArgs([]string{"-f", yamlFile, "--api-url", server.URL, "--auth-token", testAuthToken}) + var cmdErr error + output := testutil.CaptureStdout(t, func() { cmdErr = cmd.Execute() }) + + require.NoError(t, cmdErr) + assert.Contains(t, output, "no changes") + assert.NotContains(t, output, "created") + assert.Equal(t, []string{"GET " + originPath, "PUT " + originPath}, methodsAndPaths(server.Requests())) +} + func TestApply_Dashboard_ServerIDLabelOnly_UpdatesThatDashboard(t *testing.T) { testutil.SetupTestEnv(t) @@ -1442,10 +1484,81 @@ spec: testutil.CaptureStdout(t, func() { cmdErr = cmd.Execute() }) require.Error(t, cmdErr) - assert.Contains(t, cmdErr.Error(), `metadata.labels["dash0.com/origin"] ("dash0-cli_checkout") and metadata.dash0Extensions.id ("other-id") must be equal`) + assert.Contains(t, cmdErr.Error(), `metadata.labels["dash0.com/origin"] ("dash0-cli_checkout") and metadata.dash0Extensions.id ("other-id") must be equal; set only metadata.labels["dash0.com/origin"], and move "other-id" to metadata.labels["dash0.com/id"] if it is the id of an existing dashboard`) assert.Empty(t, server.Requests()) } +const ( + exportedDashboardID = "0b7e5d2a-1c3f-4e8a-9b6d-2f4a8c1e7d90" + exportedDashboardOrigin = "dash0-cli_checkout" +) + +// applyExportedDashboardWithNewOrigin applies an exported dashboard that has +// gained an origin. The origin is unknown; storedStatus is the answer for the +// exported id. +func applyExportedDashboardWithNewOrigin(t *testing.T, storedStatus int) []testutil.RecordedRequest { + t.Helper() + testutil.SetupTestEnv(t) + + yamlFile := filepath.Join(t.TempDir(), "dashboard.yaml") + require.NoError(t, os.WriteFile(yamlFile, []byte(`kind: Dashboard +metadata: + name: checkout + labels: + dash0.com/id: `+exportedDashboardID+` + dash0.com/origin: `+exportedDashboardOrigin+` +spec: + display: + name: Checkout +`), 0644)) + + server := testutil.NewMockServer(t, testutil.FixturesDir()) + server.On(http.MethodGet, "/api/dashboards/"+exportedDashboardOrigin, testutil.MockResponse{ + StatusCode: http.StatusNotFound, + BodyFile: testutil.FixtureDashboardsNotFound, + Validator: testutil.RequireHeaders, + }) + stored := testutil.MockResponse{StatusCode: storedStatus, BodyFile: testutil.FixtureDashboardsImportSuccess, Validator: testutil.RequireHeaders} + if storedStatus == http.StatusNotFound { + stored.BodyFile = testutil.FixtureDashboardsNotFound + } + server.On(http.MethodGet, "/api/dashboards/"+exportedDashboardID, stored) + server.WithDashboardsUpdate(testutil.FixtureDashboardsImportSuccess) + + cmd := NewApplyCmd() + cmd.SetArgs([]string{"-f", yamlFile, "--api-url", server.URL, "--auth-token", testAuthToken}) + var cmdErr error + testutil.CaptureStdout(t, func() { cmdErr = cmd.Execute() }) + require.NoError(t, cmdErr) + return server.Requests() +} + +// Adding an origin to an exported dashboard must update that dashboard, not +// create a second one at the origin. +func TestApply_Dashboard_OriginNotFoundUpdatesAtID(t *testing.T) { + requests := applyExportedDashboardWithNewOrigin(t, http.StatusOK) + + assert.Equal(t, []string{ + "GET /api/dashboards/" + exportedDashboardOrigin, + "GET /api/dashboards/" + exportedDashboardID, + "PUT /api/dashboards/" + exportedDashboardID, + }, methodsAndPaths(requests)) + + var body dash0api.DashboardDefinition + require.NoError(t, json.Unmarshal(requests[2].Body, &body)) + assert.Equal(t, exportedDashboardOrigin, body.GetOrigin(), "the existing dashboard gets the origin") +} + +func TestApply_Dashboard_OriginAndIDNotFoundCreatesAtOrigin(t *testing.T) { + requests := applyExportedDashboardWithNewOrigin(t, http.StatusNotFound) + + assert.Equal(t, []string{ + "GET /api/dashboards/" + exportedDashboardOrigin, + "GET /api/dashboards/" + exportedDashboardID, + "PUT /api/dashboards/" + exportedDashboardOrigin, + }, methodsAndPaths(requests)) +} + func TestApply_Dashboard_RoundTrip(t *testing.T) { testutil.SetupTestEnv(t) @@ -2863,6 +2976,23 @@ spec: want: `Dashboard "Test Dashboard" (test-dashboard-id) restored`, wantErr: `Dashboard "Test Dashboard" was saved, but the API still shows it as deleted`, }, + { + name: "dashboard by origin", + yaml: `kind: Dashboard +metadata: + name: test-dashboard + labels: + dash0.com/origin: dash0-cli_checkout +spec: + display: + name: Test Dashboard +`, + path: apiPathDashboards + "/dash0-cli_checkout", + deletedFixture: testutil.FixtureDashboardsGetDeleted, + putFixture: testutil.FixtureDashboardsImportSuccess, + want: `Dashboard "Test Dashboard" (test-dashboard-id) restored`, + wantErr: `Dashboard "Test Dashboard" was saved, but the API still shows it as deleted`, + }, { name: "view", yaml: `kind: Dash0View diff --git a/internal/apply/since_integration_test.go b/internal/apply/since_integration_test.go index ab109f2b..16c38821 100644 --- a/internal/apply/since_integration_test.go +++ b/internal/apply/since_integration_test.go @@ -658,11 +658,37 @@ metadata: spec: display: name: Shared Dashboard +` + // An exported dashboard carries its server id in dash0.com/id. Adding an + // origin changes its key, but apply updates it at that id, so it is the + // same dashboard. + const idYAML = `apiVersion: dash0.com/v1alpha1 +kind: Dashboard +metadata: + name: shared-dashboard + labels: + dash0.com/id: shared-id +spec: + display: + name: Shared Dashboard +` + const originAndIDYAML = `apiVersion: dash0.com/v1alpha1 +kind: Dashboard +metadata: + name: shared-dashboard + labels: + dash0.com/id: shared-id + dash0.com/origin: dash0-cli_shared-dashboard +spec: + display: + name: Shared Dashboard ` for _, tt := range []struct{ name, before, after string }{ {"PersesDashboard to Dashboard", persesYAML, plainYAML}, {"Dashboard to PersesDashboard", plainYAML, persesYAML}, {"dash0Extensions.id to dash0.com/origin", plainYAML, originYAML}, + {"dash0.com/id gains dash0.com/origin", idYAML, originAndIDYAML}, + {"PersesDashboard dash0.com/id gains a Dashboard origin", persesYAML, originAndIDYAML}, } { t.Run(tt.name, func(t *testing.T) { testutil.SetupTestEnv(t) diff --git a/internal/asset/dashboard.go b/internal/asset/dashboard.go index 975293e7..4d0563b4 100644 --- a/internal/asset/dashboard.go +++ b/internal/asset/dashboard.go @@ -20,7 +20,7 @@ func ImportDashboard(ctx context.Context, apiClient dash0api.Client, dashboard * return ImportResult{}, err } // A failed preflight does not block the write: the PUT reports the same error. - plan, _ := PlanDashboard(ctx, apiClient, dashboard, dataset) + plan, _ := planDashboard(ctx, apiClient, dashboard, dataset) id := plan.Key var ( @@ -58,18 +58,31 @@ func PlanDashboard(ctx context.Context, apiClient dash0api.Client, dashboard *da if err := ValidateDashboard(dashboard); err != nil { return ImportPlan{}, err } - // Read the key first: the strip clears the dash0.com/id label. + return planDashboard(ctx, apiClient, dashboard, dataset) +} + +func planDashboard(ctx context.Context, apiClient dash0api.Client, dashboard *dash0api.DashboardDefinition, dataset *string) (ImportPlan, error) { + // Read the keys first: the strip clears the dash0.com/id label. key := DashboardKey(dashboard) + alias := DashboardAlias(dashboard) dash0api.StripDashboardServerFields(dashboard) plan := ImportPlan{Key: key, Action: ActionCreated, Proposed: dashboard} if key == "" { return plan, nil } - existing, err := apiClient.GetDashboard(ctx, plan.Key, dataset) + lookupKey := key + existing, err := apiClient.GetDashboard(ctx, lookupKey, dataset) + // An exported dashboard that gains an origin still names its server id in + // dash0.com/id, so updating at that id gives the existing dashboard the + // origin instead of creating a second one. + if dash0api.IsNotFound(err) && alias != "" { + lookupKey = alias + existing, err = apiClient.GetDashboard(ctx, lookupKey, dataset) + } if err != nil { return plan, ignoreNotFound(err) } - plan.Action, plan.Before = actionForExisting(DashboardDeletedAt(existing)), existing + plan.Key, plan.Action, plan.Before = lookupKey, actionForExisting(DashboardDeletedAt(existing)), existing return plan, nil } @@ -118,6 +131,19 @@ func DashboardKey(dashboard *dash0api.DashboardDefinition) string { return PreferredKey(DashboardKeys(dashboard)) } +// DashboardAlias returns the dash0.com/id label when the dashboard upserts by +// another key, or "". It names the same dashboard: ImportDashboard falls back +// to it, and apply --since does not delete it. +func DashboardAlias(dashboard *dash0api.DashboardDefinition) string { + if dashboard == nil || dashboard.Metadata.Labels == nil || dashboard.Metadata.Labels.Dash0Comid == nil { + return "" + } + if label := *dashboard.Metadata.Labels.Dash0Comid; label != DashboardKey(dashboard) { + return label + } + return "" +} + // ValidateDashboard rejects a dashboard whose dash0.com/origin label and // metadata.dash0Extensions.id differ: the API refuses the create, and an // update would address only one of the two. @@ -125,8 +151,8 @@ func ValidateDashboard(dashboard *dash0api.DashboardDefinition) error { origin, id := dashboard.GetOrigin(), dashboardExtensionsID(dashboard) if origin != "" && id != "" && origin != id { return fmt.Errorf( - "%s (%q) and metadata.dash0Extensions.id (%q) must be equal; set only %s", - OriginLabel, origin, id, OriginLabel, + "%s (%q) and metadata.dash0Extensions.id (%q) must be equal; set only %s, and move %q to metadata.labels[\"dash0.com/id\"] if it is the id of an existing dashboard", + OriginLabel, origin, id, OriginLabel, id, ) } return nil diff --git a/internal/asset/identifier.go b/internal/asset/identifier.go index e2344e73..8af62067 100644 --- a/internal/asset/identifier.go +++ b/internal/asset/identifier.go @@ -32,7 +32,7 @@ type identifierProbe struct { Metadata struct { // Values are not all strings: sampling rules nest custom labels // under labels.custom, so only string values are read. - Labels map[string]any `json:"labels"` + Labels map[string]any `json:"labels"` } `json:"metadata"` } @@ -105,6 +105,21 @@ func ExtractIdentifier(data []byte) (string, error) { } } +// ExtractAlias returns a second identifier that names the same asset as the +// one ExtractIdentifier returns, or "". Only dashboards have one: see +// DashboardAlias. +func ExtractAlias(data []byte) (string, error) { + kind, err := dash0yaml.DetectKind(data) + if err != nil || NormalizeKind(kind) != "dashboard" { + return "", err + } + var dashboard dash0api.DashboardDefinition + if err := dash0yaml.Unmarshal(data, &dashboard); err != nil { + return "", fmt.Errorf("failed to extract alias: %w", err) + } + return DashboardAlias(&dashboard), nil +} + // label returns the string value of a metadata label, or "" when it is absent // or not a string. func (p identifierProbe) label(key string) string { diff --git a/internal/dashboards/create_integration_test.go b/internal/dashboards/create_integration_test.go new file mode 100644 index 00000000..79f33f0f --- /dev/null +++ b/internal/dashboards/create_integration_test.go @@ -0,0 +1,51 @@ +//go:build integration + +package dashboards + +import ( + "os" + "path/filepath" + "testing" + + "github.com/dash0hq/dash0-cli/internal/testutil" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCreateDashboard_OriginAndExtensionsIDDiffer_FailsBeforeAnyRequest(t *testing.T) { + for _, tt := range []struct { + name string + extra []string + }{ + {"create", nil}, + {"create --dry-run", []string{"--dry-run"}}, + } { + t.Run(tt.name, func(t *testing.T) { + testutil.SetupTestEnv(t) + + yamlFile := filepath.Join(t.TempDir(), "dashboard.yaml") + require.NoError(t, os.WriteFile(yamlFile, []byte(`kind: Dashboard +metadata: + name: checkout + labels: + dash0.com/origin: dash0-cli_checkout + dash0Extensions: + id: other-id +spec: + display: + name: Checkout +`), 0644)) + + server := testutil.NewMockServer(t, testutil.FixturesDir()) + cmd := NewDashboardsCmd() + cmd.SetArgs(append([]string{"create", "-f", yamlFile, "--api-url", server.URL, "--auth-token", testAuthToken}, tt.extra...)) + + var err error + testutil.CaptureStdout(t, func() { err = cmd.Execute() }) + + require.Error(t, err) + assert.Contains(t, err.Error(), `metadata.labels["dash0.com/origin"] ("dash0-cli_checkout") and metadata.dash0Extensions.id ("other-id") must be equal; set only metadata.labels["dash0.com/origin"], and move "other-id" to metadata.labels["dash0.com/id"] if it is the id of an existing dashboard`) + assert.Empty(t, server.Requests()) + }) + } +} diff --git a/internal/diff/upsertkey_characterization_test.go b/internal/diff/upsertkey_characterization_test.go index c6e6a580..c0733054 100644 --- a/internal/diff/upsertkey_characterization_test.go +++ b/internal/diff/upsertkey_characterization_test.go @@ -216,7 +216,7 @@ func TestUpsertKey_Characterization(t *testing.T) { withNeither: {"", "", ""}, withOrigin: {"o", "o", "GetDashboard(o)"}, withID: {"i", "i", "GetDashboard(i)"}, - withBoth: {"o", "o", `error: metadata.labels["dash0.com/origin"] ("o") and metadata.dash0Extensions.id ("i") must be equal; set only metadata.labels["dash0.com/origin"]`}, + withBoth: {"o", "o", `error: metadata.labels["dash0.com/origin"] ("o") and metadata.dash0Extensions.id ("i") must be equal; set only metadata.labels["dash0.com/origin"], and move "i" to metadata.labels["dash0.com/id"] if it is the id of an existing dashboard`}, }, "checkrule": { withNeither: {"", "", ""}, diff --git a/internal/git/diff.go b/internal/git/diff.go index 9932463e..93552b90 100644 --- a/internal/git/diff.go +++ b/internal/git/diff.go @@ -94,7 +94,7 @@ func Diff(before, after Snapshot) DeletionPlan { var plan DeletionPlan for key, path := range before.Identifiers { - if survives(after, key) { + if survives(before, after, key) { continue } deletion := Deletion{ @@ -227,19 +227,45 @@ func Diff(before, after Snapshot) DeletionPlan { return plan } -// survives reports whether the asset behind key is still declared in after. +// survives reports whether the asset behind key is still declared in after, +// under its identifier or its alias, in either snapshot. A dashboard file +// that gains an origin keeps its server id as the alias, and apply updates the +// same dashboard, so the old id is not a deletion. +// // apply upserts PersesDashboard and Dashboard documents through the same // dashboard import (see applyDocument in internal/apply), so a file // re-authored from one kind spelling to the other under the same identifier // is still the same live dashboard, not a deletion. The key keeps its own // kind, so a real deletion is still reported under the spelling it had. -func survives(after Snapshot, key IdentifierKey) bool { - if _, ok := after.Identifiers[key]; ok { +func survives(before, after Snapshot, key IdentifierKey) bool { + names := []string{key.Identifier} + if alias := before.Aliases[key]; alias != "" { + names = append(names, alias) + } + kinds := []string{key.Kind} + if equivalent := equivalentDashboardKind(key.Kind); equivalent != "" { + kinds = append(kinds, equivalent) + } + for _, kind := range kinds { + for _, name := range names { + if declares(after, IdentifierKey{Kind: kind, Identifier: name}) { + return true + } + } + } + return false +} + +// declares reports whether snapshot names key as an identifier or an alias. +// ponytail: linear scan of aliases per lookup, fine for a repo's asset count. +func declares(snapshot Snapshot, key IdentifierKey) bool { + if _, ok := snapshot.Identifiers[key]; ok { return true } - if alias := equivalentDashboardKind(key.Kind); alias != "" { - _, ok := after.Identifiers[IdentifierKey{Kind: alias, Identifier: key.Identifier}] - return ok + for aliasOf, alias := range snapshot.Aliases { + if aliasOf.Kind == key.Kind && alias == key.Identifier { + return true + } } return false } diff --git a/internal/git/snapshot.go b/internal/git/snapshot.go index de5c1c7a..3cc9888e 100644 --- a/internal/git/snapshot.go +++ b/internal/git/snapshot.go @@ -109,6 +109,11 @@ type Snapshot struct { // resolveDeletionNames in internal/apply/since.go) from memory instead // of shelling out to `git cat-file` a second time for the same blob. RawContent map[string][]byte + + // Aliases maps an identifier to a second identifier of the same asset + // (see asset.ExtractAlias), so an asset still declared under either one + // is not a deletion. + Aliases map[IdentifierKey]string } func newSnapshot() Snapshot { @@ -125,6 +130,7 @@ func newSnapshot() Snapshot { TeamUsesOriginByIdentifier: map[string]bool{}, Paths: map[string]bool{}, RawContent: map[string][]byte{}, + Aliases: map[IdentifierKey]string{}, } } @@ -299,7 +305,16 @@ func ingestDocuments(snap *Snapshot, path string, data []byte) error { snap.NoIdentifier[docPath] = NoIdentifierDoc{Kind: normalizedKind, FilePath: path} continue } - snap.Identifiers[IdentifierKey{Kind: normalizedKind, Identifier: identifier}] = docPath + key := IdentifierKey{Kind: normalizedKind, Identifier: identifier} + snap.Identifiers[key] = docPath + + alias, err := asset.ExtractAlias(docBytes) + if err != nil { + return fmt.Errorf("failed to extract alias: %w", err) + } + if alias != "" { + snap.Aliases[key] = alias + } if normalizedKind == "prometheusrule" { // asset.ExtractPrometheusAlertNames, not diff --git a/internal/skill/content/SKILL.md b/internal/skill/content/SKILL.md index 9a4a52df..26263e11 100644 --- a/internal/skill/content/SKILL.md +++ b/internal/skill/content/SKILL.md @@ -50,7 +50,7 @@ Every asset type accepts a user-defined identifier in its YAML/JSON document. Wh | Kind | Identifier field | |------|------------------| -| `Dashboard` | `metadata.dash0Extensions.id` | +| `Dashboard` | `metadata.labels["dash0.com/origin"]`, then `metadata.dash0Extensions.id`, then `metadata.labels["dash0.com/id"]` | | `PersesDashboard` | `metadata.labels["dash0.com/id"]` | | `CheckRule` | top-level `id` | | `PrometheusRule` (alerting or recording) | `metadata.labels["dash0.com/id"]` (a CRD with 2+ alerting rules derives a distinct id per alert instead: `