-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wire up for eventhandler integration in the static to flow converter (#…
- Loading branch information
1 parent
900a035
commit cce5b03
Showing
8 changed files
with
160 additions
and
6 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
98 changes: 98 additions & 0 deletions
98
converter/internal/staticconvert/internal/build/eventhandler.go
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,98 @@ | ||
package build | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/grafana/agent/component/common/loki" | ||
flow_relabel "github.com/grafana/agent/component/common/relabel" | ||
"github.com/grafana/agent/component/loki/relabel" | ||
"github.com/grafana/agent/component/loki/source/kubernetes_events" | ||
"github.com/grafana/agent/converter/diag" | ||
"github.com/grafana/agent/converter/internal/common" | ||
eventhandler_v2 "github.com/grafana/agent/pkg/integrations/v2/eventhandler" | ||
"github.com/grafana/river/scanner" | ||
) | ||
|
||
func (b *IntegrationsConfigBuilder) appendEventHandlerV2(config *eventhandler_v2.Config) { | ||
compLabel, err := scanner.SanitizeIdentifier(b.formatJobName(config.Name(), nil)) | ||
if err != nil { | ||
b.diags.Add(diag.SeverityLevelCritical, fmt.Sprintf("failed to sanitize job name: %s", err)) | ||
} | ||
|
||
b.diags.AddAll(common.ValidateSupported(common.NotDeepEquals, config.SendTimeout, eventhandler_v2.DefaultConfig.SendTimeout, "eventhandler send_timeout", "this field is not configurable in flow mode")) | ||
b.diags.AddAll(common.ValidateSupported(common.NotDeepEquals, config.CachePath, eventhandler_v2.DefaultConfig.CachePath, "eventhandler cache_path", "this field is not configurable in flow mode")) | ||
b.diags.AddAll(common.ValidateSupported(common.NotDeepEquals, config.InformerResync, eventhandler_v2.DefaultConfig.InformerResync, "eventhandler informer_resync", "this field is not configurable in flow mode")) | ||
b.diags.AddAll(common.ValidateSupported(common.NotDeepEquals, config.FlushInterval, eventhandler_v2.DefaultConfig.FlushInterval, "eventhandler flush_interval", "this field is not configurable in flow mode")) | ||
|
||
receiver := getLogsReceiver(config) | ||
if len(config.ExtraLabels) > 0 { | ||
receiver = b.injectExtraLabels(config, receiver, compLabel) | ||
} | ||
|
||
args := toEventHandlerV2(config, receiver) | ||
|
||
b.f.Body().AppendBlock(common.NewBlockWithOverride( | ||
[]string{"loki", "source", "kubernetes_events"}, | ||
compLabel, | ||
args, | ||
)) | ||
} | ||
|
||
func (b *IntegrationsConfigBuilder) injectExtraLabels(config *eventhandler_v2.Config, receiver common.ConvertLogsReceiver, compLabel string) common.ConvertLogsReceiver { | ||
var relabelConfigs []*flow_relabel.Config | ||
for _, extraLabel := range config.ExtraLabels { | ||
defaultConfig := flow_relabel.DefaultRelabelConfig | ||
relabelConfig := &defaultConfig | ||
relabelConfig.SourceLabels = []string{"__address__"} | ||
relabelConfig.TargetLabel = extraLabel.Name | ||
relabelConfig.Replacement = extraLabel.Value | ||
|
||
relabelConfigs = append(relabelConfigs, relabelConfig) | ||
} | ||
|
||
relabelArgs := relabel.Arguments{ | ||
ForwardTo: []loki.LogsReceiver{receiver}, | ||
RelabelConfigs: relabelConfigs, | ||
MaxCacheSize: relabel.DefaultArguments.MaxCacheSize, | ||
} | ||
|
||
b.f.Body().AppendBlock(common.NewBlockWithOverride( | ||
[]string{"loki", "relabel"}, | ||
compLabel, | ||
relabelArgs, | ||
)) | ||
|
||
return common.ConvertLogsReceiver{ | ||
Expr: fmt.Sprintf("loki.relabel.%s.receiver", compLabel), | ||
} | ||
} | ||
|
||
func getLogsReceiver(config *eventhandler_v2.Config) common.ConvertLogsReceiver { | ||
logsReceiver := common.ConvertLogsReceiver{} | ||
if config.LogsInstance != "" { | ||
compLabel, err := scanner.SanitizeIdentifier("logs_" + config.LogsInstance) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to sanitize job name: %s", err)) | ||
} | ||
|
||
logsReceiver.Expr = fmt.Sprintf("loki.write.%s.receiver", compLabel) | ||
} | ||
|
||
return logsReceiver | ||
} | ||
|
||
func toEventHandlerV2(config *eventhandler_v2.Config, receiver common.ConvertLogsReceiver) *kubernetes_events.Arguments { | ||
defaultOverrides := kubernetes_events.DefaultArguments | ||
defaultOverrides.Client.KubeConfig = config.KubeconfigPath | ||
if config.Namespace != "" { | ||
defaultOverrides.Namespaces = []string{config.Namespace} | ||
} | ||
|
||
return &kubernetes_events.Arguments{ | ||
ForwardTo: []loki.LogsReceiver{receiver}, | ||
JobName: kubernetes_events.DefaultArguments.JobName, | ||
Namespaces: defaultOverrides.Namespaces, | ||
LogFormat: config.LogFormat, | ||
Client: defaultOverrides.Client, | ||
} | ||
} |
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
5 changes: 4 additions & 1 deletion
5
converter/internal/staticconvert/testdata-v2/unsupported.diags
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
(Error) The converter does not support converting the provided eventhandler send_timeout config: this field is not configurable in flow mode | ||
(Error) The converter does not support converting the provided eventhandler cache_path config: this field is not configurable in flow mode | ||
(Error) The converter does not support converting the provided eventhandler informer_resync config: this field is not configurable in flow mode | ||
(Error) The converter does not support converting the provided eventhandler flush_interval config: this field is not configurable in flow mode | ||
(Warning) Please review your agent command line flags and ensure they are set in your Flow mode config file where necessary. | ||
(Error) The converter does not support converting the provided eventhandler integration. | ||
(Error) The converter does not support converting the provided app_agent_receiver traces_instance config. |
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