-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[extension/healthcheckv2] Manage extension and subcomponents (#34144)
**Description:** <Describe what has changed.> The PR is the sixth and final in a series to decompose #30673 into more manageable pieces for review. This PR adds code to wire up the extension as a status watcher and to manage the HTTP and gRPC services as independent subcomponents. After this merges the extension will be usable for evaluation purposes and we will likely make some refinements based on feedback from early adopters. **Link to tracking Issue:** #26661 **Testing:** Units / manual **Documentation:** Comments, etc.
- Loading branch information
Showing
4 changed files
with
333 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: 'enhancement' | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: extension/healthcheckv2 | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add extension/subcomponent management logic. | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [26661] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
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,135 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package healthcheckv2extension | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/confmap/confmaptest" | ||
"go.opentelemetry.io/collector/extension/extensiontest" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/healthcheckv2extension/internal/status" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/healthcheckv2extension/internal/testhelpers" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil" | ||
) | ||
|
||
func TestComponentStatus(t *testing.T) { | ||
cfg := createDefaultConfig().(*Config) | ||
cfg.HTTPConfig.Endpoint = testutil.GetAvailableLocalAddress(t) | ||
cfg.GRPCConfig.NetAddr.Endpoint = testutil.GetAvailableLocalAddress(t) | ||
cfg.UseV2 = true | ||
ext := newExtension(context.Background(), *cfg, extensiontest.NewNopSettings()) | ||
|
||
// Status before Start will be StatusNone | ||
st, ok := ext.aggregator.AggregateStatus(status.ScopeAll, status.Concise) | ||
require.True(t, ok) | ||
assert.Equal(t, st.Status(), component.StatusNone) | ||
|
||
require.NoError(t, ext.Start(context.Background(), componenttest.NewNopHost())) | ||
|
||
traces := testhelpers.NewPipelineMetadata("traces") | ||
|
||
// StatusStarting will be sent immediately. | ||
for _, id := range traces.InstanceIDs() { | ||
ext.ComponentStatusChanged(id, component.NewStatusEvent(component.StatusStarting)) | ||
} | ||
|
||
// StatusOK will be queued until the PipelineWatcher Ready method is called. | ||
for _, id := range traces.InstanceIDs() { | ||
ext.ComponentStatusChanged(id, component.NewStatusEvent(component.StatusOK)) | ||
} | ||
|
||
// Note the use of assert.Eventually here and throughout this test is because | ||
// status events are processed asynchronously in the background. | ||
assert.Eventually(t, func() bool { | ||
st, ok = ext.aggregator.AggregateStatus(status.ScopeAll, status.Concise) | ||
require.True(t, ok) | ||
return st.Status() == component.StatusStarting | ||
}, time.Second, 10*time.Millisecond) | ||
|
||
require.NoError(t, ext.Ready()) | ||
|
||
assert.Eventually(t, func() bool { | ||
st, ok = ext.aggregator.AggregateStatus(status.ScopeAll, status.Concise) | ||
require.True(t, ok) | ||
return st.Status() == component.StatusOK | ||
}, time.Second, 10*time.Millisecond) | ||
|
||
// StatusStopping will be sent immediately. | ||
for _, id := range traces.InstanceIDs() { | ||
ext.ComponentStatusChanged(id, component.NewStatusEvent(component.StatusStopping)) | ||
} | ||
|
||
assert.Eventually(t, func() bool { | ||
st, ok = ext.aggregator.AggregateStatus(status.ScopeAll, status.Concise) | ||
require.True(t, ok) | ||
return st.Status() == component.StatusStopping | ||
}, time.Second, 10*time.Millisecond) | ||
|
||
require.NoError(t, ext.NotReady()) | ||
require.NoError(t, ext.Shutdown(context.Background())) | ||
|
||
// Events sent after shutdown will be discarded | ||
for _, id := range traces.InstanceIDs() { | ||
ext.ComponentStatusChanged(id, component.NewStatusEvent(component.StatusStopped)) | ||
} | ||
|
||
st, ok = ext.aggregator.AggregateStatus(status.ScopeAll, status.Concise) | ||
require.True(t, ok) | ||
assert.Equal(t, component.StatusStopping, st.Status()) | ||
} | ||
|
||
func TestNotifyConfig(t *testing.T) { | ||
confMap, err := confmaptest.LoadConf( | ||
filepath.Join("internal", "http", "testdata", "config.yaml"), | ||
) | ||
require.NoError(t, err) | ||
confJSON, err := os.ReadFile( | ||
filepath.Clean(filepath.Join("internal", "http", "testdata", "config.json")), | ||
) | ||
require.NoError(t, err) | ||
|
||
endpoint := testutil.GetAvailableLocalAddress(t) | ||
|
||
cfg := createDefaultConfig().(*Config) | ||
cfg.UseV2 = true | ||
cfg.HTTPConfig.Endpoint = endpoint | ||
cfg.HTTPConfig.Config.Enabled = true | ||
cfg.HTTPConfig.Config.Path = "/config" | ||
|
||
ext := newExtension(context.Background(), *cfg, extensiontest.NewNopSettings()) | ||
|
||
require.NoError(t, ext.Start(context.Background(), componenttest.NewNopHost())) | ||
t.Cleanup(func() { require.NoError(t, ext.Shutdown(context.Background())) }) | ||
|
||
client := &http.Client{} | ||
url := fmt.Sprintf("http://%s/config", endpoint) | ||
|
||
var resp *http.Response | ||
|
||
resp, err = client.Get(url) | ||
require.NoError(t, err) | ||
assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) | ||
|
||
require.NoError(t, ext.NotifyConfig(context.Background(), confMap)) | ||
|
||
resp, err = client.Get(url) | ||
require.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
require.NoError(t, err) | ||
assert.Equal(t, confJSON, body) | ||
} |
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