|
3 | 3 | from opentelemetry.metrics import get_meter_provider, set_meter_provider |
4 | 4 | from opentelemetry.sdk.metrics import MeterProvider |
5 | 5 |
|
6 | | -from pusher.config import Config |
| 6 | +from pusher.config import Config, PriceConfig, PriceSourceConfig, ConstantSourceConfig, SingleSourceConfig, \ |
| 7 | + PairSourceConfig, OracleMidAverageConfig, PriceSource |
7 | 8 |
|
8 | 9 | METER_NAME = "hip3pusher" |
9 | 10 |
|
@@ -42,3 +43,41 @@ def _init_metrics(self): |
42 | 43 | description="Interval between push requests (seconds)", |
43 | 44 | unit="s", |
44 | 45 | ) |
| 46 | + # labels: dex, price_type, symbol |
| 47 | + self.price_config_counter = self.meter.create_counter( |
| 48 | + name="hip_3_relayer_price_config", |
| 49 | + description="Price source config", |
| 50 | + ) |
| 51 | + |
| 52 | + def set_price_configs(self, dex: str, price_config: PriceConfig): |
| 53 | + self._set_price_config_type(dex, price_config.oracle, "oracle") |
| 54 | + self._set_price_config_type(dex, price_config.mark, "mark") |
| 55 | + self._set_price_config_type(dex, price_config.external, "external") |
| 56 | + |
| 57 | + def _set_price_config_type(self, dex: str, price_source_config: dict[str, list[PriceSourceConfig]], price_type: str): |
| 58 | + for symbol in price_source_config: |
| 59 | + source_config_str = ' | '.join(self._get_source_config_str(source_config) for source_config in price_source_config[symbol]) |
| 60 | + labels = { |
| 61 | + "dex": dex, |
| 62 | + "symbol": symbol, |
| 63 | + "price_type": price_type, |
| 64 | + "config": source_config_str, |
| 65 | + } |
| 66 | + self.price_config_counter.add(1, labels) |
| 67 | + |
| 68 | + def _get_source_config_str(self, source_config: PriceSourceConfig): |
| 69 | + if isinstance(source_config, ConstantSourceConfig): |
| 70 | + return f"constant({source_config.value})" |
| 71 | + elif isinstance(source_config, SingleSourceConfig): |
| 72 | + return self._get_price_source_str(source_config.source) |
| 73 | + elif isinstance(source_config, PairSourceConfig): |
| 74 | + base_str = self._get_price_source_str(source_config.base_source) |
| 75 | + quote_str = self._get_price_source_str(source_config.quote_source) |
| 76 | + return f"pair({base_str},{quote_str})" |
| 77 | + elif isinstance(source_config, OracleMidAverageConfig): |
| 78 | + return f"oracle_mid_average({source_config.symbol})" |
| 79 | + else: |
| 80 | + return "unknown" |
| 81 | + |
| 82 | + def _get_price_source_str(self, price_source: PriceSource): |
| 83 | + return f"{price_source.source_name}({str(price_source.source_id)[:8]})" |
0 commit comments