-
Notifications
You must be signed in to change notification settings - Fork 487
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
Cleanup after the Otel upgrade #4359
Changes from all commits
0199d8b
9dc069b
6434e90
d4377d4
d06c667
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 |
---|---|---|
|
@@ -2,10 +2,13 @@ | |
package headers | ||
|
||
import ( | ||
"encoding" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/grafana/agent/component" | ||
"github.com/grafana/agent/component/otelcol/auth" | ||
"github.com/grafana/agent/pkg/river" | ||
"github.com/grafana/agent/pkg/river/rivertypes" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/headerssetterextension" | ||
otelcomponent "go.opentelemetry.io/collector/component" | ||
|
@@ -40,6 +43,11 @@ func (args Arguments) Convert() (otelcomponent.Config, error) { | |
Key: &h.Key, | ||
} | ||
|
||
err := h.Action.Convert(&upstreamHeader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if h.Value != nil { | ||
upstreamHeader.Value = &h.Value.Value | ||
} | ||
|
@@ -65,15 +73,87 @@ func (args Arguments) Exporters() map[otelcomponent.DataType]map[otelcomponent.I | |
return nil | ||
} | ||
|
||
type Action string | ||
|
||
const ( | ||
ActionInsert Action = "insert" | ||
ActionUpdate Action = "update" | ||
ActionUpsert Action = "upsert" | ||
ActionDelete Action = "delete" | ||
) | ||
|
||
var ( | ||
_ river.Validator = (*Action)(nil) | ||
_ encoding.TextUnmarshaler = (*Action)(nil) | ||
) | ||
|
||
// Validate implements river.Validator. | ||
func (a *Action) Validate() error { | ||
switch *a { | ||
case ActionInsert, ActionUpdate, ActionUpsert, ActionDelete: | ||
// This is a valid value, do not error | ||
default: | ||
return fmt.Errorf("action is set to an invalid value of %q", *a) | ||
} | ||
return nil | ||
} | ||
|
||
// Convert the River type to the Otel type. | ||
// TODO: When headerssetterextension.actionValue is made external, | ||
// remove the input parameter and make this output the Otel type. | ||
func (a *Action) Convert(hc *headerssetterextension.HeaderConfig) error { | ||
switch *a { | ||
case ActionInsert: | ||
hc.Action = headerssetterextension.INSERT | ||
case ActionUpdate: | ||
hc.Action = headerssetterextension.UPDATE | ||
case ActionUpsert: | ||
hc.Action = headerssetterextension.UPSERT | ||
case ActionDelete: | ||
hc.Action = headerssetterextension.DELETE | ||
default: | ||
return fmt.Errorf("action is set to an invalid value of %q", *a) | ||
} | ||
return nil | ||
} | ||
|
||
func (a *Action) UnmarshalText(text []byte) error { | ||
str := Action(strings.ToLower(string(text))) | ||
switch str { | ||
case ActionInsert, ActionUpdate, ActionUpsert, ActionDelete: | ||
*a = str | ||
return nil | ||
default: | ||
return fmt.Errorf("unknown action %v", str) | ||
} | ||
} | ||
Comment on lines
+120
to
+129
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. Does this need to exist or is the Validate method enough? 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.
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.
Are you sure? Action is just a string. 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. Ah, I see, there's text transformation going on when unmarshaling to make it case insensitive. OK, that adds some validity to UnmarshalText but it's still not strictly necessary. I'm ok with keeping it in both cases (including the tail sampling case being discussed below) 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. The text transformation isn't the main reason. If I don't implement
The text transformation was simply so that we match what the Collector does. TBH I'm not 100% sure if I like it or if we should just stick to some Agent convention, but I wasn't sure if we have a convention and thought this way it'll be easier to migrate Collector configs to the Agent. |
||
|
||
// Header is an individual Header to send along with requests. | ||
type Header struct { | ||
Key string `river:"key,attr"` | ||
Value *rivertypes.OptionalSecret `river:"value,attr,optional"` | ||
FromContext *string `river:"from_context,attr,optional"` | ||
Action Action `river:"action,attr,optional"` | ||
} | ||
|
||
var _ river.Defaulter = &Header{} | ||
|
||
var DefaultHeader = Header{ | ||
Action: ActionUpsert, | ||
} | ||
|
||
// SetToDefault implements river.Defaulter. | ||
func (h *Header) SetToDefault() { | ||
*h = DefaultHeader | ||
} | ||
|
||
// Validate implements river.Validator. | ||
func (h *Header) Validate() error { | ||
err := h.Action.Validate() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
switch { | ||
case h.Key == "": | ||
return fmt.Errorf("key must be set to a non-empty string") | ||
|
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.
Note that all breaking changes need to be documented with details and potential migration instructions in the upgrade guide.
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.
Thank you, I forgot to update the guide in #3858. I added three entries to it now!