2020-10-31 20:16:55 +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-10-31 20:16:55 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-10-31 20:16:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// config contains configuration for Resource creation.
|
|
|
|
type config struct {
|
|
|
|
// detectors that will be evaluated.
|
|
|
|
detectors []Detector
|
|
|
|
}
|
|
|
|
|
|
|
|
// Option is the interface that applies a configuration option.
|
|
|
|
type Option interface {
|
|
|
|
// Apply sets the Option value of a config.
|
|
|
|
Apply(*config)
|
2021-02-24 20:03:35 +02:00
|
|
|
|
|
|
|
// A private method to prevent users implementing the
|
|
|
|
// interface and so future additions to it will not
|
|
|
|
// violate compatibility.
|
|
|
|
private()
|
2020-10-31 20:16:55 +02:00
|
|
|
}
|
|
|
|
|
2021-02-24 20:03:35 +02:00
|
|
|
type option struct{}
|
|
|
|
|
|
|
|
func (option) private() {}
|
|
|
|
|
2020-10-31 20:16:55 +02:00
|
|
|
// WithAttributes adds attributes to the configured Resource.
|
2021-02-18 19:59:37 +02:00
|
|
|
func WithAttributes(attributes ...attribute.KeyValue) Option {
|
2020-10-31 20:16:55 +02:00
|
|
|
return WithDetectors(detectAttributes{attributes})
|
|
|
|
}
|
|
|
|
|
|
|
|
type detectAttributes struct {
|
2021-02-18 19:59:37 +02:00
|
|
|
attributes []attribute.KeyValue
|
2020-10-31 20:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d detectAttributes) Detect(context.Context) (*Resource, error) {
|
|
|
|
return NewWithAttributes(d.attributes...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDetectors adds detectors to be evaluated for the configured resource.
|
|
|
|
func WithDetectors(detectors ...Detector) Option {
|
2021-02-24 20:03:35 +02:00
|
|
|
return detectorsOption{detectors: detectors}
|
2020-10-31 20:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type detectorsOption struct {
|
2021-02-24 20:03:35 +02:00
|
|
|
option
|
2020-10-31 20:16:55 +02:00
|
|
|
detectors []Detector
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply implements Option.
|
|
|
|
func (o detectorsOption) Apply(cfg *config) {
|
|
|
|
cfg.detectors = append(cfg.detectors, o.detectors...)
|
|
|
|
}
|
|
|
|
|
2021-04-29 20:12:48 +02:00
|
|
|
// WithBuiltinDetectors adds the built detectors to the configured resource.
|
|
|
|
func WithBuiltinDetectors() Option {
|
|
|
|
return WithDetectors(telemetrySDK{},
|
|
|
|
host{},
|
|
|
|
fromEnv{})
|
2020-10-31 20:16:55 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 20:12:48 +02:00
|
|
|
// WithFromEnv adds attributes from environment variables to the configured resource.
|
|
|
|
func WithFromEnv() Option {
|
|
|
|
return WithDetectors(fromEnv{})
|
2020-10-31 20:16:55 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 20:12:48 +02:00
|
|
|
// WithHost adds attributes from the host to the configured resource.
|
|
|
|
func WithHost() Option {
|
|
|
|
return WithDetectors(host{})
|
2020-10-31 20:16:55 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 20:12:48 +02:00
|
|
|
// WithTelemetrySDK adds TelemetrySDK version info to the configured resource.
|
|
|
|
func WithTelemetrySDK() Option {
|
|
|
|
return WithDetectors(telemetrySDK{})
|
2020-10-31 20:16:55 +02:00
|
|
|
}
|