From ac935fb27637b62184354bc0ee5b5d607431beb8 Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Tue, 21 Jan 2025 16:38:34 -0600 Subject: [PATCH] fix: api client should set default telemetry if not specified --- api_client.go | 3 +++ api_client_test.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 api_client_test.go diff --git a/api_client.go b/api_client.go index 979587e..42c47a1 100644 --- a/api_client.go +++ b/api_client.go @@ -65,6 +65,9 @@ type service struct { // NewAPIClient creates a new API client. Requires a userAgent string describing your application. // optionally a custom http.Client to allow for advanced features such as caching. func NewAPIClient(cfg *Configuration) *APIClient { + if cfg.Telemetry == nil { + cfg.Telemetry = telemetry.DefaultTelemetryConfiguration() + } if cfg.HTTPClient == nil { if cfg.Credentials == nil { cfg.HTTPClient = http.DefaultClient diff --git a/api_client_test.go b/api_client_test.go new file mode 100644 index 0000000..1f9fc80 --- /dev/null +++ b/api_client_test.go @@ -0,0 +1,23 @@ +package openfga + +import ( + "github.com/openfga/go-sdk/telemetry" + "net/http" + "testing" + "time" +) + +func TestApiClientCreatedWithDefaultTelemetry(t *testing.T) { + cfg := Configuration{ + HTTPClient: &http.Client{Timeout: 10 * time.Second}, + ApiUrl: "http://localhost:8080/", + } + _ = NewAPIClient(&cfg) + + telemetry1 := telemetry.Get(telemetry.TelemetryFactoryParameters{Configuration: cfg.Telemetry}) + telemetry2 := telemetry.Get(telemetry.TelemetryFactoryParameters{Configuration: cfg.Telemetry}) + + if telemetry1 != telemetry2 { + t.Fatalf("Telemetry instance should be the same") + } +}