| 115 | |
| 116 | /** @internal */ |
| 117 | export const fromMetricKey = <Type extends MetricKeyType.MetricKeyType<any, any>>( |
| 118 | key: MetricKey.MetricKey<Type> |
| 119 | ): Metric.Metric< |
| 120 | Type, |
| 121 | MetricKeyType.MetricKeyType.InType<Type>, |
| 122 | MetricKeyType.MetricKeyType.OutType<Type> |
| 123 | > => { |
| 124 | let untaggedHook: |
| 125 | | MetricHook.MetricHook< |
| 126 | MetricKeyType.MetricKeyType.InType<Type>, |
| 127 | MetricKeyType.MetricKeyType.OutType<Type> |
| 128 | > |
| 129 | | undefined |
| 130 | const hookCache = new WeakMap<ReadonlyArray<MetricLabel.MetricLabel>, MetricHook.MetricHook<any, any>>() |
| 131 | |
| 132 | const hook = (extraTags: ReadonlyArray<MetricLabel.MetricLabel>): MetricHook.MetricHook< |
| 133 | MetricKeyType.MetricKeyType.InType<Type>, |
| 134 | MetricKeyType.MetricKeyType.OutType<Type> |
| 135 | > => { |
| 136 | if (extraTags.length === 0) { |
| 137 | if (untaggedHook !== undefined) { |
| 138 | return untaggedHook |
| 139 | } |
| 140 | untaggedHook = globalMetricRegistry.get(key) |
| 141 | return untaggedHook |
| 142 | } |
| 143 | |
| 144 | let hook = hookCache.get(extraTags) |
| 145 | if (hook !== undefined) { |
| 146 | return hook |
| 147 | } |
| 148 | hook = globalMetricRegistry.get(metricKey.taggedWithLabels(key, extraTags)) |
| 149 | hookCache.set(extraTags, hook) |
| 150 | return hook |
| 151 | } |
| 152 | |
| 153 | return make( |
| 154 | key.keyType, |
| 155 | (input, extraTags) => hook(extraTags).update(input), |
| 156 | (extraTags) => hook(extraTags).get(), |
| 157 | (input, extraTags) => hook(extraTags).modify(input) |
| 158 | ) |
| 159 | } |
| 160 | |
| 161 | /** @internal */ |
| 162 | export const gauge: { |