Skip to content

Commit 91cb86d

Browse files
committed
Introduce a remapping interface and refactor hostmetrics
1 parent e7bf03c commit 91cb86d

File tree

15 files changed

+963
-137
lines changed

15 files changed

+963
-137
lines changed

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.DEFAULT_GOAL := all
2+
all: gomodtidy update-licenses fieldalignment fmt
3+
4+
test:
5+
go test -v -race ./...
6+
7+
fmt:
8+
go run golang.org/x/tools/cmd/goimports@v0.18.0 -w .
9+
10+
gomodtidy:
11+
go mod tidy -v
12+
13+
fieldalignment:
14+
go run golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@v0.18.0 -test=false $(shell go list ./... | grep -v test)
15+
16+
update-licenses:
17+
go run github.com/elastic/go-licenser@v0.4.1 -ext .go .

go.mod

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module github.com/elastic/opentelemetry-lib
2+
3+
go 1.21.1
4+
5+
require (
6+
github.com/google/go-cmp v0.6.0
7+
github.com/stretchr/testify v1.9.0
8+
go.opentelemetry.io/collector/pdata v1.7.0
9+
go.uber.org/zap v1.27.0
10+
)
11+
12+
require (
13+
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/gogo/protobuf v1.3.2 // indirect
15+
github.com/json-iterator/go v1.1.12 // indirect
16+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
17+
github.com/modern-go/reflect2 v1.0.2 // indirect
18+
github.com/pmezard/go-difflib v1.0.0 // indirect
19+
go.uber.org/multierr v1.11.0 // indirect
20+
golang.org/x/net v0.23.0 // indirect
21+
golang.org/x/sys v0.18.0 // indirect
22+
golang.org/x/text v0.14.0 // indirect
23+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
24+
google.golang.org/grpc v1.63.2 // indirect
25+
google.golang.org/protobuf v1.34.0 // indirect
26+
gopkg.in/yaml.v3 v3.0.1 // indirect
27+
)

go.sum

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
5+
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
6+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
7+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
8+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
9+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
10+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
11+
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
12+
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
13+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
14+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
15+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
16+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
17+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
18+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
19+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
20+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
21+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
22+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
23+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
24+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
25+
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
26+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
27+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
28+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
29+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
30+
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
31+
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
32+
go.opentelemetry.io/collector/pdata v1.7.0 h1:/WNsBbE6KM3TTPUb9v/5B7IDqnDkgf8GyFhVJJqu7II=
33+
go.opentelemetry.io/collector/pdata v1.7.0/go.mod h1:ehCBBA5GoFrMZkwyZAKGY/lAVSgZf6rzUt3p9mddmPU=
34+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
35+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
36+
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
37+
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
38+
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
39+
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
40+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
41+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
42+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
43+
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
44+
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
45+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
46+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
47+
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
48+
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
49+
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
50+
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
51+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
52+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
53+
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
54+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
55+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
56+
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
57+
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
58+
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
59+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
60+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
61+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
62+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
63+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
64+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
65+
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
66+
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
67+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
68+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
69+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
70+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
71+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de h1:cZGRis4/ot9uVm639a+rHCUaG0JJHEsdyzSQTMX+suY=
72+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY=
73+
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
74+
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
75+
google.golang.org/protobuf v1.34.0 h1:Qo/qEd2RZPCf2nKuorzksSknv0d3ERwp1vFG38gSmH4=
76+
google.golang.org/protobuf v1.34.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
77+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
78+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
79+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
80+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
81+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

remappers/common/const.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package common
19+
20+
const (
21+
// OTelTranslatedLabel is added as an attribute to each datapoint that
22+
// is translated from otel.
23+
OTelTranslatedLabel = "otel_translated"
24+
25+
// DatastreamDatasetLabel defines a dataset label for attributes.
26+
DatastreamDatasetLabel = "data_stream.dataset"
27+
)

remappers/hostmetrics/config.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package hostmetrics
19+
20+
type config struct {
21+
SystemIntegrationDataset bool
22+
}
23+
24+
// Option allows configuring the behavior of the hostmetrics remapper.
25+
type Option func(config) config
26+
27+
func newConfig(opts ...Option) (cfg config) {
28+
for _, opt := range opts {
29+
cfg = opt(cfg)
30+
}
31+
return cfg
32+
}
33+
34+
// WithSystemIntegrationDataset sets the dataset of the remapped metrics as
35+
// as per the system integration. Example: system.cpu, system.memory, etc.
36+
func WithSystemIntegrationDataset(b bool) Option {
37+
return func(c config) config {
38+
c.SystemIntegrationDataset = b
39+
return c
40+
}
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package hostmetrics
19+
20+
import (
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestConfig(t *testing.T) {
27+
for _, tc := range []struct {
28+
name string
29+
opts []Option
30+
expected config
31+
}{
32+
{
33+
name: "default",
34+
opts: nil,
35+
expected: config{
36+
SystemIntegrationDataset: false,
37+
},
38+
},
39+
{
40+
name: "system_integration_dataset",
41+
opts: []Option{WithSystemIntegrationDataset(true)},
42+
expected: config{
43+
SystemIntegrationDataset: true,
44+
},
45+
},
46+
} {
47+
t.Run(tc.name, func(t *testing.T) {
48+
assert.Equal(t, tc.expected, newConfig(tc.opts...))
49+
})
50+
}
51+
}

remappers/hostmetrics/cpu.go

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,51 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
118
package hostmetrics
219

320
import (
21+
"errors"
22+
423
"go.opentelemetry.io/collector/pdata/pcommon"
524
"go.opentelemetry.io/collector/pdata/pmetric"
625
)
726

8-
func addCPUMetrics(metrics pmetric.MetricSlice, resource pcommon.Resource, dataset string) error {
9-
var timestamp pcommon.Timestamp
27+
func remapCPUMetrics(
28+
src, out pmetric.MetricSlice,
29+
_ pcommon.Resource,
30+
dataset string,
31+
) error {
32+
var timestamp pcommon.Timestamp // Use the timestamp of the last read datapoint
1033
var numCores int64
1134
var totalPercent, idlePercent, systemPercent, userPercent, stealPercent,
1235
iowaitPercent, nicePercent, irqPercent, softirqPercent float64
1336

14-
// iterate all metrics in the current scope and generate the additional Elastic system integration metrics
15-
for i := 0; i < metrics.Len(); i++ {
16-
metric := metrics.At(i)
17-
if metric.Name() == "system.cpu.logical.count" {
37+
// iterate all metrics in the current scope and generate the additional Elastic
38+
// system integration metrics.
39+
for i := 0; i < src.Len(); i++ {
40+
metric := src.At(i)
41+
switch metric.Name() {
42+
case "system.cpu.logical.count":
1843
dp := metric.Sum().DataPoints().At(0)
1944
if timestamp == 0 {
2045
timestamp = dp.Timestamp()
2146
}
22-
2347
numCores = dp.IntValue()
24-
} else if metric.Name() == "system.cpu.utilization" {
48+
case "system.cpu.utilization":
2549
dataPoints := metric.Gauge().DataPoints()
2650
for j := 0; j < dataPoints.Len(); j++ {
2751
dp := dataPoints.At(j)
@@ -61,23 +85,8 @@ func addCPUMetrics(metrics pmetric.MetricSlice, resource pcommon.Resource, datas
6185
}
6286
}
6387

64-
totalNorm := totalPercent / float64(numCores)
65-
idleNorm := idlePercent / float64(numCores)
66-
systemNorm := systemPercent / float64(numCores)
67-
userNorm := userPercent / float64(numCores)
68-
stealNorm := stealPercent / float64(numCores)
69-
iowaitNorm := iowaitPercent / float64(numCores)
70-
niceNorm := nicePercent / float64(numCores)
71-
irqNorm := irqPercent / float64(numCores)
72-
softirqNorm := softirqPercent / float64(numCores)
73-
74-
addMetrics(metrics, resource, dataset,
75-
metric{
76-
dataType: pmetric.MetricTypeSum,
77-
name: "system.cpu.cores",
78-
timestamp: timestamp,
79-
intValue: &numCores,
80-
},
88+
// Add all metrics that are independent of cpu logical count.
89+
addMetrics(out, dataset, emptyMutator,
8190
metric{
8291
dataType: pmetric.MetricTypeGauge,
8392
name: "system.cpu.total.pct",
@@ -132,6 +141,38 @@ func addCPUMetrics(metrics pmetric.MetricSlice, resource pcommon.Resource, datas
132141
timestamp: timestamp,
133142
doubleValue: &softirqPercent,
134143
},
144+
)
145+
146+
// TODO (lahsivjar): Remove the dependency on the system.cpu.logical.count
147+
// metric. The num of cores can be derived from the system.cpu.utilization
148+
// metric using the cpu dimension.
149+
if numCores == 0 {
150+
return errors.New("system.cpu.logical.count metric is missing in the hostmetrics")
151+
}
152+
153+
totalNorm := totalPercent / float64(numCores)
154+
idleNorm := idlePercent / float64(numCores)
155+
systemNorm := systemPercent / float64(numCores)
156+
userNorm := userPercent / float64(numCores)
157+
stealNorm := stealPercent / float64(numCores)
158+
iowaitNorm := iowaitPercent / float64(numCores)
159+
niceNorm := nicePercent / float64(numCores)
160+
irqNorm := irqPercent / float64(numCores)
161+
softirqNorm := softirqPercent / float64(numCores)
162+
163+
addMetrics(out, dataset, emptyMutator,
164+
metric{
165+
dataType: pmetric.MetricTypeSum,
166+
name: "system.cpu.cores",
167+
timestamp: timestamp,
168+
intValue: &numCores,
169+
},
170+
metric{
171+
dataType: pmetric.MetricTypeSum,
172+
name: "system.load.cores",
173+
timestamp: timestamp,
174+
intValue: &numCores,
175+
},
135176
metric{
136177
dataType: pmetric.MetricTypeGauge,
137178
name: "system.cpu.total.norm.pct",

0 commit comments

Comments
 (0)