8000 enhancement(multitenant): Track rounding error in billing calculation by bboreham · Pull Request #3779 · weaveworks/scope · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

enhancement(multitenant): Track rounding error in billing calculation #3779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/multitenant/billing_emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/base64"
"flag"
"math"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -37,6 +38,7 @@ type BillingEmitter struct {

sync.Mutex
intervalCache map[string]time.Duration
rounding map[string]float64
}

// NewBillingEmitter changes a new billing emitter which emits billing events
Expand All @@ -46,6 +48,7 @@ func NewBillingEmitter(upstream app.Collector, billingClient *billing.Client, cf
billing: billingClient,
BillingEmitterConfig: cfg,
intervalCache: make(map[string]time.Duration),
rounding: make(map[string]float64),
}, nil
}

Expand Down Expand Up @@ -84,9 +87,14 @@ func (e *BillingEmitter) Add(ctx context.Context, rep report.Report, buf []byte)
weaveNetCount = 1
}

// Billing takes an integer number of seconds, so keep track of the amount lost to rounding
nodeSeconds := interval.Seconds()*float64(len(rep.Host.Nodes)) + e.rounding[userID]
rounding := nodeSeconds - math.Floor(nodeSeconds)
e.rounding[userID] = rounding

amounts := billing.Amounts{
billing.ContainerSeconds: int64(interval/time.Second) * int64(len(rep.Container.Nodes)),
billing.NodeSeconds: int64(interval/time.Second) * int64(len(rep.Host.Nodes)),
billing.NodeSeconds: int64(nodeSeconds),
billing.WeaveNetSeconds: int64(interval/time.Second) * int64(weaveNetCount),
}
metadata := map[string]string{
Expand Down
0