Skip to content

Commit c49361a

Browse files
avilevy18atoulme
andauthored
[processor/resourcedetection] Add os.version resource attribute (#38087)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Adding the `os.version` resource attribute to system `resourcedetection` processor <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes: #38059 <!--Describe what testing was performed and which tests were added.--> #### Testing Updated unit tests. <!--Describe the documentation added.--> #### Documentation N/A <!--Please delete paragraphs that you did not use before submitting.--> --------- Co-authored-by: Antoine Toulme <[email protected]>
1 parent 9eccd24 commit c49361a

File tree

11 files changed

+110
-1
lines changed

11 files changed

+110
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: 'enhancement'
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: resourcedetection
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: 'Adding the os.version resource attribute to system resourcedetection processor'
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [38087]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

internal/metadataproviders/system/metadata.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/Showmax/go-fqdn"
1515
"github.com/shirou/gopsutil/v4/cpu"
16+
"github.com/shirou/gopsutil/v4/host"
1617
conventions "go.opentelemetry.io/collector/semconv/v1.27.0"
1718
"go.opentelemetry.io/otel/attribute"
1819
"go.opentelemetry.io/otel/sdk/resource"
@@ -53,6 +54,9 @@ type Provider interface {
5354
// OSType returns the host operating system
5455
OSType() (string, error)
5556

57+
// OSVersion returns the version of the operating system
58+
OSVersion() (string, error)
59+
5660
// LookupCNAME returns the canonical name for the current host
5761
LookupCNAME() (string, error)
5862

@@ -88,6 +92,14 @@ func (systemMetadataProvider) OSType() (string, error) {
8892
return internal.GOOSToOSType(runtime.GOOS), nil
8993
}
9094

95+
func (systemMetadataProvider) OSVersion() (string, error) {
96+
info, err := host.Info()
97+
if err != nil {
98+
return "", fmt.Errorf("OSVersion failed to get os version: %w", err)
99+
}
100+
return info.PlatformVersion, nil
101+
}
102+
91103
func (systemMetadataProvider) FQDN() (string, error) {
92104
return fqdn.FqdnHostname()
93105
}

processor/resourcedetectionprocessor/internal/system/documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
| host.name | The host.name | Any Str | true |
2222
| os.description | Human readable OS version information. | Any Str | false |
2323
| os.type | The os.type | Any Str | true |
24+
| os.version | The os.version | Any Str | false |

processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_config.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_config_test.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_resource.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_resource_test.go

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/system/internal/metadata/testdata/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ all_set:
2727
enabled: true
2828
os.type:
2929
enabled: true
30+
os.version:
31+
enabled: true
3032
none_set:
3133
resource_attributes:
3234
host.arch:
@@ -55,3 +57,5 @@ none_set:
5557
enabled: false
5658
os.type:
5759
enabled: false
60+
os.version:
61+
enabled: false

processor/resourcedetectionprocessor/internal/system/metadata.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ resource_attributes:
1919
description: The os.type
2020
type: string
2121
enabled: true
22+
os.version:
23+
description: The os.version
24+
type: string
25+
enabled: false
2226
host.arch:
2327
description: The host.arch
2428
type: string

processor/resourcedetectionprocessor/internal/system/system.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
8888
return pcommon.NewResource(), "", fmt.Errorf("failed getting OS type: %w", err)
8989
}
9090

91+
osVersion, err := d.provider.OSVersion()
92+
if err != nil {
93+
return pcommon.NewResource(), "", fmt.Errorf("failed getting OS version: %w", err)
94+
}
95+
9196
hostArch, err := d.provider.HostArch()
9297
if err != nil {
9398
return pcommon.NewResource(), "", fmt.Errorf("failed getting host architecture: %w", err)
@@ -136,6 +141,7 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
136141
if err == nil {
137142
d.rb.SetHostName(hostname)
138143
d.rb.SetOsType(osType)
144+
d.rb.SetOsVersion(osVersion)
139145
if d.cfg.ResourceAttributes.HostID.Enabled {
140146
if hostID, hostIDErr := d.provider.HostID(ctx); hostIDErr == nil {
141147
d.rb.SetHostID(hostID)

processor/resourcedetectionprocessor/internal/system/system_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ func (m *mockMetadata) OSType() (string, error) {
4848
return args.String(0), args.Error(1)
4949
}
5050

51+
func (m *mockMetadata) OSVersion() (string, error) {
52+
args := m.MethodCalled("OSVersion")
53+
return args.String(0), args.Error(1)
54+
}
55+
5156
func (m *mockMetadata) HostID(_ context.Context) (string, error) {
5257
args := m.MethodCalled("HostID")
5358
return args.String(0), args.Error(1)
@@ -147,6 +152,7 @@ func allEnabledConfig() metadata.ResourceAttributesConfig {
147152
cfg.HostIP.Enabled = true
148153
cfg.HostMac.Enabled = true
149154
cfg.OsDescription.Enabled = true
155+
cfg.OsVersion.Enabled = true
150156
return cfg
151157
}
152158

@@ -155,6 +161,7 @@ func TestDetectFQDNAvailable(t *testing.T) {
155161
md.On("FQDN").Return("fqdn", nil)
156162
md.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
157163
md.On("OSType").Return("darwin", nil)
164+
md.On("OSVersion").Return("22.04.2 LTS (Jammy Jellyfish)", nil)
158165
md.On("HostID").Return("2", nil)
159166
md.On("HostArch").Return("amd64", nil)
160167
md.On("HostIPs").Return(testIPsAddresses, nil)
@@ -171,6 +178,7 @@ func TestDetectFQDNAvailable(t *testing.T) {
171178
conventions.AttributeHostName: "fqdn",
172179
conventions.AttributeOSDescription: "Ubuntu 22.04.2 LTS (Jammy Jellyfish)",
173180
conventions.AttributeOSType: "darwin",
181+
conventions.AttributeOSVersion: "22.04.2 LTS (Jammy Jellyfish)",
174182
conventions.AttributeHostID: "2",
175183
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
176184
"host.ip": testIPsAttribute,
@@ -186,6 +194,7 @@ func TestFallbackHostname(t *testing.T) {
186194
mdHostname.On("FQDN").Return("", errors.New("err"))
187195
mdHostname.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
188196
mdHostname.On("OSType").Return("darwin", nil)
197+
mdHostname.On("OSVersion").Return("22.04.2 LTS (Jammy Jellyfish)", nil)
189198
mdHostname.On("HostArch").Return("amd64", nil)
190199

191200
detector := newTestDetector(mdHostname, []string{"dns", "os"}, metadata.DefaultResourceAttributesConfig())
@@ -210,6 +219,7 @@ func TestEnableHostID(t *testing.T) {
210219
mdHostname.On("FQDN").Return("", errors.New("err"))
211220
mdHostname.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
212221
mdHostname.On("OSType").Return("darwin", nil)
222+
mdHostname.On("OSVersion").Return("22.04.2 LTS (Jammy Jellyfish)", nil)
213223
mdHostname.On("HostID").Return("3", nil)
214224
mdHostname.On("HostArch").Return("amd64", nil)
215225
mdHostname.On("HostIPs").Return(testIPsAddresses, nil)
@@ -225,6 +235,7 @@ func TestEnableHostID(t *testing.T) {
225235
conventions.AttributeHostName: "hostname",
226236
conventions.AttributeOSDescription: "Ubuntu 22.04.2 LTS (Jammy Jellyfish)",
227237
conventions.AttributeOSType: "darwin",
238+
conventions.AttributeOSVersion: "22.04.2 LTS (Jammy Jellyfish)",
228239
conventions.AttributeHostID: "3",
229240
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
230241
"host.ip": testIPsAttribute,
@@ -239,6 +250,7 @@ func TestUseHostname(t *testing.T) {
239250
mdHostname.On("Hostname").Return("hostname", nil)
240251
mdHostname.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
241252
mdHostname.On("OSType").Return("darwin", nil)
253+
mdHostname.On("OSVersion").Return("22.04.2 LTS (Jammy Jellyfish)", nil)
242254
mdHostname.On("HostID").Return("1", nil)
243255
mdHostname.On("HostArch").Return("amd64", nil)
244256
mdHostname.On("HostIPs").Return(testIPsAddresses, nil)
@@ -254,6 +266,7 @@ func TestUseHostname(t *testing.T) {
254266
conventions.AttributeHostName: "hostname",
255267
conventions.AttributeOSDescription: "Ubuntu 22.04.2 LTS (Jammy Jellyfish)",
256268
conventions.AttributeOSType: "darwin",
269+
conventions.AttributeOSVersion: "22.04.2 LTS (Jammy Jellyfish)",
257270
conventions.AttributeHostID: "1",
258271
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
259272
"host.ip": testIPsAttribute,
@@ -268,6 +281,7 @@ func TestDetectError(t *testing.T) {
268281
mdFQDN := &mockMetadata{}
269282
mdFQDN.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
270283
mdFQDN.On("OSType").Return("windows", nil)
284+
mdFQDN.On("OSVersion").Return("22.04.2 LTS (Jammy Jellyfish)", nil)
271285
mdFQDN.On("FQDN").Return("", errors.New("err"))
272286
mdFQDN.On("Hostname").Return("", errors.New("err"))
273287
mdFQDN.On("HostID").Return("", errors.New("err"))
@@ -285,6 +299,7 @@ func TestDetectError(t *testing.T) {
285299
mdHostname := &mockMetadata{}
286300
mdHostname.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
287301
mdHostname.On("OSType").Return("windows", nil)
302+
mdHostname.On("OSVersion").Return("22.04.2 LTS (Jammy Jellyfish)", nil)
288303
mdHostname.On("Hostname").Return("", errors.New("err"))
289304
mdHostname.On("HostID").Return("", errors.New("err"))
290305
mdHostname.On("HostArch").Return("amd64", nil)
@@ -302,6 +317,7 @@ func TestDetectError(t *testing.T) {
302317
mdOSType.On("FQDN").Return("fqdn", nil)
303318
mdOSType.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
304319
mdOSType.On("OSType").Return("", errors.New("err"))
320+
mdOSType.On("OSVersion").Return("", "22.04.2 LTS (Jammy Jellyfish)")
305321
mdOSType.On("HostID").Return("1", nil)
306322
mdOSType.On("HostArch").Return("amd64", nil)
307323
mdOSType.On("HostIPs").Return(testIPsAddresses, nil)
@@ -312,11 +328,28 @@ func TestDetectError(t *testing.T) {
312328
assert.Equal(t, "", schemaURL)
313329
assert.True(t, internal.IsEmptyResource(res))
314330

331+
// OS version fails
332+
mdOSVersion := &mockMetadata{}
333+
mdOSVersion.On("FQDN").Return("fqdn", nil)
334+
mdOSVersion.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
335+
mdOSVersion.On("OSType").Return("windows", nil)
336+
mdOSVersion.On("OSVersion").Return("", errors.New("err"))
337+
mdOSVersion.On("HostID").Return("1", nil)
338+
mdOSVersion.On("HostArch").Return("amd64", nil)
339+
mdOSVersion.On("HostIPs").Return(testIPsAddresses, nil)
340+
341+
detector = newTestDetector(mdOSVersion, []string{"os"}, allEnabledConfig())
342+
res, schemaURL, err = detector.Detect(context.Background())
343+
assert.Error(t, err)
344+
assert.Equal(t, "", schemaURL)
345+
assert.True(t, internal.IsEmptyResource(res))
346+
315347
// Host ID fails. All other attributes should be set.
316348
mdHostID := &mockMetadata{}
317349
mdHostID.On("Hostname").Return("hostname", nil)
318350
mdHostID.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
319351
mdHostID.On("OSType").Return("linux", nil)
352+
mdHostID.On("OSVersion").Return("22.04.2 LTS (Jammy Jellyfish)", nil)
320353
mdHostID.On("HostID").Return("", errors.New("err"))
321354
mdHostID.On("HostArch").Return("arm64", nil)
322355
mdHostID.On("HostIPs").Return(testIPsAddresses, nil)
@@ -330,6 +363,7 @@ func TestDetectError(t *testing.T) {
330363
conventions.AttributeHostName: "hostname",
331364
conventions.AttributeOSDescription: "Ubuntu 22.04.2 LTS (Jammy Jellyfish)",
332365
conventions.AttributeOSType: "linux",
366+
conventions.AttributeOSVersion: "22.04.2 LTS (Jammy Jellyfish)",
333367
conventions.AttributeHostArch: conventions.AttributeHostArchARM64,
334368
"host.ip": testIPsAttribute,
335369
"host.mac": testMACsAttribute,
@@ -341,6 +375,7 @@ func TestDetectCPUInfo(t *testing.T) {
341375
md.On("FQDN").Return("fqdn", nil)
342376
md.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
343377
md.On("OSType").Return("darwin", nil)
378+
md.On("OSVersion").Return("22.04.2 LTS (Jammy Jellyfish)", nil)
344379
md.On("HostID").Return("2", nil)
345380
md.On("HostArch").Return("amd64", nil)
346381
md.On("HostIPs").Return(testIPsAddresses, nil)
@@ -359,6 +394,7 @@ func TestDetectCPUInfo(t *testing.T) {
359394
conventions.AttributeHostName: "fqdn",
360395
conventions.AttributeOSDescription: "Ubuntu 22.04.2 LTS (Jammy Jellyfish)",
361396
conventions.AttributeOSType: "darwin",
397+
conventions.AttributeOSVersion: "22.04.2 LTS (Jammy Jellyfish)",
362398
conventions.AttributeHostID: "2",
363399
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
364400
"host.ip": testIPsAttribute,

0 commit comments

Comments
 (0)