Skip to content

Commit cd9a3f1

Browse files
[extension/clientaddrmiddleware] Add initial clientaddrmiddleware extension (#930)
* [extension/clientaddrmiddleware] Add initial `clientaddrmiddleware` extension * Run `make goporto` * Run ` make gogenerate && make license-update && make lint` * Change configuration to `metadata_keys` * Update README.md to describe middleware that uses a predefined list of keys
1 parent b17fa25 commit cd9a3f1

File tree

12 files changed

+502
-0
lines changed

12 files changed

+502
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ../../Makefile.Common
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Client Address Middleware Extension
2+
3+
> [!WARNING]
4+
> 🚧 This component is a work in progress
5+
6+
The client address middleware provides the ability to set the [`client.Info.Addr`](https://github.com/open-telemetry/opentelemetry-collector/blob/client/v1.47.0/client/client.go#L95) based on the following metadata keys:
7+
- `forwarded`
8+
- `x-real-ip`
9+
- `x-forwarded-for`
10+
11+
Keys are processed in the above order, the first valid value is used to set the client address. If there are no valid addresses found, the client address is not updated.
12+
13+
## Configuration
14+
Receivers should be configured with `include_metadata: true`, so that the context includes client metadata keys.
15+
16+
### Example
17+
The following example configures both the otlp `grpc` and `http` receivers with the client address middleware.
18+
```yaml
19+
extensions:
20+
clientaddrmiddleware: {}
21+
22+
receivers:
23+
otlp:
24+
protocols:
25+
http:
26+
include_metadata: true
27+
middlewares:
28+
- id: clientaddrmiddleware
29+
grpc:
30+
include_metadata: true
31+
middlewares:
32+
- id: clientaddrmiddleware
33+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 clientaddrmiddlewareextension // import "github.com/elastic/opentelemetry-collector-components/extension/clientaddrmiddlewareextension"
19+
20+
import (
21+
"go.opentelemetry.io/collector/component"
22+
)
23+
24+
type Config struct {
25+
}
26+
27+
func createDefaultConfig() component.Config {
28+
return &Config{}
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
//go:generate mdatagen metadata.yaml
19+
20+
package clientaddrmiddlewareextension // import "github.com/elastic/opentelemetry-collector-components/extension/clientaddrmiddlewareextension"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 clientaddrmiddlewareextension // import "github.com/elastic/opentelemetry-collector-components/extension/clientaddrmiddlewareextension"
19+
20+
import (
21+
"context"
22+
23+
"go.opentelemetry.io/collector/component"
24+
"go.opentelemetry.io/collector/extension"
25+
26+
"github.com/elastic/opentelemetry-collector-components/extension/clientaddrmiddlewareextension/internal/metadata"
27+
)
28+
29+
func NewFactory() extension.Factory {
30+
return extension.NewFactory(
31+
metadata.Type,
32+
createDefaultConfig,
33+
createExtension,
34+
metadata.ExtensionStability,
35+
)
36+
}
37+
38+
func createExtension(_ context.Context, set extension.Settings, cfg component.Config) (extension.Extension, error) {
39+
return newClientAddrMiddleware(cfg.(*Config), set)
40+
}

extension/clientaddrmiddlewareextension/generated_component_test.go

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

extension/clientaddrmiddlewareextension/generated_package_test.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module github.com/elastic/opentelemetry-collector-components/extension/clientaddrmiddlewareextension
2+
3+
go 1.24.0
4+
5+
require (
6+
github.com/stretchr/testify v1.11.1
7+
go.opentelemetry.io/collector/component v1.47.0
8+
go.opentelemetry.io/collector/component/componenttest v0.141.0
9+
go.opentelemetry.io/collector/confmap v1.47.0
10+
go.opentelemetry.io/collector/extension v1.47.0
11+
go.opentelemetry.io/collector/extension/extensiontest v0.141.0
12+
go.uber.org/goleak v1.3.0
13+
google.golang.org/grpc v1.77.0
14+
)
15+
16+
require (
17+
github.com/davecgh/go-spew v1.1.1 // indirect
18+
github.com/go-logr/logr v1.4.3 // indirect
19+
github.com/go-logr/stdr v1.2.2 // indirect
20+
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
21+
github.com/gobwas/glob v0.2.3 // indirect
22+
github.com/google/uuid v1.6.0 // indirect
23+
github.com/hashicorp/go-version v1.7.0 // indirect
24+
github.com/json-iterator/go v1.1.12 // indirect
25+
github.com/knadh/koanf/maps v0.1.2 // indirect
26+
github.com/knadh/koanf/providers/confmap v1.0.0 // indirect
27+
github.com/knadh/koanf/v2 v2.3.0 // indirect
28+
github.com/mitchellh/copystructure v1.2.0 // indirect
29+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
30+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
31+
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
32+
github.com/pmezard/go-difflib v1.0.0 // indirect
33+
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
34+
go.opentelemetry.io/collector/featuregate v1.47.0 // indirect
35+
go.opentelemetry.io/collector/pdata v1.47.0 // indirect
36+
go.opentelemetry.io/otel v1.38.0 // indirect
37+
go.opentelemetry.io/otel/metric v1.38.0 // indirect
38+
go.opentelemetry.io/otel/sdk v1.38.0 // indirect
39+
go.opentelemetry.io/otel/sdk/metric v1.38.0 // indirect
40+
go.opentelemetry.io/otel/trace v1.38.0 // indirect
41+
go.uber.org/multierr v1.11.0 // indirect
42+
go.uber.org/zap v1.27.1 // indirect
43+
go.yaml.in/yaml/v3 v3.0.4 // indirect
44+
golang.org/x/net v0.46.1-0.20251013234738-63d1a5100f82 // indirect
45+
golang.org/x/sys v0.37.0 // indirect
46+
golang.org/x/text v0.30.0 // indirect
47+
google.golang.org/genproto/googleapis/rpc v0.0.0-20251022142026-3a174f9686a8 // indirect
48+
google.golang.org/protobuf v1.36.10 // indirect
49+
gopkg.in/yaml.v3 v3.0.1 // indirect
50+
)

0 commit comments

Comments
 (0)