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"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
|
2021-08-09 07:19:24 +00:00
|
|
|
)
|
|
|
|
|
2023-12-21 16:20:48 +00:00
|
|
|
type (
|
|
|
|
pidProvider func() int
|
|
|
|
executablePathProvider func() (string, error)
|
|
|
|
commandArgsProvider func() []string
|
|
|
|
ownerProvider func() (*user.User, error)
|
|
|
|
runtimeNameProvider func() string
|
|
|
|
runtimeVersionProvider func() string
|
|
|
|
runtimeOSProvider func() string
|
|
|
|
runtimeArchProvider func() string
|
|
|
|
)
|
2021-08-09 07:19:24 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
defaultPidProvider pidProvider = os.Getpid
|
|
|
|
defaultExecutablePathProvider executablePathProvider = os.Executable
|
|
|
|
defaultCommandArgsProvider commandArgsProvider = func() []string { return os.Args }
|
|
|
|
defaultOwnerProvider ownerProvider = user.Current
|
2023-02-01 17:06:36 +00:00
|
|
|
defaultRuntimeNameProvider runtimeNameProvider = func() string {
|
|
|
|
if runtime.Compiler == "gc" {
|
|
|
|
return "go"
|
|
|
|
}
|
|
|
|
return runtime.Compiler
|
|
|
|
}
|
2021-08-09 07:19:24 +00:00
|
|
|
defaultRuntimeVersionProvider runtimeVersionProvider = runtime.Version
|
|
|
|
defaultRuntimeOSProvider runtimeOSProvider = func() string { return runtime.GOOS }
|
|
|
|
defaultRuntimeArchProvider runtimeArchProvider = func() string { return runtime.GOARCH }
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
pid = defaultPidProvider
|
|
|
|
executablePath = defaultExecutablePathProvider
|
|
|
|
commandArgs = defaultCommandArgsProvider
|
|
|
|
owner = defaultOwnerProvider
|
|
|
|
runtimeName = defaultRuntimeNameProvider
|
|
|
|
runtimeVersion = defaultRuntimeVersionProvider
|
|
|
|
runtimeOS = defaultRuntimeOSProvider
|
|
|
|
runtimeArch = defaultRuntimeArchProvider
|
|
|
|
)
|
|
|
|
|
|
|
|
func setDefaultOSProviders() {
|
|
|
|
setOSProviders(
|
|
|
|
defaultPidProvider,
|
|
|
|
defaultExecutablePathProvider,
|
|
|
|
defaultCommandArgsProvider,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setOSProviders(
|
|
|
|
pidProvider pidProvider,
|
|
|
|
executablePathProvider executablePathProvider,
|
|
|
|
commandArgsProvider commandArgsProvider,
|
|
|
|
) {
|
|
|
|
pid = pidProvider
|
|
|
|
executablePath = executablePathProvider
|
|
|
|
commandArgs = commandArgsProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func setDefaultRuntimeProviders() {
|
|
|
|
setRuntimeProviders(
|
|
|
|
defaultRuntimeNameProvider,
|
|
|
|
defaultRuntimeVersionProvider,
|
|
|
|
defaultRuntimeOSProvider,
|
|
|
|
defaultRuntimeArchProvider,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setRuntimeProviders(
|
|
|
|
runtimeNameProvider runtimeNameProvider,
|
|
|
|
runtimeVersionProvider runtimeVersionProvider,
|
|
|
|
runtimeOSProvider runtimeOSProvider,
|
|
|
|
runtimeArchProvider runtimeArchProvider,
|
|
|
|
) {
|
|
|
|
runtimeName = runtimeNameProvider
|
|
|
|
runtimeVersion = runtimeVersionProvider
|
|
|
|
runtimeOS = runtimeOSProvider
|
|
|
|
runtimeArch = runtimeArchProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func setDefaultUserProviders() {
|
|
|
|
setUserProviders(defaultOwnerProvider)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setUserProviders(ownerProvider ownerProvider) {
|
|
|
|
owner = ownerProvider
|
|
|
|
}
|
|
|
|
|
2023-12-21 16:20:48 +00:00
|
|
|
type (
|
|
|
|
processPIDDetector struct{}
|
|
|
|
processExecutableNameDetector struct{}
|
|
|
|
processExecutablePathDetector struct{}
|
|
|
|
processCommandArgsDetector struct{}
|
|
|
|
processOwnerDetector struct{}
|
|
|
|
processRuntimeNameDetector struct{}
|
|
|
|
processRuntimeVersionDetector struct{}
|
|
|
|
processRuntimeDescriptionDetector struct{}
|
|
|
|
)
|
2021-08-09 07:19:24 +00:00
|
|
|
|
|
|
|
// Detect returns a *Resource that describes the process identifier (PID) of the
|
|
|
|
// executing process.
|
|
|
|
func (processPIDDetector) Detect(ctx context.Context) (*Resource, error) {
|
2023-12-18 20:31:00 +00:00
|
|
|
return NewWithAttributes(semconv.SchemaURL, semconv.ProcessPID(pid())), nil
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect returns a *Resource that describes the name of the process executable.
|
|
|
|
func (processExecutableNameDetector) Detect(ctx context.Context) (*Resource, error) {
|
|
|
|
executableName := filepath.Base(commandArgs()[0])
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
return NewWithAttributes(semconv.SchemaURL, semconv.ProcessExecutableName(executableName)), nil
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect returns a *Resource that describes the full path of the process executable.
|
|
|
|
func (processExecutablePathDetector) Detect(ctx context.Context) (*Resource, error) {
|
|
|
|
executablePath, err := executablePath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
return NewWithAttributes(semconv.SchemaURL, semconv.ProcessExecutablePath(executablePath)), nil
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect returns a *Resource that describes all the command arguments as received
|
|
|
|
// by the process.
|
|
|
|
func (processCommandArgsDetector) Detect(ctx context.Context) (*Resource, error) {
|
2023-12-18 20:31:00 +00:00
|
|
|
return NewWithAttributes(semconv.SchemaURL, semconv.ProcessCommandArgs(commandArgs()...)), nil
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect returns a *Resource that describes the username of the user that owns the
|
|
|
|
// process.
|
|
|
|
func (processOwnerDetector) Detect(ctx context.Context) (*Resource, error) {
|
|
|
|
owner, err := owner()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
return NewWithAttributes(semconv.SchemaURL, semconv.ProcessOwner(owner.Username)), nil
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect returns a *Resource that describes the name of the compiler used to compile
|
|
|
|
// this process image.
|
|
|
|
func (processRuntimeNameDetector) Detect(ctx context.Context) (*Resource, error) {
|
2023-12-18 20:31:00 +00:00
|
|
|
return NewWithAttributes(semconv.SchemaURL, semconv.ProcessRuntimeName(runtimeName())), nil
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect returns a *Resource that describes the version of the runtime of this process.
|
|
|
|
func (processRuntimeVersionDetector) Detect(ctx context.Context) (*Resource, error) {
|
2023-12-18 20:31:00 +00:00
|
|
|
return NewWithAttributes(semconv.SchemaURL, semconv.ProcessRuntimeVersion(runtimeVersion())), nil
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect returns a *Resource that describes the runtime of this process.
|
|
|
|
func (processRuntimeDescriptionDetector) Detect(ctx context.Context) (*Resource, error) {
|
|
|
|
runtimeDescription := fmt.Sprintf(
|
|
|
|
"go version %s %s/%s", runtimeVersion(), runtimeOS(), runtimeArch())
|
|
|
|
|
|
|
|
return NewWithAttributes(
|
2023-02-01 17:06:36 +00:00
|
|
|
semconv.SchemaURL,
|
2023-12-18 20:31:00 +00:00
|
|
|
semconv.ProcessRuntimeDescription(runtimeDescription),
|
2021-08-09 07:19:24 +00:00
|
|
|
), nil
|
|
|
|
}
|