Skip to content

Commit 87ec73c

Browse files
committed
Add initial implementation for exporter-toolkit receiver interface
Signed-off-by: Arthur Silva Sens <[email protected]>
1 parent 654f19d commit 87ec73c

File tree

7 files changed

+384
-0
lines changed

7 files changed

+384
-0
lines changed

otelcollector/config.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package otelcollector
15+
16+
import "fmt"
17+
18+
type Config struct {
19+
DisableDefaults bool
20+
EnableCollectors []string
21+
ExcludeCollectors []string
22+
}
23+
24+
func (c Config) Validate() error {
25+
if len(c.EnableCollectors) > 0 && len(c.ExcludeCollectors) > 0 {
26+
return fmt.Errorf("%q and %q can't be used at the same time", "EnableCollectors", "ExcludeCollectors")
27+
}
28+
return nil
29+
}

otelcollector/doc.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
// Package otelcollector implements the OpenTelemetry Collector receiver interface
15+
// for the Prometheus Node Exporter.
16+
//
17+
// Use this go module to embed the Prometheus Node Exporter in your OpenTelemetry Collector
18+
// using OCB (OpenTelemetry Collector Builder).
19+
package otelcollector

otelcollector/exporter.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package otelcollector
15+
16+
import (
17+
"context"
18+
"errors"
19+
20+
"github.com/prometheus/client_golang/prometheus"
21+
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
22+
"github.com/prometheus/exporter-toolkit/otlpreceiver"
23+
)
24+
25+
type NodeExporter struct {
26+
config *Config
27+
registry *prometheus.Registry
28+
}
29+
30+
func NewNodeExporter(config *Config) *NodeExporter {
31+
return &NodeExporter{
32+
config: config,
33+
registry: prometheus.NewRegistry(),
34+
}
35+
}
36+
37+
func (ne *NodeExporter) Initialize(ctx context.Context, cfg otlpreceiver.Config) (*prometheus.Registry, error) {
38+
var exporterCfg *Config
39+
40+
if cfg != nil {
41+
var ok bool
42+
exporterCfg, ok = cfg.(*Config)
43+
if !ok {
44+
return nil, errors.New("error reading configuration")
45+
}
46+
} else {
47+
// Use default configuration when none is provided
48+
exporterCfg = &Config{
49+
DisableDefaults: false,
50+
EnableCollectors: []string{},
51+
ExcludeCollectors: []string{},
52+
}
53+
}
54+
55+
ne.registry.MustRegister(versioncollector.NewCollector("node_exporter"))
56+
57+
ne.config = exporterCfg
58+
return ne.registry, nil
59+
}
60+
61+
func (ne *NodeExporter) Shutdown(_ context.Context) error {
62+
// There's nothing special needed to shutdown node-exporter.
63+
return nil
64+
}

otelcollector/factory.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package otelcollector
15+
16+
import (
17+
"go.opentelemetry.io/collector/component"
18+
"go.opentelemetry.io/collector/receiver"
19+
20+
"github.com/prometheus/exporter-toolkit/otlpreceiver"
21+
)
22+
23+
func NewFactory() receiver.Factory {
24+
defaults := map[string]interface{}{
25+
"disable_defaults": false,
26+
"enable_collectors": []string{},
27+
"exclude_collectors": []string{},
28+
}
29+
30+
return otlpreceiver.NewFactory(
31+
otlpreceiver.WithType(component.MustNewType("prometheus_node_exporter")),
32+
otlpreceiver.WithInitializer(NewNodeExporter(&Config{})),
33+
otlpreceiver.WithConfigUnmarshaler(&ConfigUnmarshaler{}),
34+
otlpreceiver.WithComponentDefaults(defaults),
35+
)
36+
}

otelcollector/go.mod

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module github.com/prometheus/node_exporter/otel_collector
2+
3+
go 1.25.0
4+
5+
require (
6+
github.com/prometheus/client_golang v1.23.2
7+
github.com/prometheus/exporter-toolkit v0.0.0-00010101000000-000000000000
8+
go.opentelemetry.io/collector/component v1.44.0
9+
go.opentelemetry.io/collector/receiver v1.44.0
10+
)
11+
12+
require (
13+
github.com/beorn7/perks v1.0.1 // indirect
14+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
15+
github.com/go-logr/logr v1.4.3 // indirect
16+
github.com/go-logr/stdr v1.2.2 // indirect
17+
github.com/gogo/protobuf v1.3.2 // indirect
18+
github.com/hashicorp/go-version v1.7.0 // indirect
19+
github.com/json-iterator/go v1.1.12 // indirect
20+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
21+
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
22+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
23+
github.com/prometheus/client_model v0.6.2 // indirect
24+
github.com/prometheus/common v0.66.1 // indirect
25+
github.com/prometheus/procfs v0.16.1 // indirect
26+
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
27+
go.opentelemetry.io/collector/consumer v1.44.0 // indirect
28+
go.opentelemetry.io/collector/featuregate v1.44.0 // indirect
29+
go.opentelemetry.io/collector/internal/telemetry v0.138.0 // indirect
30+
go.opentelemetry.io/collector/pdata v1.44.0 // indirect
31+
go.opentelemetry.io/collector/pipeline v1.44.0 // indirect
32+
go.opentelemetry.io/contrib/bridges/otelzap v0.13.0 // indirect
33+
go.opentelemetry.io/otel v1.38.0 // indirect
34+
go.opentelemetry.io/otel/log v0.14.0 // indirect
35+
go.opentelemetry.io/otel/metric v1.38.0 // indirect
36+
go.opentelemetry.io/otel/trace v1.38.0 // indirect
37+
go.uber.org/multierr v1.11.0 // indirect
38+
go.uber.org/zap v1.27.0 // indirect
39+
go.yaml.in/yaml/v2 v2.4.3 // indirect
40+
golang.org/x/net v0.45.0 // indirect
41+
golang.org/x/sys v0.37.0 // indirect
42+
golang.org/x/text v0.30.0 // indirect
43+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250825161204-c5933d9347a5 // indirect
44+
google.golang.org/grpc v1.76.0 // indirect
45+
google.golang.org/protobuf v1.36.10 // indirect
46+
)
47+
48+
replace github.com/prometheus/exporter-toolkit => ../../exporter-toolkit

0 commit comments

Comments
 (0)