auto.go (3120B)
1 // Copyright The OpenTelemetry Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package resource // import "go.opentelemetry.io/otel/sdk/resource" 16 17 import ( 18 "context" 19 "errors" 20 "fmt" 21 "strings" 22 ) 23 24 var ( 25 // ErrPartialResource is returned by a detector when complete source 26 // information for a Resource is unavailable or the source information 27 // contains invalid values that are omitted from the returned Resource. 28 ErrPartialResource = errors.New("partial resource") 29 ) 30 31 // Detector detects OpenTelemetry resource information. 32 type Detector interface { 33 // DO NOT CHANGE: any modification will not be backwards compatible and 34 // must never be done outside of a new major release. 35 36 // Detect returns an initialized Resource based on gathered information. 37 // If the source information to construct a Resource contains invalid 38 // values, a Resource is returned with the valid parts of the source 39 // information used for initialization along with an appropriately 40 // wrapped ErrPartialResource error. 41 Detect(ctx context.Context) (*Resource, error) 42 // DO NOT CHANGE: any modification will not be backwards compatible and 43 // must never be done outside of a new major release. 44 } 45 46 // Detect calls all input detectors sequentially and merges each result with the previous one. 47 // It returns the merged error too. 48 func Detect(ctx context.Context, detectors ...Detector) (*Resource, error) { 49 r := new(Resource) 50 return r, detect(ctx, r, detectors) 51 } 52 53 // detect runs all detectors using ctx and merges the result into res. This 54 // assumes res is allocated and not nil, it will panic otherwise. 55 func detect(ctx context.Context, res *Resource, detectors []Detector) error { 56 var ( 57 r *Resource 58 errs detectErrs 59 err error 60 ) 61 62 for _, detector := range detectors { 63 if detector == nil { 64 continue 65 } 66 r, err = detector.Detect(ctx) 67 if err != nil { 68 errs = append(errs, err) 69 if !errors.Is(err, ErrPartialResource) { 70 continue 71 } 72 } 73 r, err = Merge(res, r) 74 if err != nil { 75 errs = append(errs, err) 76 } 77 *res = *r 78 } 79 80 if len(errs) == 0 { 81 return nil 82 } 83 return errs 84 } 85 86 type detectErrs []error 87 88 func (e detectErrs) Error() string { 89 errStr := make([]string, len(e)) 90 for i, err := range e { 91 errStr[i] = fmt.Sprintf("* %s", err) 92 } 93 94 format := "%d errors occurred detecting resource:\n\t%s" 95 return fmt.Sprintf(format, len(e), strings.Join(errStr, "\n\t")) 96 } 97 98 func (e detectErrs) Unwrap() error { 99 switch len(e) { 100 case 0: 101 return nil 102 case 1: 103 return e[0] 104 } 105 return e[1:] 106 } 107 108 func (e detectErrs) Is(target error) bool { 109 return len(e) != 0 && errors.Is(e[0], target) 110 }