-
Notifications
You must be signed in to change notification settings - Fork 0
/
market_pairs.go
47 lines (40 loc) · 1.94 KB
/
market_pairs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package metrics
import (
"fmt"
"sync"
"github.com/jinxprotocol/v4-chain/protocol/daemons/pricefeed/client/types"
)
var (
// marketToPair maps live marketIds to marketPair strings and is used for labelling metrics.
// This map is populated whenever markets are created or updated and access to this map is
// synchronized by the below mutex.
// The most correct approach here would be to get the marketPair from current chain state, so that
// we do not accidentally capture outdated or incorrect labels for market updates from rejected blocks.
// (A rejected block with a market creation should not present a problem as we will not log regular
// metrics for this market id unless it is later re-assigned - and re-updated.) However, these labels
// are used across the prices keeper, daemon, and daemon server code, so there is motive to avoid
// a solution that requires propagating references to centralized state. Furthermore, we judge that
// these market pairs are very unlikely to be updated, so this solution, while not perfect, is
// acceptable for the use case of logging/metrics in order to manage code complexity.
marketToPair = map[types.MarketId]string{}
// lock synchronizes access to the marketToPair map.
lock sync.Mutex
)
// SetMarketPairForTelemetry sets a market pair to an in-memory map of marketId to marketPair strings used
// for labelling metrics. This method is synchronized.
func SetMarketPairForTelemetry(marketId types.MarketId, marketPair string) {
lock.Lock()
defer lock.Unlock()
marketToPair[marketId] = marketPair
}
// GetMarketPairForTelemetry returns the market pair string for a given marketId. If the marketId is not
// found in the map, returns the INVALID string. This method is synchronized.
func GetMarketPairForTelemetry(marketId types.MarketId) string {
lock.Lock()
defer lock.Unlock()
marketPair, exists := marketToPair[marketId]
if !exists {
return fmt.Sprintf("invalid_id:%v", marketId)
}
return marketPair
}