Skip to content

Commit fc001bf

Browse files
committed
remove all the otel and slog changes
1 parent a959d34 commit fc001bf

File tree

8 files changed

+302
-23
lines changed

8 files changed

+302
-23
lines changed

iavlx/commit_multi_tree.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"bytes"
55
"fmt"
66
io "io"
7-
"log/slog"
87
"os"
98
"path/filepath"
109
"sync"
1110

1211
dbm "github.com/cosmos/cosmos-db"
1312
protoio "github.com/cosmos/gogoproto/io"
1413

14+
"cosmossdk.io/log"
1515
"cosmossdk.io/store/mem"
1616
"cosmossdk.io/store/metrics"
1717
pruningtypes "cosmossdk.io/store/pruning/types"
@@ -23,7 +23,7 @@ import (
2323
type CommitMultiTree struct {
2424
dir string
2525
opts Options
26-
logger *slog.Logger
26+
logger log.Logger
2727
trees []storetypes.CommitStore // always ordered by tree name
2828
treeKeys []storetypes.StoreKey // always ordered by tree name
2929
storeTypes []storetypes.StoreType // store types by tree index
@@ -377,7 +377,7 @@ func (db *CommitMultiTree) SetMetrics(metrics metrics.StoreMetrics) {
377377
db.logger.Warn("SetMetrics is not implemented for CommitMultiTree")
378378
}
379379

380-
func LoadDB(path string, opts *Options, logger *slog.Logger) (*CommitMultiTree, error) {
380+
func LoadDB(path string, opts *Options, logger log.Logger) (*CommitMultiTree, error) {
381381
// n := len(treeNames)
382382
//trees := make([]*CommitTree, n)
383383
//treesByName := make(map[string]int, n)

iavlx/commit_tree.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package iavlx
33
import (
44
"fmt"
55
"io"
6-
"log/slog"
76
"sync"
87
"sync/atomic"
98

9+
"cosmossdk.io/log"
1010
pruningtypes "cosmossdk.io/store/pruning/types"
1111
storetypes "cosmossdk.io/store/types"
1212
)
@@ -28,13 +28,13 @@ type CommitTree struct {
2828

2929
pendingOrphans [][]NodeID
3030

31-
logger *slog.Logger
31+
logger log.Logger
3232

3333
lastCommitId storetypes.CommitID
3434
commitCtx *commitContext
3535
}
3636

37-
func NewCommitTree(dir string, opts Options, logger *slog.Logger) (*CommitTree, error) {
37+
func NewCommitTree(dir string, opts Options, logger log.Logger) (*CommitTree, error) {
3838
ts, err := NewTreeStore(dir, opts, logger)
3939
if err != nil {
4040
return nil, fmt.Errorf("failed to create tree store: %w", err)

iavlx/tree_store.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ package iavlx
33
import (
44
"errors"
55
"fmt"
6-
"log/slog"
76
"sync"
87
"sync/atomic"
98
"time"
109

1110
"github.com/tidwall/btree"
11+
12+
"cosmossdk.io/log"
1213
)
1314

1415
type TreeStore struct {
15-
logger *slog.Logger
16+
logger log.Logger
1617
dir string
1718

1819
currentWriter *ChangesetWriter
@@ -39,7 +40,7 @@ type changesetEntry struct {
3940
changeset atomic.Pointer[Changeset]
4041
}
4142

42-
func NewTreeStore(dir string, options Options, logger *slog.Logger) (*TreeStore, error) {
43+
func NewTreeStore(dir string, options Options, logger log.Logger) (*TreeStore, error) {
4344
ts := &TreeStore{
4445
dir: dir,
4546
changesets: &btree.Map[uint32, *changesetEntry]{},

server/api/server.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"context"
5+
"fmt"
56
"net"
67
"net/http"
78
"strings"
@@ -23,6 +24,7 @@ import (
2324
"github.com/cosmos/cosmos-sdk/codec/legacy"
2425
"github.com/cosmos/cosmos-sdk/server/config"
2526
cmtlogwrapper "github.com/cosmos/cosmos-sdk/server/log"
27+
"github.com/cosmos/cosmos-sdk/telemetry"
2628
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
2729
)
2830

@@ -33,6 +35,7 @@ type Server struct {
3335
ClientCtx client.Context
3436
GRPCSrv *grpc.Server
3537
logger log.Logger
38+
metrics *telemetry.Metrics
3639

3740
// Start() is blocking and generally called from a separate goroutine.
3841
// Close() can be called asynchronously and access shared memory
@@ -188,6 +191,31 @@ func (s *Server) Close() error {
188191
return s.listener.Close()
189192
}
190193

194+
func (s *Server) SetTelemetry(m *telemetry.Metrics) {
195+
s.mtx.Lock()
196+
s.registerMetrics(m)
197+
s.mtx.Unlock()
198+
}
199+
200+
func (s *Server) registerMetrics(m *telemetry.Metrics) {
201+
s.metrics = m
202+
203+
metricsHandler := func(w http.ResponseWriter, r *http.Request) {
204+
format := strings.TrimSpace(r.FormValue("format"))
205+
206+
gr, err := s.metrics.Gather(format)
207+
if err != nil {
208+
writeErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to gather metrics: %s", err))
209+
return
210+
}
211+
212+
w.Header().Set("Content-Type", gr.ContentType)
213+
_, _ = w.Write(gr.Metrics)
214+
}
215+
216+
s.Router.HandleFunc("/metrics", metricsHandler).Methods("GET")
217+
}
218+
191219
// errorResponse defines the attributes of a JSON error response.
192220
type errorResponse struct {
193221
Code int `json:"code,omitempty"`

server/config/config.go

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package config
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"math"
67

78
"github.com/spf13/viper"
9+
"google.golang.org/grpc"
810

911
pruningtypes "cosmossdk.io/store/pruning/types"
1012

11-
_ "github.com/cosmos/cosmos-sdk/telemetry"
13+
"github.com/cosmos/cosmos-sdk/telemetry"
1214
sdk "github.com/cosmos/cosmos-sdk/types"
1315
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
1416
)
@@ -124,6 +126,21 @@ type APIConfig struct {
124126
// Ref: https://github.com/cosmos/cosmos-sdk/issues/6420
125127
}
126128

129+
// BlockRange represents a range of block heights as [start_block, end_block] (inclusive).
130+
// It is used to map gRPC historical connections to specific block height ranges for routing
131+
// historical queries to appropriate archive nodes.
132+
//
133+
// Example:
134+
// - [0, 1000] represents blocks from genesis (0) through block 1000
135+
// - [1001, 2000] represents blocks from 1001 through 2000
136+
//
137+
// Both start and end blocks must be non-negative, and start must be less than or equal to end.
138+
type BlockRange [2]int
139+
140+
// HistoricalGRPCConnections is a map of block ranges to gRPC client connections
141+
// used for routing requests to different backend nodes based on block height.
142+
type HistoricalGRPCConnections map[BlockRange]*grpc.ClientConn
143+
127144
// GRPCConfig defines configuration for the gRPC server.
128145
type GRPCConfig struct {
129146
// Enable defines if the gRPC server should be enabled.
@@ -142,6 +159,9 @@ type GRPCConfig struct {
142159

143160
// SkipCheckHeader defines if the gRPC server should bypass header checking.
144161
SkipCheckHeader bool `mapstructure:"skip-check-header"`
162+
163+
// HistoricalGRPCAddressBlockRange maps block ranges to gRPC addresses for routing historical queries.
164+
HistoricalGRPCAddressBlockRange map[BlockRange]string `mapstructure:"-"`
145165
}
146166

147167
// GRPCWebConfig defines configuration for the gRPC-web server.
@@ -190,12 +210,13 @@ type Config struct {
190210
BaseConfig `mapstructure:",squash"`
191211

192212
// Telemetry defines the application telemetry configuration
193-
API APIConfig `mapstructure:"api"`
194-
GRPC GRPCConfig `mapstructure:"grpc"`
195-
GRPCWeb GRPCWebConfig `mapstructure:"grpc-web"`
196-
StateSync StateSyncConfig `mapstructure:"state-sync"`
197-
Streaming StreamingConfig `mapstructure:"streaming"`
198-
Mempool MempoolConfig `mapstructure:"mempool"`
213+
Telemetry telemetry.Config `mapstructure:"telemetry"`
214+
API APIConfig `mapstructure:"api"`
215+
GRPC GRPCConfig `mapstructure:"grpc"`
216+
GRPCWeb GRPCWebConfig `mapstructure:"grpc-web"`
217+
StateSync StateSyncConfig `mapstructure:"state-sync"`
218+
Streaming StreamingConfig `mapstructure:"streaming"`
219+
Mempool MempoolConfig `mapstructure:"mempool"`
199220
}
200221

201222
// SetMinGasPrices sets the validator's minimum gas prices.
@@ -233,6 +254,10 @@ func DefaultConfig() *Config {
233254
IAVLDisableFastNode: false,
234255
AppDBBackend: "",
235256
},
257+
Telemetry: telemetry.Config{
258+
Enabled: false,
259+
GlobalLabels: [][]string{},
260+
},
236261
API: APIConfig{
237262
Enable: false,
238263
Swagger: false,
@@ -273,6 +298,26 @@ func GetConfig(v *viper.Viper) (Config, error) {
273298
if err := v.Unmarshal(conf); err != nil {
274299
return Config{}, fmt.Errorf("error extracting app config: %w", err)
275300
}
301+
raw := v.GetString("grpc.historical-grpc-address-block-range")
302+
if len(raw) > 0 {
303+
data := make(map[string]BlockRange)
304+
if err := json.Unmarshal([]byte(raw), &data); err != nil {
305+
return Config{}, fmt.Errorf("failed to parse historical-grpc-address-block-range as JSON: %w (value: %s)", err, raw)
306+
}
307+
historicalGRPCAddressBlockRange := make(map[BlockRange]string, len(data))
308+
for address, blockRange := range data {
309+
if blockRange[0] < 0 || blockRange[1] < 0 {
310+
return Config{}, fmt.Errorf("invalid block range [%d, %d] for address %s: block numbers cannot be negative",
311+
blockRange[0], blockRange[1], address)
312+
}
313+
if blockRange[0] > blockRange[1] {
314+
return Config{}, fmt.Errorf("invalid block range [%d, %d] for address %s: start block must be <= end block",
315+
blockRange[0], blockRange[1], address)
316+
}
317+
historicalGRPCAddressBlockRange[blockRange] = address
318+
}
319+
conf.GRPC.HistoricalGRPCAddressBlockRange = historicalGRPCAddressBlockRange
320+
}
276321
return *conf, nil
277322
}
278323

0 commit comments

Comments
 (0)