@@ -12,6 +12,8 @@ import (
1212 "github.com/fluent/fluent-operator/v2/apis/fluentd/v1alpha1/plugins/output"
1313 "github.com/fluent/fluent-operator/v2/apis/fluentd/v1alpha1/plugins/params"
1414 fluentdRouter "github.com/fluent/fluent-operator/v2/pkg/fluentd/router"
15+
16+ "github.com/fluent/fluent-operator/v2/pkg/constants"
1517)
1618
1719// +kubebuilder:object:generate=false
@@ -30,6 +32,7 @@ type Renderer interface {
3032type PluginResources struct {
3133 InputPlugins []params.PluginStore
3234 MainRouterPlugins params.PluginStore
35+ MonitoringFilters []params.PluginStore
3336 LabelPluginResources []params.PluginStore
3437}
3538
@@ -45,13 +48,39 @@ type CfgResources struct {
4548}
4649
4750// NewGlobalPluginResources represents a combined global fluentd resources
48- func NewGlobalPluginResources (globalId string ) * PluginResources {
51+ func NewGlobalPluginResources (globalId string , enablePrometheusMonitoring bool , metricsPort * int32 , metricsBind * string ) * PluginResources {
4952 globalMainRouter := fluentdRouter .NewGlobalRouter (globalId )
50- return & PluginResources {
53+ pluginResources := & PluginResources {
5154 InputPlugins : make ([]params.PluginStore , 0 ),
5255 MainRouterPlugins : * globalMainRouter ,
56+ MonitoringFilters : make ([]params.PluginStore , 0 ),
5357 LabelPluginResources : make ([]params.PluginStore , 0 ),
5458 }
59+ if enablePrometheusMonitoring {
60+ incomingMonitoringFilter := fluentdRouter .NewIncomingMonitoringFilter ()
61+ outgoingMonitoringMatch := fluentdRouter .NewOutgoingMonitoringMatch ()
62+
63+ var determinedMetricsPort int32
64+ if metricsPort == nil {
65+ determinedMetricsPort = constants .DefaultMetricsPort
66+ } else {
67+ determinedMetricsPort = * metricsPort
68+ }
69+ var determinedMetricsBind string
70+ if metricsBind == nil {
71+ determinedMetricsBind = constants .DefaultBind
72+ } else {
73+ determinedMetricsBind = * metricsBind
74+ }
75+
76+ pluginResources .MonitoringFilters = append (pluginResources .MonitoringFilters , * incomingMonitoringFilter , * outgoingMonitoringMatch )
77+
78+ outputSources := fluentdRouter .NewMetricsExposeSources (determinedMetricsPort , determinedMetricsBind )
79+ for _ , s := range outputSources {
80+ pluginResources .MonitoringFilters = append (pluginResources .MonitoringFilters , * s )
81+ }
82+ }
83+ return pluginResources
5584}
5685
5786func NewCfgResources () * CfgResources {
@@ -237,7 +266,7 @@ func (pgr *PluginResources) WithCfgResources(cfgRouteLabel string, r *CfgResourc
237266 }
238267
239268 cfgLabelPlugin := params .NewPluginStore ("label" )
240- cfgLabelPlugin .InsertPairs ( "tag" , cfgRouteLabel )
269+ cfgLabelPlugin .Tag = cfgRouteLabel
241270
242271 // insert filter plugins of this fluentd config
243272 for _ , filter := range r .FilterPlugins {
@@ -255,7 +284,7 @@ func (pgr *PluginResources) WithCfgResources(cfgRouteLabel string, r *CfgResourc
255284 return nil
256285}
257286
258- func (pgr * PluginResources ) RenderMainConfig (enableMultiWorkers bool ) (string , error ) {
287+ func (pgr * PluginResources ) RenderMainConfig (enableMultiWorkers bool , enablePrometheusMetrics bool ) (string , error ) {
259288 if len (pgr .InputPlugins ) == 0 && len (pgr .LabelPluginResources ) == 0 {
260289 return "" , fmt .Errorf ("no plugins detect" )
261290 }
@@ -271,6 +300,17 @@ func (pgr *PluginResources) RenderMainConfig(enableMultiWorkers bool) (string, e
271300 buf .WriteString (pluginStore .String ())
272301 }
273302
303+ // sort monitoring plugins
304+ if enablePrometheusMetrics {
305+ monitoringPlugins := ByHashcode (pgr .MonitoringFilters )
306+ for _ , pluginStore := range monitoringPlugins {
307+ if enableMultiWorkers {
308+ pluginStore .SetIgnorePath ()
309+ }
310+ buf .WriteString (pluginStore .String ())
311+ }
312+ }
313+
274314 // sort main routers
275315 childRouters := ByRouteLabelsPointers (pgr .MainRouterPlugins .Childs )
276316 pgr .MainRouterPlugins .Childs = childRouters
0 commit comments