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

otelcol-config: support ephemeral for OTRM agents #1667

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions pkg/tools/otelcol-config/ephemeral.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package main

import "errors"
import (
"bytes"
"fmt"

"github.com/mikefarah/yq/v4/pkg/yqlib"
)

// EnableEphemeralAction links the available ephemeral configuration to conf.d
func EnableEphemeralAction(ctx *actionContext) error {
Expand All @@ -9,7 +14,7 @@ func EnableEphemeralAction(ctx *actionContext) error {
return err
}
if conf.SumologicRemote != nil {
return errors.New("enable-ephemeral not supported for remote-controlled collectors")
return writeEphemeralRemote(ctx, conf.SumologicRemote, ".extensions.sumologic.ephemeral = true")
}
return ctx.LinkEphemeral()
}
Expand All @@ -21,7 +26,34 @@ func DisableEphemeralAction(ctx *actionContext) error {
return err
}
if conf.SumologicRemote != nil {
return errors.New("disable-ephemeral not supported for remote-controlled collectors")
return writeEphemeralRemote(ctx, conf.SumologicRemote, ".extensions.sumologic.ephemeral = false")
}
return ctx.UnlinkEphemeral()
}

func writeEphemeralRemote(ctx *actionContext, doc []byte, expr string) error {
encoder := yqlib.YamlFormat.EncoderFactory()
decoder := yqlib.YamlFormat.DecoderFactory()
eval := yqlib.NewStringEvaluator()
if len(doc) == 0 {
// --null-input
buf := new(bytes.Buffer)
writer := yqlib.NewSinglePrinterWriter(buf)
printer := yqlib.NewPrinter(encoder, writer)
err := yqlib.NewStreamEvaluator().EvaluateNew(expr, printer)
if err != nil {
return fmt.Errorf("error writing sumologic-remote.yaml: %s", err)
}
doc = buf.Bytes()
} else {
result, err := eval.EvaluateAll(expr, string(doc), encoder, decoder)
if err != nil {
return fmt.Errorf("error evaluating yq expression: %s", err)
}
doc = []byte(result)
}
if _, err := ctx.WriteSumologicRemote(doc); err != nil {
return fmt.Errorf("error writing sumologic-remote.yaml: %s", err)
}
return nil
}
14 changes: 8 additions & 6 deletions pkg/tools/otelcol-config/ephemeral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,27 @@ func TestEnableEphemeralActionRemoteControlled(t *testing.T) {
ctx := &actionContext{
ConfigDir: fstest.MapFS{
SumologicRemoteDotYaml: &fstest.MapFile{
Data: []byte(`{"extensions":{"opamp":{"enabled":true}}}`),
Data: []byte("extensions:\n opamp:\n enabled: true\n"),
},
},
WriteSumologicRemote: newTestWriter([]byte("extensions:\n opamp:\n enabled: true\n sumologic:\n ephemeral: true\n")).Write,
}
if err := EnableEphemeralAction(ctx); err == nil {
t.Fatal("expected non-nil error")
if err := EnableEphemeralAction(ctx); err != nil {
t.Fatal(err)
}
}

func TestDisableEphemeralActionRemoteControlled(t *testing.T) {
ctx := &actionContext{
ConfigDir: fstest.MapFS{
SumologicRemoteDotYaml: &fstest.MapFile{
Data: []byte(`{"extensions":{"opamp":{"enabled":true}}}`),
Data: []byte("extensions:\n opamp:\n enabled: true\n"),
},
},
WriteSumologicRemote: newTestWriter([]byte("extensions:\n opamp:\n enabled: true\n sumologic:\n ephemeral: false\n")).Write,
}
if err := DisableEphemeralAction(ctx); err == nil {
t.Fatal("expected non-nil error")
if err := DisableEphemeralAction(ctx); err != nil {
t.Fatal(err)
}
}

Expand Down
Loading