Skip to content

Commit

Permalink
Fix: Change telemetry to snake case
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-saxelby-cko committed Dec 16, 2024
1 parent 00418be commit dba59d9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions common/request_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions test/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package test

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"

Expand All @@ -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{
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit dba59d9

Please sign in to comment.