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

[extension/opamp] Add support for polling interval in HTTP client #34811

Merged
merged 10 commits into from
Sep 3, 2024
27 changes: 27 additions & 0 deletions .chloggen/opampextension_add_http_polling_interval.yaml
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: opampextension

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Adds the ability to configure the polling interval for the HTTP client.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [34749]

# (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: []
1 change: 1 addition & 0 deletions extension/opampextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The following settings are optional:
- `ws`: The OpAMP websocket transport settings.
- `tls`: TLS settings.
- `headers`: HTTP headers to set.
- `polling_interval`: The interval at which the extension will poll the server. Only applicable to HTTP client. Defaults to 30s.
matej-g marked this conversation as resolved.
Show resolved Hide resolved
- `instance_uid`: A UUIDv7 formatted as a 36 character string in canonical
representation. Auto-generated on start if missing. Setting this ensures the
instance UID remains constant across process restarts.
Expand Down
21 changes: 19 additions & 2 deletions extension/opampextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ type commonFields struct {
Headers map[string]configopaque.String `mapstructure:"headers,omitempty"`
}

type httpFields struct {
commonFields `mapstructure:",squash"`

PollingInterval time.Duration `mapstructure:"polling_interval"`
}
matej-g marked this conversation as resolved.
Show resolved Hide resolved

// OpAMPServer contains the OpAMP transport configuration.
type OpAMPServer struct {
WS *commonFields `mapstructure:"ws,omitempty"`
HTTP *commonFields `mapstructure:"http,omitempty"`
HTTP *httpFields `mapstructure:"http,omitempty"`
}

func (c *commonFields) Scheme() string {
Expand All @@ -93,7 +99,10 @@ func (s OpAMPServer) GetClient(logger *zap.Logger) client.OpAMPClient {
if s.WS != nil {
return client.NewWebSocket(newLoggerFromZap(logger.With(zap.String("client", "ws"))))
}
return client.NewHTTP(newLoggerFromZap(logger.With(zap.String("client", "http"))))

httpClient := client.NewHTTP(newLoggerFromZap(logger.With(zap.String("client", "http"))))
httpClient.SetPollingInterval(s.GetPollingInterval())
return httpClient
}

func (s OpAMPServer) GetHeaders() map[string]configopaque.String {
Expand Down Expand Up @@ -123,6 +132,14 @@ func (s OpAMPServer) GetEndpoint() string {
return ""
}

func (s OpAMPServer) GetPollingInterval() time.Duration {
if s.HTTP != nil {
return s.HTTP.PollingInterval
}
matej-g marked this conversation as resolved.
Show resolved Hide resolved

return 0
}

// Validate checks if the extension configuration is valid
func (cfg *Config) Validate() error {
switch {
Expand Down
39 changes: 24 additions & 15 deletions extension/opampextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ func TestUnmarshalHttpConfig(t *testing.T) {
assert.Equal(t,
&Config{
Server: &OpAMPServer{
HTTP: &commonFields{
Endpoint: "https://127.0.0.1:4320/v1/opamp",
HTTP: &httpFields{
commonFields: commonFields{
Endpoint: "https://127.0.0.1:4320/v1/opamp",
},
PollingInterval: 1 * time.Minute,
},
},
InstanceUID: "01BX5ZZKBKACTAV9WEVGEMMVRZ",
Expand Down Expand Up @@ -115,13 +118,15 @@ func TestConfig_Getters(t *testing.T) {
name: "HTTP valid endpoint and valid instance id",
fields: fields{
Server: &OpAMPServer{
HTTP: &commonFields{
Endpoint: "https://127.0.0.1:4320/v1/opamp",
Headers: map[string]configopaque.String{
"test": configopaque.String("test"),
},
TLSSetting: configtls.ClientConfig{
Insecure: true,
HTTP: &httpFields{
commonFields: commonFields{
Endpoint: "https://127.0.0.1:4320/v1/opamp",
Headers: map[string]configopaque.String{
"test": configopaque.String("test"),
},
TLSSetting: configtls.ClientConfig{
Insecure: true,
},
},
},
},
Expand Down Expand Up @@ -194,7 +199,7 @@ func TestConfig_Validate(t *testing.T) {
name: "HTTP must have endpoint",
fields: fields{
Server: &OpAMPServer{
HTTP: &commonFields{},
HTTP: &httpFields{},
},
},
wantErr: func(t assert.TestingT, err error, _ ...any) bool {
Expand All @@ -205,8 +210,10 @@ func TestConfig_Validate(t *testing.T) {
name: "HTTP valid endpoint and invalid instance id",
fields: fields{
Server: &OpAMPServer{
HTTP: &commonFields{
Endpoint: "https://127.0.0.1:4320/v1/opamp",
HTTP: &httpFields{
commonFields: commonFields{
Endpoint: "https://127.0.0.1:4320/v1/opamp",
},
},
},
InstanceUID: "01BX5ZZKBKACTAV9WEVGEMMVRZFAIL",
Expand All @@ -219,8 +226,10 @@ func TestConfig_Validate(t *testing.T) {
name: "HTTP valid endpoint and valid instance id",
fields: fields{
Server: &OpAMPServer{
HTTP: &commonFields{
Endpoint: "https://127.0.0.1:4320/v1/opamp",
HTTP: &httpFields{
commonFields: commonFields{
Endpoint: "https://127.0.0.1:4320/v1/opamp",
},
},
},
InstanceUID: "01BX5ZZKBKACTAV9WEVGEMMVRZ",
Expand All @@ -241,7 +250,7 @@ func TestConfig_Validate(t *testing.T) {
fields: fields{
Server: &OpAMPServer{
WS: &commonFields{},
HTTP: &commonFields{},
HTTP: &httpFields{},
},
},
wantErr: func(t assert.TestingT, err error, _ ...any) bool {
Expand Down
1 change: 1 addition & 0 deletions extension/opampextension/testdata/config_http.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
server:
http:
endpoint: https://127.0.0.1:4320/v1/opamp
polling_interval: 1m
instance_uid: 01BX5ZZKBKACTAV9WEVGEMMVRZ