-
Notifications
You must be signed in to change notification settings - Fork 107
Fix project ID mismatch in API analytics publish flow #3353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ package models | |
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/wso2/api-platform/common/chainkey" | ||
|
|
@@ -47,13 +48,23 @@ type RuntimeDeployConfig struct { | |
|
|
||
| // Metadata contains identity information for the deployed API. | ||
| type Metadata struct { | ||
| UUID string | ||
| Kind string | ||
| Handle string | ||
| Version string | ||
| DisplayName string | ||
| ProjectID string | ||
| LLM *LLMMetadata // nil for non-LLM kinds | ||
| UUID string | ||
| Kind string | ||
| Handle string | ||
| Version string | ||
| DisplayName string | ||
| ProjectID string // from gateway.api-platform.wso2.com/project-id (UUID for CP deploys) | ||
| ProjectHandle string // from gateway.api-platform.wso2.com/project-handle (analytics-facing) | ||
| LLM *LLMMetadata // nil for non-LLM kinds | ||
| } | ||
|
|
||
| // AnalyticsProjectRef returns the project identity to publish for analytics | ||
| // (Moesif metadata.projectId). Prefer the user-facing handle when present. | ||
| func (m Metadata) AnalyticsProjectRef() string { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The requirement is to send both to Moesif. AnalyticsProjectRef() currently replaces the UUID with the handle, which changes the meaning of the existing projectId attribute and could break existing dashboards/alerts and other consumers of that value. Can we keep the existing projectId as the UUID and add the handle as a separate attribute instead? That would satisfy the requirement without changing existing semantics. |
||
| if handle := strings.TrimSpace(m.ProjectHandle); handle != "" { | ||
| return handle | ||
| } | ||
| return m.ProjectID | ||
| } | ||
|
|
||
| // LLMMetadata carries LLM-specific metadata for provider/proxy scenarios. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package models | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestMetadataAnalyticsProjectRef(t *testing.T) { | ||
| t.Run("prefers project handle for analytics", func(t *testing.T) { | ||
| md := Metadata{ | ||
| ProjectID: "019feb20-bd8f-74f1-9489-8814a129cd80", | ||
| ProjectHandle: "new-project", | ||
| } | ||
| if got := md.AnalyticsProjectRef(); got != "new-project" { | ||
| t.Fatalf("AnalyticsProjectRef() = %q, want handle", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("falls back to project id when handle unset", func(t *testing.T) { | ||
| md := Metadata{ProjectID: "019feb20-bd8f-74f1-9489-8814a129cd80"} | ||
| if got := md.AnalyticsProjectRef(); got != md.ProjectID { | ||
| t.Fatalf("AnalyticsProjectRef() = %q, want project id", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("trims padded handle", func(t *testing.T) { | ||
| md := Metadata{ | ||
| ProjectID: "019feb20-bd8f-74f1-9489-8814a129cd80", | ||
| ProjectHandle: " new-project ", | ||
| } | ||
| if got := md.AnalyticsProjectRef(); got != "new-project" { | ||
| t.Fatalf("AnalyticsProjectRef() = %q, want trimmed handle", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("whitespace-only handle falls back to project id", func(t *testing.T) { | ||
| md := Metadata{ | ||
| ProjectID: "019feb20-bd8f-74f1-9489-8814a129cd80", | ||
| ProjectHandle: " \t ", | ||
| } | ||
| if got := md.AnalyticsProjectRef(); got != md.ProjectID { | ||
| t.Fatalf("AnalyticsProjectRef() = %q, want project id fallback", got) | ||
| } | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1051,7 +1051,7 @@ func (t *Translator) translateAPIConfig(cfg *models.StoredConfig, allConfigs []* | |
| templateHandle := t.extractTemplateHandle(cfg, allConfigs) | ||
| providerName := t.extractProviderName(cfg, allConfigs) | ||
|
|
||
| apiProjectID := extractProjectIDFromConfig(cfg) | ||
| apiProjectID := analyticsProjectRefFromConfig(cfg) | ||
|
|
||
| // Build a map of upstream definition name -> basePath for dynamic routing | ||
| // This allows the policy engine to apply the correct path transformation when UpstreamName is used | ||
|
|
@@ -1508,6 +1508,24 @@ func extractProjectIDFromConfig(cfg *models.StoredConfig) string { | |
| return "" | ||
| } | ||
|
|
||
| // extractProjectHandleFromConfig reads the analytics-facing project handle annotation. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is a no-op here: the value is only passed to the REST route builder, which doesn’t use the project-id parameter. The actual Envoy metadata is stamped by the per-topic route builder, which still uses the original UUID-only extraction. WebSub would keep publishing the UUID while REST publishes the handle, making the same Moesif attribute mean different things by API type. follow-up: MCP/LLM APIs currently get no project attribute, and MCP would drop annotations during conversion anyway. We should confirm whether those API types are in scope. |
||
| func extractProjectHandleFromConfig(cfg *models.StoredConfig) string { | ||
| if annotations := cfg.GetAnnotations(); annotations != nil { | ||
| if handle, exists := (*annotations)[commonconstants.AnnotationProjectHandle]; exists { | ||
| return strings.TrimSpace(handle) | ||
| } | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| // analyticsProjectRefFromConfig prefers project-handle for Moesif / analytics metadata. | ||
| func analyticsProjectRefFromConfig(cfg *models.StoredConfig) string { | ||
| if handle := extractProjectHandleFromConfig(cfg); handle != "" { | ||
| return handle | ||
| } | ||
| return extractProjectIDFromConfig(cfg) | ||
| } | ||
|
|
||
| func (t *Translator) extractTemplateHandle(cfg *models.StoredConfig, allConfigs []*models.StoredConfig) string { | ||
| if cfg.SourceConfiguration == nil { | ||
| return "" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ResolveImportProject still reads the dual-typed project-id annotation and passes it to a handle lookup, so control-plane artifacts with UUIDs would fail if they reach this path.
Since we now have a dedicated handle annotation, can the importer prefer the new key and fall back to project-id for older artifacts? This would also let us eventually make project-id consistently UUID-based and remove the current ambiguity.