Skip to content

Commit bca8e60

Browse files
Merge pull request #4817 from Azure/gather-observability-query-embedding
gather-observability: embed query config into binary
2 parents bec4074 + 439cca6 commit bca8e60

File tree

4 files changed

+35
-41
lines changed

4 files changed

+35
-41
lines changed

test/E2ELocal.mk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ e2e-local/gather-observability: $(ARO_HCP_TESTS) $(TEMPLATIZE)
4747
--subscription-id "$$(az account show --query id -o tsv)" \
4848
--output $(OBSERVABILITY_OUTPUT) \
4949
--severity-threshold Sev3 \
50-
--start-time-fallback "$$(date -u -v-1H +%Y-%m-%dT%H:%M:%SZ)" \
51-
--queries-config $(DIR)/cmd/aro-hcp-tests/gather-observability/queries.yaml
50+
--start-time-fallback "$$(date -u -v-1H +%Y-%m-%dT%H:%M:%SZ)"
5251
@echo "Observability artifacts written to $(OBSERVABILITY_OUTPUT)"
5352
.PHONY: e2e-local/gather-observability
5453

test/cmd/aro-hcp-tests/gather-observability/chart_test.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ package gatherobservability
1717
import (
1818
"encoding/json"
1919
"math"
20-
"os"
21-
"path/filepath"
2220
"strings"
2321
"testing"
2422
)
@@ -497,13 +495,7 @@ func TestLoadQueriesConfig(t *testing.T) {
497495
for _, tt := range tests {
498496
t.Run(tt.name, func(t *testing.T) {
499497
t.Parallel()
500-
tmpDir := t.TempDir()
501-
cfgPath := filepath.Join(tmpDir, "queries.yaml")
502-
if err := os.WriteFile(cfgPath, []byte(tt.yaml), 0644); err != nil {
503-
t.Fatalf("failed to write temp config: %v", err)
504-
}
505-
506-
cfg, err := loadQueriesConfig(cfgPath)
498+
cfg, err := parseQueriesConfig([]byte(tt.yaml))
507499
if tt.wantErr != "" {
508500
if err == nil {
509501
t.Fatalf("expected error containing %q, got nil", tt.wantErr)
@@ -523,10 +515,13 @@ func TestLoadQueriesConfig(t *testing.T) {
523515
}
524516
}
525517

526-
func TestLoadQueriesConfigFileNotFound(t *testing.T) {
518+
func TestLoadQueriesConfigEmbedded(t *testing.T) {
527519
t.Parallel()
528-
_, err := loadQueriesConfig("/nonexistent/path/queries.yaml")
529-
if err == nil {
530-
t.Fatal("expected error for missing file, got nil")
520+
cfg, err := loadQueriesConfig()
521+
if err != nil {
522+
t.Fatalf("embedded queries.yaml should parse without error: %v", err)
523+
}
524+
if len(cfg.Queries) == 0 {
525+
t.Fatal("embedded queries.yaml should contain at least one query")
531526
}
532527
}

test/cmd/aro-hcp-tests/gather-observability/options.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ func BindOptions(opts *RawOptions, cmd *cobra.Command) error {
6262
cmd.Flags().StringVar(&opts.RenderedConfig, "rendered-config", opts.RenderedConfig, "Path to the rendered configuration YAML file.")
6363
cmd.Flags().StringVar(&opts.SubscriptionID, "subscription-id", opts.SubscriptionID, "Azure subscription ID.")
6464
cmd.Flags().StringVar(&opts.StartTimeFallback, "start-time-fallback", opts.StartTimeFallback, "Optional RFC3339 time to use as start time fallback when steps and test timing are unavailable.")
65-
cmd.Flags().StringVar(&opts.QueriesConfigPath, "queries-config", opts.QueriesConfigPath, "Path to YAML file specifying PromQL queries to execute and chart.")
6665
cmd.Flags().StringVar(&opts.SeverityThreshold, "severity-threshold", opts.SeverityThreshold, "Include alerts at this severity level or more critical (Sev0=critical .. Sev4=verbose). E.g. Sev2 includes Sev0, Sev1, Sev2. If not set, all severities are shown.")
6766
return nil
6867
}
@@ -73,7 +72,6 @@ type RawOptions struct {
7372
RenderedConfig string
7473
SubscriptionID string
7574
StartTimeFallback string
76-
QueriesConfigPath string
7775
SeverityThreshold string
7876
}
7977

@@ -199,27 +197,25 @@ func (o *ValidatedOptions) Complete(ctx context.Context) (*Options, error) {
199197
cred: cred,
200198
}
201199

202-
if o.QueriesConfigPath != "" {
203-
queries, err := loadQueriesConfig(o.QueriesConfigPath)
204-
if err != nil {
205-
return nil, fmt.Errorf("failed to load queries config: %w", err)
206-
}
207-
completed.Queries = queries
208-
logger.Info("loaded queries config", "path", o.QueriesConfigPath, "queries", len(queries.Queries))
200+
queries, err := loadQueriesConfig()
201+
if err != nil {
202+
return nil, fmt.Errorf("failed to load queries config: %w", err)
203+
}
204+
completed.Queries = queries
205+
logger.Info("loaded embedded queries config", "queries", len(queries.Queries))
209206

210-
// Resolve Prometheus endpoints eagerly so we fail fast
211-
completed.SvcPromEndpoint, err = lookupPrometheusEndpoint(ctx, cred, o.SubscriptionID, regionRG, svcWorkspace)
212-
if err != nil {
213-
return nil, fmt.Errorf("failed to look up svc Prometheus endpoint for workspace %s in %s: %w", svcWorkspace, regionRG, err)
214-
}
215-
logger.Info("resolved svc Prometheus endpoint", "endpoint", completed.SvcPromEndpoint)
207+
// Resolve Prometheus endpoints eagerly so we fail fast
208+
completed.SvcPromEndpoint, err = lookupPrometheusEndpoint(ctx, cred, o.SubscriptionID, regionRG, svcWorkspace)
209+
if err != nil {
210+
return nil, fmt.Errorf("failed to look up svc Prometheus endpoint for workspace %s in %s: %w", svcWorkspace, regionRG, err)
211+
}
212+
logger.Info("resolved svc Prometheus endpoint", "endpoint", completed.SvcPromEndpoint)
216213

217-
completed.HcpPromEndpoint, err = lookupPrometheusEndpoint(ctx, cred, o.SubscriptionID, regionRG, hcpWorkspace)
218-
if err != nil {
219-
return nil, fmt.Errorf("failed to look up hcp Prometheus endpoint for workspace %s in %s: %w", hcpWorkspace, regionRG, err)
220-
}
221-
logger.Info("resolved hcp Prometheus endpoint", "endpoint", completed.HcpPromEndpoint)
214+
completed.HcpPromEndpoint, err = lookupPrometheusEndpoint(ctx, cred, o.SubscriptionID, regionRG, hcpWorkspace)
215+
if err != nil {
216+
return nil, fmt.Errorf("failed to look up hcp Prometheus endpoint for workspace %s in %s: %w", hcpWorkspace, regionRG, err)
222217
}
218+
logger.Info("resolved hcp Prometheus endpoint", "endpoint", completed.HcpPromEndpoint)
223219

224220
return &Options{completedOptions: completed}, nil
225221
}

test/cmd/aro-hcp-tests/gather-observability/promql.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import (
2222
"io"
2323
"net/http"
2424
"net/url"
25-
"os"
2625
"strconv"
2726
"strings"
2827
"time"
2928

29+
_ "embed"
30+
3031
"sigs.k8s.io/yaml"
3132

3233
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
@@ -68,11 +69,14 @@ type PrometheusResult struct {
6869
Values [][]any `json:"values"` // each element is [unix_timestamp_float, "string_value"]
6970
}
7071

71-
func loadQueriesConfig(path string) (*QueriesConfig, error) {
72-
data, err := os.ReadFile(path)
73-
if err != nil {
74-
return nil, fmt.Errorf("failed to read queries config %s: %w", path, err)
75-
}
72+
//go:embed queries.yaml
73+
var defaultQueriesYAML []byte
74+
75+
func loadQueriesConfig() (*QueriesConfig, error) {
76+
return parseQueriesConfig(defaultQueriesYAML)
77+
}
78+
79+
func parseQueriesConfig(data []byte) (*QueriesConfig, error) {
7680
var cfg QueriesConfig
7781
if err := yaml.Unmarshal(data, &cfg); err != nil {
7882
return nil, fmt.Errorf("failed to parse queries config: %w", err)

0 commit comments

Comments
 (0)