Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- When using WithNewRoot, don't use the parent context for making sampling decisions. (#2032)
- `oteltest.Tracer` now creates a valid `SpanContext` when using `WithNewRoot`. (#2073)
- OS type detector now sets the correct `dragonflybsd` value for DragonFly BSD. (#2092)

### Security

Expand Down
4 changes: 4 additions & 0 deletions sdk/resource/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ var (
RuntimeOS = runtimeOS
RuntimeArch = runtimeArch
)

var (
MapRuntimeOSToSemconvOSType = mapRuntimeOSToSemconvOSType
)
34 changes: 33 additions & 1 deletion sdk/resource/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"strings"

"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)

Expand All @@ -43,9 +44,11 @@ type osDescriptionDetector struct{}
func (osTypeDetector) Detect(ctx context.Context) (*Resource, error) {
osType := runtimeOS()

osTypeAttribute := mapRuntimeOSToSemconvOSType(osType)

return NewWithAttributes(
semconv.SchemaURL,
semconv.OSTypeKey.String(strings.ToLower(osType)),
osTypeAttribute,
), nil
}

Expand Down Expand Up @@ -84,3 +87,32 @@ func WithOS() Option {
osDescriptionDetector{},
)
}

// mapRuntimeOSToSemconvOSType translates the OS name as provided by the Go runtime
// into an OS type attribute with the corresponding value defined by the semantic
// conventions. In case the provided OS name isn't mapped, it's transformed to lowercase
// and used as the value for the returned OS type attribute.
func mapRuntimeOSToSemconvOSType(osType string) attribute.KeyValue {
// the elements in this map are the intersection between
// available GOOS values and defined semconv OS types
osTypeAttributeMap := map[string]attribute.KeyValue{
"darwin": semconv.OSTypeDarwin,
"dragonfly": semconv.OSTypeDragonflyBSD,
"freebsd": semconv.OSTypeFreeBSD,
"linux": semconv.OSTypeLinux,
"netbsd": semconv.OSTypeNetBSD,
"openbsd": semconv.OSTypeOpenBSD,
"solaris": semconv.OSTypeSolaris,
"windows": semconv.OSTypeWindows,
}

var osTypeAttribute attribute.KeyValue

if attr, ok := osTypeAttributeMap[osType]; ok {
osTypeAttribute = attr
} else {
osTypeAttribute = semconv.OSTypeKey.String(strings.ToLower(osType))
}

return osTypeAttribute
}
30 changes: 30 additions & 0 deletions sdk/resource/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (

"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)

func mockRuntimeProviders() {
Expand Down Expand Up @@ -84,3 +86,31 @@ func TestWithOS(t *testing.T) {
"os.description": "Test",
}, toMap(res))
}

func TestMapRuntimeOSToSemconvOSType(t *testing.T) {
tt := []struct {
Name string
Goos string
OSType attribute.KeyValue
}{
{"Apple Darwin", "darwin", semconv.OSTypeDarwin},
{"DragonFly BSD", "dragonfly", semconv.OSTypeDragonflyBSD},
{"FreeBSD", "freebsd", semconv.OSTypeFreeBSD},
{"Linux", "linux", semconv.OSTypeLinux},
{"NetBSD", "netbsd", semconv.OSTypeNetBSD},
{"OpenBSD", "openbsd", semconv.OSTypeOpenBSD},
{"Oracle Solaris", "solaris", semconv.OSTypeSolaris},
{"Microsoft Windows", "windows", semconv.OSTypeWindows},
{"Unknown", "unknown", semconv.OSTypeKey.String("unknown")},
{"UNKNOWN", "UNKNOWN", semconv.OSTypeKey.String("unknown")},
}

for _, tc := range tt {
tc := tc

t.Run(tc.Name, func(t *testing.T) {
osTypeAttribute := resource.MapRuntimeOSToSemconvOSType(tc.Goos)
require.EqualValues(t, osTypeAttribute, tc.OSType)
})
}
}