Skip to content

Commit

Permalink
simplifying the config, refactor logging, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
semyon-dev committed Jul 15, 2024
1 parent 1e3e15d commit 34050c8
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Available Commands:
channel Manage operations on payment channels
freecall Manage operations on free call users
help Help about any command
init Write default configuration to file
init Write basic configuration to file
init-full Write full default configuration to file
list List channels, claims in progress, etc
serve Is the default option which starts the Daemon.
Expand Down
33 changes: 11 additions & 22 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ const (
"max_message_size_in_mb" : 4,
"daemon_type": "grpc",
"enable_dynamic_pricing":false,
"hdwallet_index": 0,
"hdwallet_mnemonic": "",
"allowed_user_flag" :false,
"auto_ssl_domain": "",
"auto_ssl_cache_dir": ".certs",
Expand All @@ -104,7 +102,7 @@ const (
"timestamp_format": "2006-01-02T15:04:05.999Z07:00"
},
"output": {
"type": "file",
"type": ["file", "stdout"],
"file_pattern": "./snet-daemon.%Y%m%d.log",
"current_link": "./snet-daemon.log",
"max_size_in_mb": 10,
Expand All @@ -114,10 +112,9 @@ const (
"hooks": []
},
"payment_channel_storage_client": {
"connection_timeout": "5s",
"request_timeout": "3s",
"endpoints": ["http://127.0.0.1:2379"]
},
"connection_timeout": "0s",
"request_timeout": "0s"
},
"payment_channel_storage_server": {
"id": "storage-1",
"scheme": "http",
Expand All @@ -137,29 +134,21 @@ const (
"token_expiry_in_minutes": 1440,
"model_training_enabled": false
}`
MinimumConfigJson string = ` {
MinimumConfigJson string = `{
"blockchain_enabled": true,
"blockchain_network_selected": "sepolia",
"passthrough_endpoint":"YOUR_SERVICE_ENDPOINT",
"service_id": "YOUR_SERVICE_ID",
"organization_id": "YOUR_ORG_ID",
"daemon_end_point": "127.0.0.1:8080",
"daemon_group_name":"default_group",
"passthrough_enabled": true,
"passthrough_endpoint":"YOUR_SERVICE_ENDPOINT",
"service_id": "ExampleServiceId",
"organization_id": "ExampleOrganizationId",
"payment_channel_storage_type": "etcd",
"ipfs_end_point": "http://ipfs.singularitynet.io:80",
"log": {
"level": "info",
"timezone": "UTC",
"formatter": {
"type": "text",
},
"log": {
"output": {
"type": "file",
"type": ["file", "stdout"]
}
},
"payment_channel_storage_client": {
"endpoints": ["http://127.0.0.1:2379"]
}}`
)

Expand Down Expand Up @@ -232,7 +221,7 @@ func Validate() error {

// Check if the Daemon is on the latest version or not
if message, err := CheckVersionOfDaemon(); err != nil {
//In case of any error on version check , just log it
//In case of any error on version check, just log it
zap.L().Warn(err.Error())
} else {
zap.L().Info(message)
Expand Down
9 changes: 6 additions & 3 deletions config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -33,10 +34,12 @@ func GetBuildTime() string {
// This function is called to see if the current daemon is on the latest version , if it is not, indicate this to the user
// when the daemon starts.
func CheckVersionOfDaemon() (message string, err error) {
latestVersionFromGit, err := GetLatestDaemonVersion()
var latestVersionFromGit string
message = "Daemon version is " + versionTag
latestVersionFromGit, err = GetLatestDaemonVersion()
if len(versionTag) > 0 && err == nil {
if strings.Compare(latestVersionFromGit, versionTag) != 0 {
message = fmt.Sprintf("There is a newer version of the Daemon %v available. You are currently on %v, please consider upgrading.", latestVersionFromGit, versionTag)
err = errors.New(fmt.Sprintf("There is a newer version of the Daemon %v available. You are currently on %v, please consider upgrading.", latestVersionFromGit, versionTag))
}
}
return message, err
Expand All @@ -45,7 +48,7 @@ func CheckVersionOfDaemon() (message string, err error) {
func GetLatestDaemonVersion() (version string, err error) {
resp, err := http.Get("https://api.github.com/repos/singnet/snet-daemon/releases/latest")
if err != nil {
return "", err
return "", fmt.Errorf("error getting latest daemon version from github: %+v", err)
}
defer resp.Body.Close()

Expand Down
14 changes: 8 additions & 6 deletions etcddb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ There are two payment channel storage types which are currently supported by sne
*memory* storage type is used in configuration where only one service replica is used by snet-daemon or
for testing purposes.

To run snet-daemon with several replicas set the payment_channel_storage_type is now initialized from Organizatio metadata:
To run snet-daemon with several replicas set the payment_channel_storage_type is now initialized from Organization metadata:
```json
{
"payment_channel_storage_type": "etcd"
Expand All @@ -25,13 +25,15 @@ To run snet-daemon with several replicas set the payment_channel_storage_type is

*payment_channel_storage_client* JSON map can be used to configure payment channel storage etcd client.

| Field name | Description |Default Value |
|--------------------|-----------------------------------------------|-------------------------|
| connection_timeout | timeout for failing to establish a connection |5 seconds |
| request_timeout | per request timeout |3 seconds |
| endpoints | list of etcd cluster endpoints (host:port) |["http://127.0.0.1:2379"]|
| Field name | Description | Default Value |
|--------------------|-----------------------------------------------|-----------------------------|
| connection_timeout | timeout for failing to establish a connection | from org metadata |
| request_timeout | per request timeout | from org metadata |
| endpoints | list of etcd cluster endpoints (host:port) | from org metadata |


**endpoints from the config are ignored and taken only from ipfs metadata**

Endpoints consist of a list of URLs which points to etcd cluster servers.


Expand Down
11 changes: 8 additions & 3 deletions etcddb/etcddb_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ func NewEtcdClientFromVip(vip *viper.Viper, metaData *blockchain.OrganizationMet
return nil, err
}

zap.L().Info("Creating new payment storage client", zap.Any("config", conf))
zap.L().Info("Creating new payment storage client (etcdv3)",
zap.String("ConnectionTimeout", conf.ConnectionTimeout.String()),
zap.String("RequestTimeout", conf.RequestTimeout.String()),
zap.Strings("Endpoints", conf.Endpoints))

var etcdv3 *clientv3.Client

Expand All @@ -82,9 +85,11 @@ func NewEtcdClientFromVip(vip *viper.Viper, metaData *blockchain.OrganizationMet
}
}

session, err := concurrency.NewSession(etcdv3)
ctx, cancel := context.WithTimeout(context.Background(), conf.RequestTimeout)
defer cancel()
session, err := concurrency.NewSession(etcdv3, concurrency.WithContext(ctx))
if err != nil {
return
return nil, fmt.Errorf("can't connect to etcddb: %v", err)
}

client = &EtcdClient{
Expand Down
12 changes: 12 additions & 0 deletions etcddb/etcddb_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ func GetEtcdClientConf(vip *viper.Viper, metaData *blockchain.OrganizationMetaDa
Endpoints: metaData.GetPaymentStorageEndPoints(),
}

confFromVip := &EtcdClientConf{}
subVip := config.SubWithDefault(vip, config.PaymentChannelStorageClientKey)
err = subVip.Unmarshal(confFromVip)

if confFromVip.RequestTimeout != 0 {
conf.RequestTimeout = confFromVip.RequestTimeout
}

if confFromVip.ConnectionTimeout != 0 {
conf.ConnectionTimeout = confFromVip.ConnectionTimeout
}

return
}

Expand Down
8 changes: 8 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -116,6 +117,13 @@ func createEncoderConfig() (*zapcore.EncoderConfig, error) {
}
}
encoderConfig.EncodeCaller = zapcore.FullCallerEncoder
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder

// colorless logs for files
if slices.Contains(config.GetStringSlice(LogOutputTypeKey), "file") {
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
}

return &encoderConfig, nil
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PARENT_PATH=$(dirname $(cd $(dirname $0); pwd -P))

if [ $# -lt 3 ]
then
echo "arguments expected are of the form <OS> <PLATFORM> and <VERSION> for the build script , as an example: '/scripts/build linux amd64 v.0.1.8'"
echo "arguments expected are of the form <OS> <PLATFORM> and <VERSION> for the build script , as an example: '/scripts/build linux amd64 v5.1.5'"
exit 1
fi
pushd $PARENT_PATH
Expand Down
2 changes: 1 addition & 1 deletion snetd/cmd/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func loadConfigFileFromCommandLine(configFlag *pflag.Flag) {
if configFlag.Changed || isFileExist(configFile) {
err := config.LoadConfig(configFile)
if err != nil {
panic(fmt.Sprint("[CONFIG] Error reading configuration file"))
panic(fmt.Errorf("[CONFIG] Error reading configuration file: %v", err))
}
fmt.Println("[CONFIG] Using custom configuration file")
} else {
Expand Down
22 changes: 12 additions & 10 deletions snetd/cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cmd

import (
"fmt"
"os"

"github.com/singnet/snet-daemon/config"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

var InitCmd = &cobra.Command{
Expand All @@ -16,17 +16,17 @@ var InitCmd = &cobra.Command{
var err error
var configFile = cmd.Flags().Lookup("config").Value.String()

zap.L().Info("Writing default configuration to the file", zap.String("config", configFile))

if isFileExist(configFile) {
zap.L().Fatal("such configFile already exists, please remove file first or rename file",
zap.String("configFile", configFile))
fmt.Println("ERROR: such configFile already exists, please remove file first or rename file:", configFile)
os.Exit(-1)
}

err = os.WriteFile(configFile, []byte(config.MinimumConfigJson), 0666)
if err != nil {
zap.L().Fatal("Cannot write configuration", zap.String("config", configFile), zap.Error(err))
fmt.Println("ERROR: Cannot write configuration rename file:", configFile)
os.Exit(-1)
}
fmt.Println("Writing basic configuration to the file:", configFile)
},
}

Expand All @@ -38,15 +38,17 @@ var InitFullCmd = &cobra.Command{
var err error
var configFile = cmd.Flags().Lookup("config").Value.String()

zap.L().Info("Writing full configuration to the file", zap.String("config", configFile))

if isFileExist(configFile) {
zap.L().Fatal("such configFile already exists, please remove file first or rename file", zap.String("config", configFile))
fmt.Println("ERROR: such configFile already exists, please remove file first or rename file:", configFile)
os.Exit(-1)
}

err = config.WriteConfig(configFile)
if err != nil {
zap.L().Fatal("Cannot write full configuration", zap.Error(err), zap.String("config", configFile))
fmt.Println("ERROR: Cannot write full configuration to file:", configFile)
os.Exit(-1)
}

fmt.Println("Writing full default configuration to the file:", configFile)
},
}

0 comments on commit 34050c8

Please sign in to comment.