Skip to content
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

Fix: Change telemetry to snake case #141

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading