Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a new AWS ECS Health Check Extension #4451

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ updates:
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "gomod"
directory: "/cmd/configschema"
schedule:
interval: "weekly"
- package-ecosystem: "gomod"
directory: "/cmd/mdatagen"
schedule:
Expand Down Expand Up @@ -169,6 +173,10 @@ updates:
directory: "/exporter/zipkinexporter"
schedule:
interval: "weekly"
- package-ecosystem: "gomod"
directory: "/extension/awsecshealthcheckextension"
schedule:
interval: "weekly"
- package-ecosystem: "gomod"
directory: "/extension/awsxrayproxy"
schedule:
Expand Down Expand Up @@ -493,10 +501,6 @@ updates:
directory: "/receiver/sapmreceiver"
schedule:
interval: "weekly"
- package-ecosystem: "gomod"
directory: "/receiver/scrapererror"
schedule:
interval: "weekly"
- package-ecosystem: "gomod"
directory: "/receiver/scraperhelper"
schedule:
Expand Down
3 changes: 3 additions & 0 deletions cmd/configschema/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sumologicexporter v0.34.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/tanzuobservabilityexporter v0.34.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/zipkinexporter v0.34.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/extension/awsecshealthcheckextension v0.34.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/extension/bearertokenauthextension v0.34.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/extension/fluentbitextension v0.34.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/extension/healthcheckextension v0.34.0 // indirect
Expand Down Expand Up @@ -459,6 +460,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elast

replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/zipkinexporter => ../../exporter/zipkinexporter

replace github.com/open-telemetry/opentelemetry-collector-contrib/extension/awsecshealthcheckextension => ../../extension/awsecshealthcheckextension

replace github.com/open-telemetry/opentelemetry-collector-contrib/extension/bearertokenauthextension => ../../extension/bearertokenauthextension

replace github.com/open-telemetry/opentelemetry-collector-contrib/extension/fluentbitextension => ../../extension/fluentbitextension
Expand Down
1 change: 1 addition & 0 deletions extension/awsecshealthcheckextension/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
22 changes: 22 additions & 0 deletions extension/awsecshealthcheckextension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# AWS ECS Health Check Extension

This new health check extension is used for ECS, it will store the exporter failures in a queue and
when there is a http request from ECS, will respond a health status based on the number of failures stored.
The failures in the queue will be rotated if expired than the time interval provided by the customer config.

# Configuration

The following settings are required:

- `endpoint` (default = `127.0.0.1:13134`): The host which could handle http request and provide http response.
- `interval` (default = `5m`): Time interval to check the number of failures, old failures which expired will be rotated.
- `exporter_error_limit` (default = `5`): The error number threshold to mark containers as healthy.

# Example

```yaml
aws_ecs_health_check:
endpoint: 127.0.0.1:13134
interval: "5m"
exporter_error_limit: 5
```
55 changes: 55 additions & 0 deletions extension/awsecshealthcheckextension/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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://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 awsecshealthcheckextension

import (
"errors"
"time"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
)

type Config struct {
config.ExtensionSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct

// TCPAddr represents a tcp endpoint address that is to publish the health check status.
// The default endpoint is "127.0.0.1:13134".
TCPAddr confignet.TCPAddr `mapstructure:",squash"`

// Interval is the time error kept, and error limit is the threshold of number of error happened during the interval
Interval string `mapstructure:"interval"`

// current scope will be focus on exporter failures
// TODO receiver/processor failures
ExporterErrorLimit int `mapstructure:"exporter_error_limit"`
}

var _ config.Extension = (*Config)(nil)

// Validate checks if the extension configuration is valid
func (cfg *Config) Validate() error {
_, err := time.ParseDuration(cfg.Interval)
if err != nil {
return err
}
if cfg.TCPAddr.Endpoint == "" {
return errors.New("bad config: endpoint must be specified")
}
if cfg.ExporterErrorLimit <= 0 {
return errors.New("bad config: exporter_error_limit expects a positive number")
}
return nil
}
58 changes: 58 additions & 0 deletions extension/awsecshealthcheckextension/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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://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 awsecshealthcheckextension

import (
"path"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtest"
)

func TestLoadConfig(t *testing.T) {
factories, err := componenttest.NopFactories()
assert.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.NoError(t, err)
require.NoError(t, err)

If this fails, it would create a flaky test with a nil pointer exception


factory := NewFactory()
factories.Extensions[typeStr] = factory
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)

require.Nil(t, err)
require.NotNil(t, cfg)

ext0 := cfg.Extensions[config.NewID(typeStr)]
assert.Equal(t, factory.CreateDefaultConfig(), ext0)

ext1 := cfg.Extensions[config.NewIDWithName(typeStr, "1")]
assert.Equal(t,
&Config{
ExtensionSettings: config.NewExtensionSettings(config.NewIDWithName(typeStr, "1")),
TCPAddr: confignet.TCPAddr{
Endpoint: "localhost:13134",
},
Interval: "5m",
ExporterErrorLimit: 5,
},
ext1)

assert.Equal(t, 1, len(cfg.Service.Extensions))
assert.Equal(t, config.NewIDWithName(typeStr, "1"), cfg.Service.Extensions[0])
}
62 changes: 62 additions & 0 deletions extension/awsecshealthcheckextension/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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://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 awsecshealthcheckextension

import (
"sync"
"time"

"go.opencensus.io/stats/view"
)

const (
exporterFailureView = "exporter/send_failed_requests"
)

// ECSHealthCheckExporter is a struct implement the exporter interface in open census that could export metrics
type ecsHealthCheckExporter struct {
mu sync.Mutex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mu sync.Mutex
rw sync.RWMutex

Using a RW Mutex will allow better performance.

exporterErrorQueue []*view.Data
}

func newECSHealthCheckExporter() *ecsHealthCheckExporter {
return &ecsHealthCheckExporter{}
}

// ExportView function could export the failure view to the queue
func (e *ecsHealthCheckExporter) ExportView(vd *view.Data) {
e.mu.Lock()
defer e.mu.Unlock()

if vd.View.Name == exporterFailureView {
e.exporterErrorQueue = append(e.exporterErrorQueue, vd)
}
}

// rotate function could rotate the error logs that expired the time interval
func (e *ecsHealthCheckExporter) rotate(interval time.Duration) {
e.mu.Lock()
defer e.mu.Unlock()

viewNum := len(e.exporterErrorQueue)
currentTime := time.Now()
for i := 0; i < viewNum; i++ {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it is managing a sliding window so index values could be stored inside the struct, improving performance.

vd := e.exporterErrorQueue[0]
if vd.Start.Add(interval).After(currentTime) {
e.exporterErrorQueue = append(e.exporterErrorQueue, vd)
}
e.exporterErrorQueue = e.exporterErrorQueue[1:]
}
}
61 changes: 61 additions & 0 deletions extension/awsecshealthcheckextension/exporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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://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 awsecshealthcheckextension

import (
"testing"
"time"

"gotest.tools/v3/assert"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could i ask you https://pkg.go.dev/github.com/stretchr/testify/assert instead? It heavily used within the project and keeps testing patterns the same.


"go.opencensus.io/stats/view"
)

func TestECSHealthCheckExporter_ExportView(t *testing.T) {
exporter := &ecsHealthCheckExporter{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could I ask you to us t.Parallel() in tests?

Would allow to speed up testing throughout the project 🙏🏽

newView := view.View{Name: exporterFailureView}
vd := &view.Data{
View: &newView,
Start: time.Time{},
End: time.Time{},
Rows: nil,
}
exporter.ExportView(vd)
assert.Equal(t, 1, len(exporter.exporterErrorQueue))
}

func TestECSHealthCheckExporter_rotate(t *testing.T) {
exporter := &ecsHealthCheckExporter{}
currentTime := time.Now()
time1 := currentTime.Add(-10 * time.Minute)
time2 := currentTime.Add(-3 * time.Minute)
newView := view.View{Name: exporterFailureView}
vd1 := &view.Data{
View: &newView,
Start: time1,
End: currentTime,
Rows: nil,
}
vd2 := &view.Data{
View: &newView,
Start: time2,
End: currentTime,
Rows: nil,
}
exporter.ExportView(vd1)
exporter.ExportView(vd2)
exporter.rotate(5 * time.Minute)
assert.Equal(t, 1, len(exporter.exporterErrorQueue))
}
Loading