1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
package telemetry
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestBuildTelemetryAttributes(t *testing.T) {
metrics := &Metrics{}
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
req.Header.Set("User-Agent", "test-agent")
res := &http.Response{
StatusCode: http.StatusOK,
Header: make(http.Header),
}
res.Header.Set("openfga-authorization-model-id", "test-model-id")
res.Header.Set("fga-query-duration-ms", "123")
methodParameters := map[string]interface{}{
"storeId": "test-store-id",
"authorizationModelId": "test-model-id",
}
requestStarted := time.Now().Add(-500 * time.Millisecond)
resendCount := 2
attrs, queryDuration, requestDuration, err := metrics.BuildTelemetryAttributes("TestMethod", methodParameters, req, res, requestStarted, resendCount)
if err != nil {
t.Errorf("Expected no error from BuildTelemetryAttributes, but got %v", err)
}
if attrs[FGAClientRequestMethod] != "TestMethod" {
t.Errorf("Expected method to be 'TestMethod', but got %v", attrs[FGAClientRequestMethod])
}
if attrs[FGAClientRequestStoreID] != "test-store-id" {
t.Errorf("Expected store ID to be 'test-store-id', but got %v", attrs[FGAClientRequestStoreID])
}
if attrs[FGAClientRequestModelID] != "test-model-id" {
t.Errorf("Expected model ID to be 'test-model-id', but got %v", attrs[FGAClientRequestModelID])
}
if attrs[FGAClientResponseModelID] != "test-model-id" {
t.Errorf("Expected model ID in response to be 'test-model-id', but got %v", attrs[FGAClientResponseModelID])
}
if attrs[HTTPServerRequestDuration] != "123" {
t.Errorf("Expected query duration to be '123', but got %v", attrs[HTTPServerRequestDuration])
}
if attrs[HTTPRequestResendCount] != "2" {
t.Errorf("Expected resend count to be '2', but got %v", attrs[HTTPRequestResendCount])
}
if requestDuration <= 0 {
t.Errorf("Expected positive request duration, but got %v", requestDuration)
}
if queryDuration != 123.0 {
t.Errorf("Expected query duration to be 123.0, but got %v", queryDuration)
}
}
|