Skip to content

Commit

Permalink
Switch SendUsage to use Resty Request
Browse files Browse the repository at this point in the history
To ensure goroutine-safe
  • Loading branch information
alexhung committed May 1, 2024
1 parent ddc7eff commit dfd9eb4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion util/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 21 additions & 16 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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")

Expand Down

0 comments on commit dfd9eb4

Please sign in to comment.