From bf331d91d49586205a47eeb26e95575f64beee15 Mon Sep 17 00:00:00 2001 From: nick-saxelby-cko <88375324+nick-saxelby-cko@users.noreply.github.com> Date: Mon, 16 Dec 2024 15:08:31 +0000 Subject: [PATCH] Fix: Change telemetry to snake case (#141) --- common/request_metrics.go | 6 +++--- test/telemetry_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/common/request_metrics.go b/common/request_metrics.go index 302cae6..9646a33 100644 --- a/common/request_metrics.go +++ b/common/request_metrics.go @@ -5,9 +5,9 @@ import "sync" const MaxCountInTelemetryQueue = 10 type RequestMetrics struct { - PrevRequestId string - RequestId string - PrevRequestDuration int + PrevRequestId string `json:"prev_request_id"` + RequestId string `json:"request_id"` + PrevRequestDuration int `json:"prev_request_duration"` } type TelemetryQueue struct { diff --git a/test/telemetry_test.go b/test/telemetry_test.go index cfa2b83..b2c2c37 100644 --- a/test/telemetry_test.go +++ b/test/telemetry_test.go @@ -2,10 +2,12 @@ package test import ( "context" + "encoding/json" "fmt" "net/http" "net/http/httptest" "os" + "strings" "testing" "time" @@ -21,6 +23,12 @@ type mockHTTPClient struct { requestsReceived []*http.Request } +type telemetry struct { + PrevRequestId string `json:"prev_request_id"` + RequestId string `json:"request_id"` + PrevRequestDuration int `json:"prev_request_duration"` +} + // newMockHTTPClient initializes a new mockHTTPClient with a TLS test server, optionally introducing a delay. func newMockHTTPClient(delay time.Duration) *mockHTTPClient { mock := &mockHTTPClient{ @@ -61,6 +69,23 @@ func (m *mockHTTPClient) CountRequestsWithHeader(header string) int { return count } +// ValidateTelemetryHeadersFormat +func (m *mockHTTPClient) ValidateTelemetryHeadersFormat(header string) error { + headerKey := http.CanonicalHeaderKey(header) // Normalize header name. + for _, req := range m.requestsReceived { + if _, ok := req.Header[headerKey]; ok { + var telemetryObj telemetry + headerStr := req.Header[headerKey][0] + decoder := json.NewDecoder(strings.NewReader(headerStr)) + decoder.DisallowUnknownFields() + if err := decoder.Decode(&telemetryObj); err != nil { + return err + } + } + } + return nil +} + // createMockEnvironment creates a mock environment using the mock server's URL. func createMockEnvironment(mockServerURL string) *configuration.CheckoutEnv { return configuration.NewEnvironment( @@ -120,6 +145,8 @@ func TestShouldSendTelemetryByDefault(t *testing.T) { expectedTelemetryHeaderCount := 2 // Telemetry is sent in the 2nd and 3rd requests. telemetryHeaderCount := mock.CountRequestsWithHeader("cko-sdk-telemetry") assert.Equal(t, expectedTelemetryHeaderCount, telemetryHeaderCount, "Expected exactly %d requests to contain the telemetry header", expectedTelemetryHeaderCount) + + assert.Nil(t, mock.ValidateTelemetryHeadersFormat("cko-sdk-telemetry")) } // TestShouldNotSendTelemetryWhenOptedOut verifies that telemetry headers are not sent when telemetry is disabled.