Documentation
¶
Overview ¶
Package stats provides Prometheus metrics export for blockchain statistics.
Package stats provides blockchain statistics, counters, and caching functionality. Implements gas price oracle, block time calculations, and aggregate metrics.
Index ¶
- func GetMetricsHelp() string
- type AddressStats
- type BlockTimeStats
- type Cache
- type Config
- type Counter
- type Counters
- type DailyStats
- type GasPrice
- type GasPrices
- type HourlyStats
- type MetricDefinition
- type MetricsExporter
- type Service
- func (s *Service) GetAddressStats(ctx context.Context, address string) (*AddressStats, error)
- func (s *Service) GetAverageBlockTime(ctx context.Context) (*BlockTimeStats, error)
- func (s *Service) GetCounters(ctx context.Context) (*Counters, error)
- func (s *Service) GetDailyTransactionStats(ctx context.Context, days int) ([]DailyStats, error)
- func (s *Service) GetGasPrices(ctx context.Context) (*GasPrices, error)
- func (s *Service) GetHourlyGasUsage(ctx context.Context) ([]HourlyStats, error)
- func (s *Service) GetTokenStats(ctx context.Context, tokenAddress string) (*TokenStats, error)
- func (s *Service) InvalidateCache()
- func (s *Service) StartBackgroundUpdater(ctx context.Context)
- func (s *Service) Tbl(name string) string
- type TokenStats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetMetricsHelp ¶
func GetMetricsHelp() string
GetMetricsHelp returns Prometheus-formatted HELP and TYPE lines
Types ¶
type AddressStats ¶
type AddressStats struct {
Address string `json:"address"`
TransactionCount int64 `json:"transaction_count"`
TokenTransferCount int64 `json:"token_transfer_count"`
InternalTxCount int64 `json:"internal_tx_count"`
GasUsed int64 `json:"gas_used"`
Balance string `json:"balance"`
}
AddressStats contains address-specific statistics
type BlockTimeStats ¶
type BlockTimeStats struct {
AverageBlockTime time.Duration `json:"average_block_time"`
MinBlockTime time.Duration `json:"min_block_time"`
MaxBlockTime time.Duration `json:"max_block_time"`
BlocksAnalyzed int `json:"blocks_analyzed"`
UpdatedAt time.Time `json:"updated_at"`
}
BlockTimeStats contains block time statistics
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache provides in-memory caching for statistics
type Config ¶
type Config struct {
CacheTTL time.Duration
GasPriceBlocks int // Number of blocks to analyze for gas prices
SlowPercentile float64 // Percentile for slow gas price
AveragePercentile float64 // Percentile for average gas price
FastPercentile float64 // Percentile for fast gas price
SimpleTransactionGas int64 // Gas for simple ETH transfer
CounterUpdateInterval time.Duration
BlockTimeBlocks int // Number of blocks for block time calculation
}
Config holds statistics service configuration
type Counter ¶
type Counter struct {
Name string `json:"name"`
Value int64 `json:"value"`
UpdatedAt time.Time `json:"updated_at"`
}
Counter represents an aggregate counter value
type Counters ¶
type Counters struct {
TotalBlocks int64 `json:"total_blocks"`
TotalTransactions int64 `json:"total_transactions"`
TotalAddresses int64 `json:"total_addresses"`
TotalContracts int64 `json:"total_contracts"`
TotalTokens int64 `json:"total_tokens"`
TotalTokenTransfers int64 `json:"total_token_transfers"`
VerifiedContracts int64 `json:"verified_contracts"`
AverageBlockTime float64 `json:"average_block_time_seconds"`
TPS24h float64 `json:"tps_24h"`
GasUsed24h int64 `json:"gas_used_24h"`
UpdatedAt time.Time `json:"updated_at"`
}
Counters holds all aggregate statistics
type DailyStats ¶
DailyStats represents daily count statistics
type GasPrice ¶
type GasPrice struct {
Price float64 `json:"price"` // Gas price in Gwei
PriorityFee float64 `json:"priority_fee"` // Priority fee in Gwei
BaseFee float64 `json:"base_fee"` // Base fee in Gwei
Time float64 `json:"time"` // Estimated confirmation time in ms
FiatPrice float64 `json:"fiat_price"` // Fiat price for simple tx
Wei string `json:"wei"` // Full price in Wei
}
GasPrice represents gas price for a fee tier
type GasPrices ¶
type GasPrices struct {
Slow *GasPrice `json:"slow"`
Average *GasPrice `json:"average"`
Fast *GasPrice `json:"fast"`
UpdatedAt time.Time `json:"updated_at"`
BaseFee string `json:"base_fee"` // Current base fee
GasUsedRatio float64 `json:"gas_used_ratio"` // Recent block gas usage
}
GasPrices represents slow/average/fast gas price estimates
type HourlyStats ¶
HourlyStats represents hourly statistics
type MetricDefinition ¶
MetricDefinition describes a Prometheus metric
func GetMetricDefinitions ¶
func GetMetricDefinitions() []MetricDefinition
GetMetricDefinitions returns all metric definitions
type MetricsExporter ¶
type MetricsExporter struct {
// contains filtered or unexported fields
}
MetricsExporter exports statistics as Prometheus metrics
func NewMetricsExporter ¶
func NewMetricsExporter(service *Service) *MetricsExporter
NewMetricsExporter creates a new Prometheus metrics exporter
func (*MetricsExporter) ServeHTTP ¶
func (m *MetricsExporter) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler for Prometheus metrics endpoint
func (*MetricsExporter) StartBackgroundUpdater ¶
func (m *MetricsExporter) StartBackgroundUpdater(ctx context.Context, interval time.Duration)
StartBackgroundUpdater starts a goroutine to periodically update metrics
func (*MetricsExporter) UpdateMetrics ¶
func (m *MetricsExporter) UpdateMetrics(ctx context.Context) error
UpdateMetrics refreshes all metric values
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service provides statistics and caching functionality
func NewService ¶
NewService creates a new statistics service. Defaults to "cchain" prefix when chainSlug is omitted.
func (*Service) GetAddressStats ¶
GetAddressStats returns statistics for a specific address
func (*Service) GetAverageBlockTime ¶
func (s *Service) GetAverageBlockTime(ctx context.Context) (*BlockTimeStats, error)
GetAverageBlockTime returns block time statistics
func (*Service) GetCounters ¶
GetCounters returns aggregate statistics
func (*Service) GetDailyTransactionStats ¶
GetDailyTransactionStats returns daily transaction counts
func (*Service) GetGasPrices ¶
GetGasPrices returns current gas price estimates
func (*Service) GetHourlyGasUsage ¶
func (s *Service) GetHourlyGasUsage(ctx context.Context) ([]HourlyStats, error)
GetHourlyGasUsage returns hourly gas usage for the last 24 hours
func (*Service) GetTokenStats ¶
GetTokenStats returns statistics for a specific token
func (*Service) InvalidateCache ¶
func (s *Service) InvalidateCache()
InvalidateCache clears all cached data
func (*Service) StartBackgroundUpdater ¶
StartBackgroundUpdater starts a background goroutine to update stats periodically