Skip to content

Commit

Permalink
The common-metering-plugin now expands environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
maoueh committed Apr 25, 2024
1 parent 5d9af0b commit 300cf75
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
37 changes: 37 additions & 0 deletions cmd/apps/metering.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package apps

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/streamingfast/dmetering"
"go.uber.org/zap"
)

// GetCommonMeteringPlugin returns the common metering plugin value to use
// for the application. It reads the `common-metering-plugin` flag
// from the command and returns the plugin after expanding the
// environment variables in it meaning 'paymentGateway://test?token=${TOKEN}'.
func GetCommonMeteringPluginValue() string {
plugin := viper.GetString("common-metering-plugin")
return os.ExpandEnv(plugin)
}

// GetCommonMeteringPlugin returns the common metering plugin to use
// for the application. It reads the `common-metering-plugin` flag
// from the command and returns the plugin after expanding the
// environment variables in it meaning 'paymentGateway://test?token=${TOKEN}'.
func GetCommonMeteringPlugin(cmd *cobra.Command, logger *zap.Logger) (dmetering.EventEmitter, error) {
// We keep cmd as argument for future proofing, at which point we are going to break
// GetCommonMeteringPluginValue above.
_ = cmd

eventEmitter, err := dmetering.New(GetCommonMeteringPluginValue(), logger)
if err != nil {
return nil, fmt.Errorf("new metering plugin: %w", err)
}

return eventEmitter, nil
}
2 changes: 1 addition & 1 deletion cmd/apps/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func start(cmd *cobra.Command, dataDir string, args []string, rootLog *zap.Logge
return fmt.Errorf("protocol specific hooks not configured correctly: %w", err)
}

eventEmitter, err := dmetering.New(sflags.MustGetString(cmd, "common-metering-plugin"), rootLog)
eventEmitter, err := GetCommonMeteringPlugin(cmd, rootLog)
if err != nil {
return fmt.Errorf("unable to initialize dmetering: %w", err)
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/apps/substreams_tier1.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,9 @@ func RegisterSubstreamsTier1App[B firecore.Block](chain *firecore.Chain[B], root
wasmExtensions = exts
}

meteringConfig := viper.GetString("common-metering-plugin")

return app.NewTier1(appLogger,
&app.Tier1Config{
MeteringConfig: meteringConfig,
MeteringConfig: GetCommonMeteringPluginValue(),

MergedBlocksStoreURL: mergedBlocksStoreURL,
OneBlocksStoreURL: oneBlocksStoreURL,
Expand Down

0 comments on commit 300cf75

Please sign in to comment.