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 // ErrPartialResource is returned by a detector when complete source 25 // information for a Resource is unavailable or the source information 26 // contains invalid values that are omitted from the returned Resource. 27 var ErrPartialResource = errors.New("partial resource") 28 29 // Detector detects OpenTelemetry resource information. 30 type Detector interface { 31 // DO NOT CHANGE: any modification will not be backwards compatible and 32 // must never be done outside of a new major release. 33 34 // Detect returns an initialized Resource based on gathered information. 35 // If the source information to construct a Resource contains invalid 36 // values, a Resource is returned with the valid parts of the source 37 // information used for initialization along with an appropriately 38 // wrapped ErrPartialResource error. 39 Detect(ctx context.Context) (*Resource, error) 40 // DO NOT CHANGE: any modification will not be backwards compatible and 41 // must never be done outside of a new major release. 42 } 43 44 // Detect calls all input detectors sequentially and merges each result with the previous one. 45 // It returns the merged error too. 46 func Detect(ctx context.Context, detectors ...Detector) (*Resource, error) { 47 r := new(Resource) 48 return r, detect(ctx, r, detectors) 49 } 50 51 // detect runs all detectors using ctx and merges the result into res. This 52 // assumes res is allocated and not nil, it will panic otherwise. 53 func detect(ctx context.Context, res *Resource, detectors []Detector) error { 54 var ( 55 r *Resource 56 errs detectErrs 57 err error 58 ) 59 60 for _, detector := range detectors { 61 if detector == nil { 62 continue 63 } 64 r, err = detector.Detect(ctx) 65 if err != nil { 66 errs = append(errs, err) 67 if !errors.Is(err, ErrPartialResource) { 68 continue 69 } 70 } 71 r, err = Merge(res, r) 72 if err != nil { 73 errs = append(errs, err) 74 } 75 *res = *r 76 } 77 78 if len(errs) == 0 { 79 return nil 80 } 81 return errs 82 } 83 84 type detectErrs []error 85 86 func (e detectErrs) Error() string { 87 errStr := make([]string, len(e)) 88 for i, err := range e { 89 errStr[i] = fmt.Sprintf("* %s", err) 90 } 91 92 format := "%d errors occurred detecting resource:\n\t%s" 93 return fmt.Sprintf(format, len(e), strings.Join(errStr, "\n\t")) 94 } 95 96 func (e detectErrs) Unwrap() error { 97 switch len(e) { 98 case 0: 99 return nil 100 case 1: 101 return e[0] 102 } 103 return e[1:] 104 } 105 106 func (e detectErrs) Is(target error) bool { 107 return len(e) != 0 && errors.Is(e[0], target) 108 } 109