Skip to content

Commit

Permalink
Add http_forwarder skeleton (#979)
Browse files Browse the repository at this point in the history
* Add http_forwarder skeleton

* Address feedback

* Address feedback
  • Loading branch information
asuresh4 committed Sep 11, 2020
1 parent 4277feb commit 7dc53b4
Show file tree
Hide file tree
Showing 8 changed files with 1,512 additions and 0 deletions.
1 change: 1 addition & 0 deletions extension/httpforwarder/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
26 changes: 26 additions & 0 deletions extension/httpforwarder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# HTTP Forwarder Extension

This extension accepts HTTP requests, optionally adds headers to them and forwards them.
The RequestURIs of the original requests are preserved by the extension.

## Configuration

* `ingress`: HTTP config settings for HTTP server listening to requests.
* `endpoint` (default: `localhost:6060`): The host to which requests should be forwarded to.
* `egress`: HTTP config settings to use for forwarding requests.
* `endpoint` (**required**): The target to which requests should be forwarded to.
* `headers` (default: `nil`): Additional headers to be added to all requests passing through the extension.
* `timeout` (default: `10s`): How long to wait for each request to complete.

### Example

```yaml
http_forwarder:
ingress:
endpoint: localhost:7070
egress:
endpoint: http:https://target/
headers:
otel_http_forwarder: dev
timeout: 5s
```
31 changes: 31 additions & 0 deletions extension/httpforwarder/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package httpforwarder

import (
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configmodels"
)

// Config defines configuration for http forwarder extension.
type Config struct {
configmodels.ExtensionSettings `mapstructure:",squash"`

// Ingress holds config settings for HTTP server listening for requests.
Ingress confighttp.HTTPServerSettings `mapstructure:"ingress"`

// Egress holds config settings to use for forwarded requests.
Egress confighttp.HTTPClientSettings `mapstructure:"egress"`
}
36 changes: 36 additions & 0 deletions extension/httpforwarder/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package httpforwarder

import (
"context"

"go.opentelemetry.io/collector/component"
)

type httpForwarder struct {
}

var _ component.ServiceExtension = (*httpForwarder)(nil)

func (h httpForwarder) Start(_ context.Context, _ component.Host) error {
// TODO: Start HTTP server
return nil
}

func (h httpForwarder) Shutdown(_ context.Context) error {
// TODO: Shutdown HTTP server
return nil
}
65 changes: 65 additions & 0 deletions extension/httpforwarder/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package httpforwarder

import (
"context"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/extension/extensionhelper"
)

const (
// The value of extension "type" in configuration.
typeStr configmodels.Type = "http_forwarder"

// Default endpoints to bind to.
defaultEndpoint = ":6060"
)

// NewFactory creates a factory for HostObserver extension.
func NewFactory() component.ExtensionFactory {
return extensionhelper.NewFactory(
typeStr,
createDefaultConfig,
createExtension)
}

func createDefaultConfig() configmodels.Extension {
return &Config{
ExtensionSettings: configmodels.ExtensionSettings{
TypeVal: typeStr,
NameVal: string(typeStr),
},
Ingress: confighttp.HTTPServerSettings{
Endpoint: defaultEndpoint,
},
Egress: confighttp.HTTPClientSettings{
Timeout: 10 * time.Second,
},
}
}

func createExtension(
_ context.Context,
_ component.ExtensionCreateParams,
_ configmodels.Extension,
) (component.ServiceExtension, error) {
// TODO: Return httpForwarder
return nil, nil
}
35 changes: 35 additions & 0 deletions extension/httpforwarder/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package httpforwarder

import (
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config/configmodels"
)

func TestFactory(t *testing.T) {
f := NewFactory()
expectType := "http_forwarder"
require.Equal(t, configmodels.Type(expectType), f.Type())

cfg := f.CreateDefaultConfig().(*Config)
require.Equal(t, expectType, cfg.Name())
require.Equal(t, configmodels.Type(expectType), cfg.Type())
require.Equal(t, ":6060", cfg.Ingress.Endpoint)
require.Equal(t, 10*time.Second, cfg.Egress.Timeout)
}
9 changes: 9 additions & 0 deletions extension/httpforwarder/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/extension/httpforwarder

go 1.14

require (
github.com/stretchr/testify v1.6.1
go.opentelemetry.io/collector v0.9.1-0.20200903224024-3eb3b664a832
go.uber.org/zap v1.15.0
)
Loading

0 comments on commit 7dc53b4

Please sign in to comment.