go.temporal.io/server@v1.23.0/common/metrics/otel_options.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package metrics 26 27 import ( 28 "go.opentelemetry.io/otel/metric" 29 ) 30 31 type ( 32 // optionSet represents a slice of metric options. We need it to be able to add options of the 33 // [metric.InstrumentOption] type to slices which may be of any other type implemented by metric.InstrumentOption. 34 // Normally, you could do something like `T metric.InstrumentOption` here, but the type dependency here is reversed. 35 // We need a generic type T that is implemented *by* metric.InstrumentOption, not the other way around. 36 // This is the only solution which avoids duplicating all the logic of the addOptions function without relying on 37 // reflection, an error-prone type assertion, or a type switch with a runtime error for unhandled cases. 38 optionSet[T any] interface { 39 addOption(option metric.InstrumentOption) T 40 } 41 counterOptions []metric.Int64CounterOption 42 gaugeOptions []metric.Float64ObservableGaugeOption 43 histogramOptions []metric.Int64HistogramOption 44 ) 45 46 func addOptions[T optionSet[T]](omp *otelMetricsHandler, opts T, metricName string) T { 47 metricDef, ok := omp.catalog.getMetric(metricName) 48 if !ok { 49 return opts 50 } 51 52 if description := metricDef.description; description != "" { 53 opts = opts.addOption(metric.WithDescription(description)) 54 } 55 56 if unit := metricDef.unit; unit != "" { 57 opts = opts.addOption(metric.WithUnit(string(unit))) 58 } 59 60 return opts 61 } 62 63 func (opts counterOptions) addOption(option metric.InstrumentOption) counterOptions { 64 return append(opts, option) 65 } 66 67 func (opts gaugeOptions) addOption(option metric.InstrumentOption) gaugeOptions { 68 return append(opts, option) 69 } 70 71 func (opts histogramOptions) addOption(option metric.InstrumentOption) histogramOptions { 72 return append(opts, option) 73 }