| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package metric // import "go.opentelemetry.io/otel/sdk/metric" |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"regexp" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/internal/global" |
| 12 |
) |
| 13 |
|
| 14 |
var ( |
| 15 |
errMultiInst = errors.New("name replacement for multiple instruments") |
| 16 |
errEmptyView = errors.New("no criteria provided for view") |
| 17 |
|
| 18 |
emptyView = func(Instrument) (Stream, bool) { return Stream{}, false } |
| 19 |
) |
| 20 |
|
| 21 |
// View is an override to the default behavior of the SDK. It defines how data |
| 22 |
// should be collected for certain instruments. It returns true and the exact |
| 23 |
// Stream to use for matching Instruments. Otherwise, if the view does not |
| 24 |
// match, false is returned. |
| 25 |
type View func(Instrument) (Stream, bool) |
| 26 |
|
| 27 |
// NewView returns a View that applies the Stream mask for all instruments that |
| 28 |
// match criteria. The returned View will only apply mask if all non-zero-value |
| 29 |
// fields of criteria match the corresponding Instrument passed to the view. If |
| 30 |
// no criteria are provided, all field of criteria are their zero-values, a |
| 31 |
// view that matches no instruments is returned. If you need to match a |
| 32 |
// zero-value field, create a View directly. |
| 33 |
// |
| 34 |
// The Name field of criteria supports wildcard pattern matching. The "*" |
| 35 |
// wildcard is recognized as matching zero or more characters, and "?" is |
| 36 |
// recognized as matching exactly one character. For example, a pattern of "*" |
| 37 |
// matches all instrument names. |
| 38 |
// |
| 39 |
// The Stream mask only applies updates for non-zero-value fields. By default, |
| 40 |
// the Instrument the View matches against will be use for the Name, |
| 41 |
// Description, and Unit of the returned Stream and no Aggregation or |
| 42 |
// AttributeFilter are set. All non-zero-value fields of mask are used instead |
| 43 |
// of the default. If you need to zero out an Stream field returned from a |
| 44 |
// View, create a View directly. |
| 45 |
func NewView(criteria Instrument, mask Stream) View { |
| 46 |
if criteria.IsEmpty() { |
| 47 |
global.Error( |
| 48 |
errEmptyView, "dropping view", |
| 49 |
"mask", mask, |
| 50 |
) |
| 51 |
return emptyView |
| 52 |
} |
| 53 |
|
| 54 |
var matchFunc func(Instrument) bool |
| 55 |
if strings.ContainsAny(criteria.Name, "*?") { |
| 56 |
if mask.Name != "" { |
| 57 |
global.Error( |
| 58 |
errMultiInst, "dropping view", |
| 59 |
"criteria", criteria, |
| 60 |
"mask", mask, |
| 61 |
) |
| 62 |
return emptyView |
| 63 |
} |
| 64 |
|
| 65 |
// Handle branching here in NewView instead of criteria.matches so |
| 66 |
// criteria.matches remains inlinable for the simple case. |
| 67 |
pattern := regexp.QuoteMeta(criteria.Name) |
| 68 |
pattern = "^" + pattern + "$" |
| 69 |
pattern = strings.ReplaceAll(pattern, `\?`, ".") |
| 70 |
pattern = strings.ReplaceAll(pattern, `\*`, ".*") |
| 71 |
re := regexp.MustCompile(pattern) |
| 72 |
matchFunc = func(i Instrument) bool { |
| 73 |
return re.MatchString(i.Name) && |
| 74 |
criteria.matchesDescription(i) && |
| 75 |
criteria.matchesKind(i) && |
| 76 |
criteria.matchesUnit(i) && |
| 77 |
criteria.matchesScope(i) |
| 78 |
} |
| 79 |
} else { |
| 80 |
matchFunc = criteria.matches |
| 81 |
} |
| 82 |
|
| 83 |
var agg Aggregation |
| 84 |
if mask.Aggregation != nil { |
| 85 |
agg = mask.Aggregation.copy() |
| 86 |
if err := agg.err(); err != nil { |
| 87 |
global.Error( |
| 88 |
err, "not using aggregation with view", |
| 89 |
"criteria", criteria, |
| 90 |
"mask", mask, |
| 91 |
) |
| 92 |
agg = nil |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return func(i Instrument) (Stream, bool) { |
| 97 |
if matchFunc(i) { |
| 98 |
return Stream{ |
| 99 |
Name: nonZero(mask.Name, i.Name), |
| 100 |
Description: nonZero(mask.Description, i.Description), |
| 101 |
Unit: nonZero(mask.Unit, i.Unit), |
| 102 |
Aggregation: agg, |
| 103 |
AttributeFilter: mask.AttributeFilter, |
| 104 |
ExemplarReservoirProviderSelector: mask.ExemplarReservoirProviderSelector, |
| 105 |
}, true |
| 106 |
} |
| 107 |
return Stream{}, false |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// nonZero returns v if it is non-zero-valued, otherwise alt. |
| 112 |
func nonZero[T comparable](v, alt T) T { |
| 113 |
var zero T |
| 114 |
if v != zero { |
| 115 |
return v |
| 116 |
} |
| 117 |
return alt |
| 118 |
} |