2020-07-20 17:43:51 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-11-04 19:10:58 +02:00
|
|
|
package resource // import "go.opentelemetry.io/otel/sdk/resource"
|
2020-07-20 17:43:51 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-03-09 01:15:27 +02:00
|
|
|
"strings"
|
2020-07-20 17:43:51 +02:00
|
|
|
)
|
|
|
|
|
2023-10-16 19:02:21 +02:00
|
|
|
// ErrPartialResource is returned by a detector when complete source
|
|
|
|
// information for a Resource is unavailable or the source information
|
|
|
|
// contains invalid values that are omitted from the returned Resource.
|
|
|
|
var ErrPartialResource = errors.New("partial resource")
|
2020-07-20 17:43:51 +02:00
|
|
|
|
2022-04-25 22:22:49 +02:00
|
|
|
// Detector detects OpenTelemetry resource information.
|
2020-07-20 17:43:51 +02:00
|
|
|
type Detector interface {
|
2021-06-18 16:56:11 +02:00
|
|
|
// DO NOT CHANGE: any modification will not be backwards compatible and
|
|
|
|
// must never be done outside of a new major release.
|
|
|
|
|
2020-07-20 17:43:51 +02:00
|
|
|
// Detect returns an initialized Resource based on gathered information.
|
2020-09-14 18:08:05 +02:00
|
|
|
// If the source information to construct a Resource contains invalid
|
|
|
|
// values, a Resource is returned with the valid parts of the source
|
|
|
|
// information used for initialization along with an appropriately
|
|
|
|
// wrapped ErrPartialResource error.
|
2020-07-20 17:43:51 +02:00
|
|
|
Detect(ctx context.Context) (*Resource, error)
|
2021-06-18 16:56:11 +02:00
|
|
|
// DO NOT CHANGE: any modification will not be backwards compatible and
|
|
|
|
// must never be done outside of a new major release.
|
2020-07-20 17:43:51 +02:00
|
|
|
}
|
|
|
|
|
2024-02-05 17:33:29 +02:00
|
|
|
// Detect returns a new [Resource] merged from all the Resources each of the
|
|
|
|
// detectors produces. Each of the detectors are called sequentially, in the
|
|
|
|
// order they are passed, merging the produced resource into the previous.
|
|
|
|
//
|
|
|
|
// This may return a partial Resource along with an error containing
|
|
|
|
// [ErrPartialResource] if that error is returned from a detector. It may also
|
|
|
|
// return a merge-conflicting Resource along with an error containing
|
|
|
|
// [ErrSchemaURLConflict] if merging Resources from different detectors results
|
|
|
|
// in a schema URL conflict. It is up to the caller to determine if this
|
|
|
|
// returned Resource should be used or not.
|
|
|
|
//
|
|
|
|
// If one of the detectors returns an error that is not [ErrPartialResource],
|
|
|
|
// the resource produced by the detector will not be merged and the returned
|
|
|
|
// error will wrap that detector's error.
|
2020-07-20 17:43:51 +02:00
|
|
|
func Detect(ctx context.Context, detectors ...Detector) (*Resource, error) {
|
2023-03-09 01:15:27 +02:00
|
|
|
r := new(Resource)
|
|
|
|
return r, detect(ctx, r, detectors)
|
|
|
|
}
|
|
|
|
|
|
|
|
// detect runs all detectors using ctx and merges the result into res. This
|
|
|
|
// assumes res is allocated and not nil, it will panic otherwise.
|
2024-02-05 17:33:29 +02:00
|
|
|
//
|
|
|
|
// If the detectors or merging resources produces any errors (i.e.
|
|
|
|
// [ErrPartialResource] [ErrSchemaURLConflict]), a single error wrapping all of
|
|
|
|
// these errors will be returned. Otherwise, nil is returned.
|
2023-03-09 01:15:27 +02:00
|
|
|
func detect(ctx context.Context, res *Resource, detectors []Detector) error {
|
|
|
|
var (
|
|
|
|
r *Resource
|
|
|
|
errs detectErrs
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-07-20 17:43:51 +02:00
|
|
|
for _, detector := range detectors {
|
2020-10-31 20:16:55 +02:00
|
|
|
if detector == nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-09 01:15:27 +02:00
|
|
|
r, err = detector.Detect(ctx)
|
2020-07-20 17:43:51 +02:00
|
|
|
if err != nil {
|
2023-03-09 01:15:27 +02:00
|
|
|
errs = append(errs, err)
|
2020-07-20 17:43:51 +02:00
|
|
|
if !errors.Is(err, ErrPartialResource) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2023-03-09 01:15:27 +02:00
|
|
|
r, err = Merge(res, r)
|
2021-06-08 18:46:42 +02:00
|
|
|
if err != nil {
|
2023-03-09 01:15:27 +02:00
|
|
|
errs = append(errs, err)
|
2021-06-08 18:46:42 +02:00
|
|
|
}
|
2023-03-09 01:15:27 +02:00
|
|
|
*res = *r
|
2020-07-20 17:43:51 +02:00
|
|
|
}
|
|
|
|
|
2023-03-09 01:15:27 +02:00
|
|
|
if len(errs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-05 17:33:29 +02:00
|
|
|
if errors.Is(errs, ErrSchemaURLConflict) {
|
|
|
|
// If there has been a merge conflict, ensure the resource has no
|
|
|
|
// schema URL.
|
|
|
|
res.schemaURL = ""
|
|
|
|
}
|
2023-03-09 01:15:27 +02:00
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
type detectErrs []error
|
|
|
|
|
|
|
|
func (e detectErrs) Error() string {
|
|
|
|
errStr := make([]string, len(e))
|
|
|
|
for i, err := range e {
|
|
|
|
errStr[i] = fmt.Sprintf("* %s", err)
|
2020-07-20 17:43:51 +02:00
|
|
|
}
|
2023-03-09 01:15:27 +02:00
|
|
|
|
|
|
|
format := "%d errors occurred detecting resource:\n\t%s"
|
|
|
|
return fmt.Sprintf(format, len(e), strings.Join(errStr, "\n\t"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e detectErrs) Unwrap() error {
|
|
|
|
switch len(e) {
|
|
|
|
case 0:
|
|
|
|
return nil
|
|
|
|
case 1:
|
|
|
|
return e[0]
|
|
|
|
}
|
|
|
|
return e[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e detectErrs) Is(target error) bool {
|
|
|
|
return len(e) != 0 && errors.Is(e[0], target)
|
2020-07-20 17:43:51 +02:00
|
|
|
}
|