2021-08-09 07:19:24 +00: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.
|
|
|
|
|
|
|
|
package resource // import "go.opentelemetry.io/otel/sdk/resource"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
)
|
|
|
|
|
|
|
|
// config contains configuration for Resource creation.
|
|
|
|
type config struct {
|
|
|
|
// detectors that will be evaluated.
|
|
|
|
detectors []Detector
|
2023-02-01 17:06:36 +00:00
|
|
|
// SchemaURL to associate with the Resource.
|
|
|
|
schemaURL string
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option is the interface that applies a configuration option.
|
|
|
|
type Option interface {
|
2023-02-01 17:06:36 +00:00
|
|
|
// apply sets the Option value of a config.
|
|
|
|
apply(config) config
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithAttributes adds attributes to the configured Resource.
|
|
|
|
func WithAttributes(attributes ...attribute.KeyValue) Option {
|
|
|
|
return WithDetectors(detectAttributes{attributes})
|
|
|
|
}
|
|
|
|
|
|
|
|
type detectAttributes struct {
|
|
|
|
attributes []attribute.KeyValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d detectAttributes) Detect(context.Context) (*Resource, error) {
|
2023-02-01 17:06:36 +00:00
|
|
|
return NewSchemaless(d.attributes...), nil
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithDetectors adds detectors to be evaluated for the configured resource.
|
|
|
|
func WithDetectors(detectors ...Detector) Option {
|
|
|
|
return detectorsOption{detectors: detectors}
|
|
|
|
}
|
|
|
|
|
|
|
|
type detectorsOption struct {
|
|
|
|
detectors []Detector
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
func (o detectorsOption) apply(cfg config) config {
|
2021-08-09 07:19:24 +00:00
|
|
|
cfg.detectors = append(cfg.detectors, o.detectors...)
|
2023-02-01 17:06:36 +00:00
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFromEnv adds attributes from environment variables to the configured resource.
|
|
|
|
func WithFromEnv() Option {
|
|
|
|
return WithDetectors(fromEnv{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithHost adds attributes from the host to the configured resource.
|
|
|
|
func WithHost() Option {
|
|
|
|
return WithDetectors(host{})
|
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
// WithHostID adds host ID information to the configured resource.
|
|
|
|
func WithHostID() Option {
|
|
|
|
return WithDetectors(hostIDDetector{})
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithTelemetrySDK adds TelemetrySDK version info to the configured resource.
|
|
|
|
func WithTelemetrySDK() Option {
|
|
|
|
return WithDetectors(telemetrySDK{})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithSchemaURL sets the schema URL for the configured resource.
|
|
|
|
func WithSchemaURL(schemaURL string) Option {
|
|
|
|
return schemaURLOption(schemaURL)
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
type schemaURLOption string
|
|
|
|
|
|
|
|
func (o schemaURLOption) apply(cfg config) config {
|
|
|
|
cfg.schemaURL = string(o)
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOS adds all the OS attributes to the configured Resource.
|
|
|
|
// See individual WithOS* functions to configure specific attributes.
|
|
|
|
func WithOS() Option {
|
|
|
|
return WithDetectors(
|
|
|
|
osTypeDetector{},
|
|
|
|
osDescriptionDetector{},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOSType adds an attribute with the operating system type to the configured Resource.
|
|
|
|
func WithOSType() Option {
|
|
|
|
return WithDetectors(osTypeDetector{})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithOSDescription adds an attribute with the operating system description to the
|
|
|
|
// configured Resource. The formatted string is equivalent to the output of the
|
|
|
|
// `uname -snrvm` command.
|
|
|
|
func WithOSDescription() Option {
|
|
|
|
return WithDetectors(osDescriptionDetector{})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithProcess adds all the Process attributes to the configured Resource.
|
|
|
|
//
|
|
|
|
// Warning! This option will include process command line arguments. If these
|
|
|
|
// contain sensitive information it will be included in the exported resource.
|
|
|
|
//
|
|
|
|
// This option is equivalent to calling WithProcessPID,
|
|
|
|
// WithProcessExecutableName, WithProcessExecutablePath,
|
|
|
|
// WithProcessCommandArgs, WithProcessOwner, WithProcessRuntimeName,
|
|
|
|
// WithProcessRuntimeVersion, and WithProcessRuntimeDescription. See each
|
|
|
|
// option function for information about what resource attributes each
|
|
|
|
// includes.
|
|
|
|
func WithProcess() Option {
|
|
|
|
return WithDetectors(
|
|
|
|
processPIDDetector{},
|
|
|
|
processExecutableNameDetector{},
|
|
|
|
processExecutablePathDetector{},
|
|
|
|
processCommandArgsDetector{},
|
|
|
|
processOwnerDetector{},
|
|
|
|
processRuntimeNameDetector{},
|
|
|
|
processRuntimeVersionDetector{},
|
|
|
|
processRuntimeDescriptionDetector{},
|
|
|
|
)
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithProcessPID adds an attribute with the process identifier (PID) to the
|
|
|
|
// configured Resource.
|
|
|
|
func WithProcessPID() Option {
|
|
|
|
return WithDetectors(processPIDDetector{})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithProcessExecutableName adds an attribute with the name of the process
|
|
|
|
// executable to the configured Resource.
|
|
|
|
func WithProcessExecutableName() Option {
|
|
|
|
return WithDetectors(processExecutableNameDetector{})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithProcessExecutablePath adds an attribute with the full path to the process
|
|
|
|
// executable to the configured Resource.
|
|
|
|
func WithProcessExecutablePath() Option {
|
|
|
|
return WithDetectors(processExecutablePathDetector{})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithProcessCommandArgs adds an attribute with all the command arguments (including
|
|
|
|
// the command/executable itself) as received by the process to the configured
|
|
|
|
// Resource.
|
|
|
|
//
|
|
|
|
// Warning! This option will include process command line arguments. If these
|
|
|
|
// contain sensitive information it will be included in the exported resource.
|
|
|
|
func WithProcessCommandArgs() Option {
|
|
|
|
return WithDetectors(processCommandArgsDetector{})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithProcessOwner adds an attribute with the username of the user that owns the process
|
|
|
|
// to the configured Resource.
|
|
|
|
func WithProcessOwner() Option {
|
|
|
|
return WithDetectors(processOwnerDetector{})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithProcessRuntimeName adds an attribute with the name of the runtime of this
|
|
|
|
// process to the configured Resource.
|
|
|
|
func WithProcessRuntimeName() Option {
|
|
|
|
return WithDetectors(processRuntimeNameDetector{})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithProcessRuntimeVersion adds an attribute with the version of the runtime of
|
|
|
|
// this process to the configured Resource.
|
|
|
|
func WithProcessRuntimeVersion() Option {
|
|
|
|
return WithDetectors(processRuntimeVersionDetector{})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithProcessRuntimeDescription adds an attribute with an additional description
|
|
|
|
// about the runtime of the process to the configured Resource.
|
|
|
|
func WithProcessRuntimeDescription() Option {
|
|
|
|
return WithDetectors(processRuntimeDescriptionDetector{})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// WithContainer adds all the Container attributes to the configured Resource.
|
|
|
|
// See individual WithContainer* functions to configure specific attributes.
|
|
|
|
func WithContainer() Option {
|
|
|
|
return WithDetectors(
|
|
|
|
cgroupContainerIDDetector{},
|
2021-08-09 07:19:24 +00:00
|
|
|
)
|
2023-02-01 17:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithContainerID adds an attribute with the id of the container to the configured Resource.
|
2023-12-18 20:31:00 +00:00
|
|
|
// Note: WithContainerID will not extract the correct container ID in an ECS environment.
|
|
|
|
// Please use the ECS resource detector instead (https://pkg.go.dev/go.opentelemetry.io/contrib/detectors/aws/ecs).
|
2023-02-01 17:06:36 +00:00
|
|
|
func WithContainerID() Option {
|
|
|
|
return WithDetectors(cgroupContainerIDDetector{})
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|