-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from all commits
45e83e8
63eae1a
d4aa797
9c18d80
74ab2cf
e66f7f1
fc92a50
caf27b9
3d48604
b48b664
4898ab3
fc749e2
973e63a
fa5e409
cbfc075
ddf3c77
c192310
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include ../../Makefile.Common |
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 | ||
``` |
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 | ||
} |
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) | ||
|
||
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]) | ||
} |
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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++ { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:] | ||||||
} | ||||||
} |
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could I ask you to us 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)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this fails, it would create a flaky test with a nil pointer exception