| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package metrix |
| 4 | |
| 5 | type InstrumentOption interface { |
| 6 | apply(*instrumentConfig) |
| 7 | } |
| 8 | |
| 9 | type optionFunc func(*instrumentConfig) |
| 10 | |
| 11 | func (f optionFunc) apply(cfg *instrumentConfig) { f(cfg) } |
| 12 | |
| 13 | type instrumentConfig struct { |
| 14 | freshnessSet bool |
| 15 | freshness FreshnessPolicy |
| 16 | |
| 17 | windowSet bool |
| 18 | window MetricWindow |
| 19 | |
| 20 | histogramBounds []float64 |
| 21 | summaryQuantile []float64 |
| 22 | summaryReservoirSet bool |
| 23 | summaryReservoir int |
| 24 | states []string |
| 25 | stateSetMode *StateSetMode |
| 26 | measureSetFields []MeasureFieldSpec |
| 27 | measureSetSemantics *MeasureSetSemantics |
| 28 | |
| 29 | descriptionSet bool |
| 30 | description string |
| 31 | chartFamilySet bool |
| 32 | chartFamily string |
| 33 | chartPrioritySet bool |
| 34 | chartPriority int |
| 35 | unitSet bool |
| 36 | unit string |
| 37 | floatSet bool |
| 38 | float bool |
| 39 | } |
| 40 | |
| 41 | func WithFreshness(policy FreshnessPolicy) InstrumentOption { |
| 42 | return optionFunc(func(cfg *instrumentConfig) { |
| 43 | cfg.freshnessSet = true |
| 44 | cfg.freshness = policy |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | func WithWindow(w MetricWindow) InstrumentOption { |
| 49 | return optionFunc(func(cfg *instrumentConfig) { |
| 50 | cfg.windowSet = true |
| 51 | cfg.window = w |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | func WithHistogramBounds(bounds ...float64) InstrumentOption { |
| 56 | return optionFunc(func(cfg *instrumentConfig) { |
| 57 | cfg.histogramBounds = append([]float64(nil), bounds...) |
| 58 | }) |
| 59 | } |
| 60 | |
| 61 | func WithSummaryQuantiles(qs ...float64) InstrumentOption { |
| 62 | return optionFunc(func(cfg *instrumentConfig) { |
| 63 | cfg.summaryQuantile = append([]float64(nil), qs...) |
| 64 | }) |
| 65 | } |
| 66 | |
| 67 | // WithSummaryReservoirSize sets the bounded sample size used for stateful summary quantile estimation. |
| 68 | // Valid only for stateful summaries. |
| 69 | func WithSummaryReservoirSize(size int) InstrumentOption { |
| 70 | return optionFunc(func(cfg *instrumentConfig) { |
| 71 | cfg.summaryReservoirSet = true |
| 72 | cfg.summaryReservoir = size |
| 73 | }) |
| 74 | } |
| 75 | |
| 76 | func WithStateSetStates(states ...string) InstrumentOption { |
| 77 | return optionFunc(func(cfg *instrumentConfig) { |
| 78 | cfg.states = append([]string(nil), states...) |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | func WithStateSetMode(mode StateSetMode) InstrumentOption { |
| 83 | return optionFunc(func(cfg *instrumentConfig) { |
| 84 | m := mode |
| 85 | cfg.stateSetMode = &m |
| 86 | }) |
| 87 | } |
| 88 | |
| 89 | func WithMeasureSetFields(fields ...MeasureFieldSpec) InstrumentOption { |
| 90 | return optionFunc(func(cfg *instrumentConfig) { |
| 91 | cfg.measureSetFields = append([]MeasureFieldSpec(nil), fields...) |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | func withMeasureSetSemantics(semantics MeasureSetSemantics) InstrumentOption { |
| 96 | return optionFunc(func(cfg *instrumentConfig) { |
| 97 | s := semantics |
| 98 | cfg.measureSetSemantics = &s |
| 99 | }) |
| 100 | } |
| 101 | |
| 102 | // WithDescription sets optional metric-family description metadata. |
| 103 | func WithDescription(description string) InstrumentOption { |
| 104 | return optionFunc(func(cfg *instrumentConfig) { |
| 105 | cfg.descriptionSet = true |
| 106 | cfg.description = description |
| 107 | }) |
| 108 | } |
| 109 | |
| 110 | // WithChartFamily sets optional metric-family chart grouping metadata. |
| 111 | func WithChartFamily(chartFamily string) InstrumentOption { |
| 112 | return optionFunc(func(cfg *instrumentConfig) { |
| 113 | cfg.chartFamilySet = true |
| 114 | cfg.chartFamily = chartFamily |
| 115 | }) |
| 116 | } |
| 117 | |
| 118 | // WithChartPriority sets optional metric-family chart priority metadata. |
| 119 | // It is currently consumed only by chartengine autogen. |
| 120 | // TODO: Revisit whether chart-template charts should also honor metrix priority hints. |
| 121 | func WithChartPriority(chartPriority int) InstrumentOption { |
| 122 | return optionFunc(func(cfg *instrumentConfig) { |
| 123 | cfg.chartPrioritySet = true |
| 124 | cfg.chartPriority = chartPriority |
| 125 | }) |
| 126 | } |
| 127 | |
| 128 | // WithUnit sets optional metric-family unit metadata. |
| 129 | func WithUnit(unit string) InstrumentOption { |
| 130 | return optionFunc(func(cfg *instrumentConfig) { |
| 131 | cfg.unitSet = true |
| 132 | cfg.unit = unit |
| 133 | }) |
| 134 | } |
| 135 | |
| 136 | // WithFloat sets optional metric-family float-dimension metadata hint. |
| 137 | func WithFloat(isFloat bool) InstrumentOption { |
| 138 | return optionFunc(func(cfg *instrumentConfig) { |
| 139 | cfg.floatSet = true |
| 140 | cfg.float = isFloat |
| 141 | }) |
| 142 | } |