1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-10 00:29:12 +02:00
opentelemetry-go/sdk/resource/auto.go
YANYZP 99c299877d
OT resource detector (#939)
* first

* all included

* constant

* res keys

* env detector

* Update sdk/detect/env.go

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>

* new test cases and strings operation to handle labels

* solved conflict

* typo

* corrected comments

* deleted env var handling

* push again

* rerun

* restructuring

* new way to aggregate errors

* all in resources package and verbose comments

* solved merge conflicts

* included new error types

* improved error handling

* updated changelog.md

* updated changelog

* updated changelog.md

* updated changelog

* Update CHANGELOG.md

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
Co-authored-by: Liz Fong-Jones <lizf@honeycomb.io>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
2020-07-20 08:43:51 -07:00

68 lines
2.3 KiB
Go

// 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.
package resource
import (
"context"
"errors"
"fmt"
)
var (
// ErrMissingResource is returned by a detector when source information
// is unavailable for a Resource.
ErrMissingResource = errors.New("missing resource")
// 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.
ErrPartialResource = errors.New("partial resource")
)
// Detector detects OpenTelemetry resource information
type Detector interface {
// Detect returns an initialized Resource based on gathered information.
// If source information to construct a Resource is inaccessible, an
// uninitialized Resource is returned with an appropriately wrapped
// ErrMissingResource error is returned. 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.
Detect(ctx context.Context) (*Resource, error)
}
// Detect calls all input detectors sequentially and merges each result with the previous one.
// It returns the merged error too.
func Detect(ctx context.Context, detectors ...Detector) (*Resource, error) {
var autoDetectedRes *Resource
var errInfo []string
for _, detector := range detectors {
res, err := detector.Detect(ctx)
if err != nil {
errInfo = append(errInfo, err.Error())
if !errors.Is(err, ErrPartialResource) {
continue
}
}
autoDetectedRes = Merge(autoDetectedRes, res)
}
var aggregatedError error
if len(errInfo) > 0 {
aggregatedError = fmt.Errorf("detecting resources: %s", errInfo)
}
return autoDetectedRes, aggregatedError
}