forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new health check feature for collector pipeline (open-telemetry#5643
- Loading branch information
Showing
13 changed files
with
393 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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 | ||
// | ||
// 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 healthcheckextension | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"go.opencensus.io/stats/view" | ||
) | ||
|
||
const ( | ||
exporterFailureView = "exporter/send_failed_requests" | ||
) | ||
|
||
// healthCheckExporter is a struct implement the exporter interface in open census that could export metrics | ||
type healthCheckExporter struct { | ||
mu sync.Mutex | ||
exporterFailureQueue []*view.Data | ||
} | ||
|
||
func newHealthCheckExporter() *healthCheckExporter { | ||
return &healthCheckExporter{} | ||
} | ||
|
||
// ExportView function could export the failure view to the queue | ||
func (e *healthCheckExporter) ExportView(vd *view.Data) { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
if vd.View.Name == exporterFailureView { | ||
e.exporterFailureQueue = append(e.exporterFailureQueue, vd) | ||
} | ||
} | ||
|
||
func (e *healthCheckExporter) checkHealthStatus(exporterFailureThreshold int) bool { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
return exporterFailureThreshold >= len(e.exporterFailureQueue) | ||
} | ||
|
||
// rotate function could rotate the error logs that expired the time interval | ||
func (e *healthCheckExporter) rotate(interval time.Duration) { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
viewNum := len(e.exporterFailureQueue) | ||
currentTime := time.Now() | ||
for i := 0; i < viewNum; i++ { | ||
vd := e.exporterFailureQueue[0] | ||
if vd.Start.Add(interval).After(currentTime) { | ||
e.exporterFailureQueue = append(e.exporterFailureQueue, vd) | ||
} | ||
e.exporterFailureQueue = e.exporterFailureQueue[1:] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// | ||
// 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 healthcheckextension | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opencensus.io/stats/view" | ||
) | ||
|
||
func TestHealthCheckExporter_ExportView(t *testing.T) { | ||
exporter := &healthCheckExporter{} | ||
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.exporterFailureQueue)) | ||
} | ||
|
||
func TestHealthCheckExporter_rotate(t *testing.T) { | ||
exporter := &healthCheckExporter{} | ||
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) | ||
assert.Equal(t, 2, len(exporter.exporterFailureQueue)) | ||
exporter.rotate(5 * time.Minute) | ||
assert.Equal(t, 1, len(exporter.exporterFailureQueue)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.