Skip to content

Commit

Permalink
fix(event): Validation rule to skip events with 0 value (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoaguimaraes authored Nov 16, 2022
1 parent ade852a commit 925801e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/component/marketplace/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package marketplace

import "github.com/kelseyhightower/envconfig"

// TimeLayout ISO time layout
const TimeLayout = "2006-01-02T15:04:05.000Z"

// AzureMarketplaceConfiguration represents the configuration for marketplace client.
type AzureMarketplaceConfiguration struct {
ResourceUri string `envconfig:"AZURE_MANAGED_APP_RESOURCE_URI" required:"true"`
Expand Down
19 changes: 19 additions & 0 deletions pkg/component/marketplace/marketplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package marketplace
import (
"context"
"net/http"
"time"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
Expand Down Expand Up @@ -32,6 +33,15 @@ func NewMarketplaceClient(config AzureMarketplaceConfiguration, authorizer autor

// CreateUsageEvent sends usage event to marketplace api for metering purpose.
func (c MarketplaceClient) CreateUsageEvent(ctx context.Context, event cloud.UsageEventReq) (cloud.UsageEventRes, error) {
if event.Quantity <= 0 {
c.logger.Infof("metric '%s' skipped (%s <-> %s) = %v",
event.DimensionID,
event.StartAt.Format(TimeLayout),
time.Now().Format(TimeLayout),
event.Quantity,
)
return cloud.UsageEventRes{}, nil
}
azevent := UsageEventReq{
ResourceId: c.config.ResourceUri,
Plan: c.config.PlanId,
Expand Down Expand Up @@ -65,6 +75,15 @@ func (c MarketplaceClient) CreateUsageEventBatch(ctx context.Context, batch clou
events := []UsageEventReq{}
resourceDimension := map[string]string{}
for _, request := range batch.Request {
if request.Quantity <= 0 {
c.logger.Infof("metric '%s' skipped (%s <-> %s) = %v",
request.DimensionID,
request.StartAt.Format(TimeLayout),
time.Now().Format(TimeLayout),
request.Quantity,
)
continue
}
resourceDimension[c.config.ResourceUri] = request.DimensionID
event := UsageEventReq{
ResourceId: c.config.ResourceUri,
Expand Down

0 comments on commit 925801e

Please sign in to comment.