diff --git a/CHANGELOG.md b/CHANGELOG.md index b9df0ff..9fd0ff4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 1.25.1 (May 2, 2024) + +IMPROVEMENTS: + +* Switch SendUsage func arg to Resty Request to make gorountine-safe + ## 1.25.0 (Apr 30, 2024) IMPROVEMENTS: diff --git a/util/sdk/sdk.go b/util/sdk/sdk.go index c67ebcf..0a11b00 100644 --- a/util/sdk/sdk.go +++ b/util/sdk/sdk.go @@ -184,7 +184,7 @@ func applyTelemetry(productId, resource, verb string, f func(context.Context, *s // best effort. Go routine it if m, ok := meta.(util.ProviderMetadata); ok { featureUsage := fmt.Sprintf("Resource/%s/%s", resource, verb) - go util.SendUsage(ctx, m.Client, productId, featureUsage) + go util.SendUsage(ctx, m.Client.R(), productId, featureUsage) } return f(ctx, data, meta) diff --git a/util/util.go b/util/util.go index 3bc2223..56cb83d 100644 --- a/util/util.go +++ b/util/util.go @@ -25,29 +25,34 @@ func resourceFeatureUsage(resourceName, method string) string { return fmt.Sprintf("Resource/%s/%s", resourceName, method) } -func SendUsageResourceCreate(ctx context.Context, client *resty.Client, productId, resourceName string) { - SendUsage(ctx, client, productId, resourceFeatureUsage(resourceName, "CREATE")) +func SendUsageResourceCreate(ctx context.Context, req *resty.Request, productId, resourceName string) { + SendUsage(ctx, req, productId, resourceFeatureUsage(resourceName, "CREATE")) } -func SendUsageResourceRead(ctx context.Context, client *resty.Client, productId, resourceName string) { - SendUsage(ctx, client, productId, resourceFeatureUsage(resourceName, "READ")) +func SendUsageResourceRead(ctx context.Context, req *resty.Request, productId, resourceName string) { + SendUsage(ctx, req, productId, resourceFeatureUsage(resourceName, "READ")) } -func SendUsageResourceUpdate(ctx context.Context, client *resty.Client, productId, resourceName string) { - SendUsage(ctx, client, productId, resourceFeatureUsage(resourceName, "UPDATE")) +func SendUsageResourceUpdate(ctx context.Context, req *resty.Request, productId, resourceName string) { + SendUsage(ctx, req, productId, resourceFeatureUsage(resourceName, "UPDATE")) } -func SendUsageResourceDelete(ctx context.Context, client *resty.Client, productId, resourceName string) { - SendUsage(ctx, client, productId, resourceFeatureUsage(resourceName, "DELETE")) +func SendUsageResourceDelete(ctx context.Context, req *resty.Request, productId, resourceName string) { + SendUsage(ctx, req, productId, resourceFeatureUsage(resourceName, "DELETE")) } -func SendUsage(ctx context.Context, client *resty.Client, productId string, featureUsages ...string) { - type Feature struct { - FeatureId string `json:"featureId"` - } - type UsageStruct struct { - ProductId string `json:"productId"` - Features []Feature `json:"features"` +type Feature struct { + FeatureId string `json:"featureId"` +} +type UsageStruct struct { + ProductId string `json:"productId"` + Features []Feature `json:"features"` +} + +func SendUsage(ctx context.Context, req *resty.Request, productId string, featureUsages ...string) { + if req == nil { + tflog.Info(ctx, "SendUsage req is nil. Skipping.") + return } features := []Feature{ @@ -60,7 +65,7 @@ func SendUsage(ctx context.Context, client *resty.Client, productId string, feat usage := UsageStruct{productId, features} - resp, err := client.R(). + resp, err := req. SetBody(usage). Post("artifactory/api/system/usage")