Skip to content
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

Bugfix/metering #517

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions cmd/substreams/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ func init() {
func runBuildE(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

var manifestPath string
if len(args) > 1 && args[1] != "" {
manifestPath = args[1]
} else {
manifestPath = sflags.MustGetString(cmd, "manifest")
}
// Parse substreams.yaml
manifestPath := sflags.MustGetString(cmd, "manifest")
if manifestPath == "" {
var err error
manifestPath, err = findManifest()
manifestPath, err = findManifest(manifestPath)
if err != nil {
return fmt.Errorf("error finding manifest: %w", err)
}
Expand Down Expand Up @@ -316,17 +321,13 @@ func (s *SPKGPacker) Build(ctx context.Context) error {

// findManifest searches for the substreams.yaml file starting from the current directory
// and moving up to the parent directories until it finds the file or reaches the user's $HOME directory.
func findManifest() (string, error) {
func findManifest(originalPath string) (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("error getting user home directory: %w", err)
}

originalDir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("error getting current directory: %w", err)
}

originalDir := filepath.Dir(originalPath)
currentDir := originalDir
for {
manifestPath := filepath.Join(currentDir, "substreams.yaml")
Expand Down
2 changes: 0 additions & 2 deletions pipeline/process_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ func (p *Pipeline) processBlock(
}
case bstream.StepNew:
p.blockStepMap[bstream.StepNew]++

dmetering.GetBytesMeter(ctx).AddBytesRead(execOutput.Len())
err = p.handleStepNew(ctx, clock, cursor, execOutput)
if err != nil && err != io.EOF {
return fmt.Errorf("step new: handler step new: %w", err)
Expand Down
18 changes: 13 additions & 5 deletions service/metering.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ func sendMetering(ctx context.Context, meter dmetering.Meter, userID, apiKeyID,
inputBytes := meter.GetCount("wasm_input_bytes")
meter.ResetCount("wasm_input_bytes")

uncompressedBytesRead := meter.GetCount("uncompressed_read_bytes")
meter.ResetCount("uncompressed_read_bytes")

uncompressedLiveBytesRead := meter.GetCount("uncompressed_live_read_bytes")
meter.ResetCount("uncompressed_live_read_bytes")

event := dmetering.Event{
UserID: userID,
ApiKeyID: apiKeyID,
Expand All @@ -25,11 +31,13 @@ func sendMetering(ctx context.Context, meter dmetering.Meter, userID, apiKeyID,

Endpoint: endpoint,
Metrics: map[string]float64{
"egress_bytes": float64(egressBytes),
"written_bytes": float64(bytesWritten),
"read_bytes": float64(bytesRead),
"wasm_input_bytes": float64(inputBytes),
"message_count": 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

y'avait-il pas déjà une convention d'avoir l'unité à la fin? donc uncompressed_read_bytes plutôt que uncompressed_bytes_read ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, i will make the change

"egress_bytes": float64(egressBytes),
"written_bytes": float64(bytesWritten),
"read_bytes": float64(bytesRead),
"wasm_input_bytes": float64(inputBytes),
"uncompressed_read_bytes": float64(uncompressedBytesRead),
"uncompressed_live_read_bytes": float64(uncompressedLiveBytesRead),
"message_count": 1,
},
Timestamp: time.Now(),
}
Expand Down
21 changes: 20 additions & 1 deletion service/tier1.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ func (s *Tier1Service) Blocks(
ctx = reqctx.WithTracer(ctx, s.tracer)
ctx = dmetering.WithBytesMeter(ctx)
ctx = dmetering.WithCounter(ctx, "wasm_input_bytes")
ctx = dmetering.WithCounter(ctx, "live_bytes_uncompressed")
ctx = dmetering.WithCounter(ctx, "stored_bytes_uncompressed")
ctx = reqctx.WithTier2RequestParameters(ctx, s.tier2RequestParameters)

ctx, span := reqctx.WithSpan(ctx, "substreams/tier1/request")
Expand Down Expand Up @@ -568,9 +570,26 @@ func (s *Tier1Service) blocks(ctx context.Context, request *pbsubstreamsrpc.Requ
streamHandler = pipe
}

// We need to meter the bytes read from the live stream, as they are not metered by the dstore meters
blockMeteredHandlerFunc := bstream.HandlerFunc(func(blk *pbbstream.Block, obj interface{}) error {
step := obj.(bstream.Stepable).Step()
switch step {
case bstream.StepNewIrreversible:
// already metered by the store in compressed bytes.
// we will meter them here for potential future use
dmetering.GetBytesMeter(ctx).CountInc("uncompressed_read_bytes", len(blk.Payload.GetValue()))
default:
// all other cases are live blocks to be metered
dmetering.GetBytesMeter(ctx).AddBytesRead(len(blk.Payload.GetValue()))
dmetering.GetBytesMeter(ctx).CountInc("uncompressed_live_read_bytes", len(blk.Payload.GetValue()))
}

return streamHandler.ProcessBlock(blk, obj)
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, donc le tier2 va lire des blocks, et ici c'est la source elle-même qui va être metered, avec des métriques différentes, fack on fera l'aggrégation à la sortie, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the tier2 doesn't play into this. tier2 does not have live blocks and so the blockstream doesn't need to be instrumented. all of the tier2's data sources are dstores which are metered.

blockStream, err := s.streamFactoryFunc(
ctx,
streamHandler,
blockMeteredHandlerFunc,
int64(requestDetails.LinearHandoffBlockNum),
request.StopBlockNum,
cursor,
Expand Down
Loading