Skip to content

Commit 04f8a14

Browse files
authored
Move prometheus server to main (#399)
* move prom server * moved prometheus server creation to main * removed prometheus port from tests * explicitly ignored return value of Shutdown * allow multiple instances of encode prom by ignoring duplicated metric name * fix handling of port in confgenerator * addressed review comments
1 parent 9740aa3 commit 04f8a14

File tree

18 files changed

+102
-116
lines changed

18 files changed

+102
-116
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ Usage:
4949
flowlogs-pipeline [flags]
5050

5151
Flags:
52-
--config string config file (default is $HOME/.flowlogs-pipeline)
53-
--health.address string Health server address (default "0.0.0.0")
54-
--health.port string Health server port (default "8080")
55-
-h, --help help for flowlogs-pipeline
56-
--log-level string Log level: debug, info, warning, error (default "error")
57-
--metrics-settings string json for global metrics settings
58-
--parameters string json of config file parameters field
59-
--pipeline string json of config file pipeline field
60-
--profile.port int Go pprof tool port (default: disabled)
52+
--config string config file (default is $HOME/.flowlogs-pipeline)
53+
--health.address string Health server address (default "0.0.0.0")
54+
--health.port string Health server port (default "8080")
55+
-h, --help help for flowlogs-pipeline
56+
--log-level string Log level: debug, info, warning, error (default "error")
57+
--metricsSettings string json for global metrics settings
58+
--parameters string json of config file parameters field
59+
--pipeline string json of config file pipeline field
60+
--profile.port int Go pprof tool port (default: disabled)
6161
```
6262
<!---END-AUTO-flowlogs-pipeline_help--->
6363

cmd/flowlogs-pipeline/main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package main
1919

2020
import (
21+
"context"
22+
"crypto/tls"
2123
"encoding/json"
2224
"fmt"
2325
"net/http"
@@ -142,7 +144,7 @@ func initFlags() {
142144
rootCmd.PersistentFlags().IntVar(&opts.Profile.Port, "profile.port", 0, "Go pprof tool port (default: disabled)")
143145
rootCmd.PersistentFlags().StringVar(&opts.PipeLine, "pipeline", "", "json of config file pipeline field")
144146
rootCmd.PersistentFlags().StringVar(&opts.Parameters, "parameters", "", "json of config file parameters field")
145-
rootCmd.PersistentFlags().StringVar(&opts.MetricsSettings, "metrics-settings", "", "json for global metrics settings")
147+
rootCmd.PersistentFlags().StringVar(&opts.MetricsSettings, "metricsSettings", "", "json for global metrics settings")
146148
}
147149

148150
func main() {
@@ -177,6 +179,20 @@ func run() {
177179
// Setup (threads) exit manager
178180
utils.SetupElegantExit()
179181

182+
// create prometheus server for operational metrics
183+
// if value of address is empty, then by default it will take 0.0.0.0
184+
addr := fmt.Sprintf("%s:%v", cfg.MetricsSettings.Address, cfg.MetricsSettings.Port)
185+
log.Infof("startServer: addr = %s", addr)
186+
promServer := &http.Server{
187+
Addr: addr,
188+
// TLS clients must use TLS 1.2 or higher
189+
TLSConfig: &tls.Config{
190+
MinVersion: tls.VersionTLS12,
191+
},
192+
}
193+
tlsConfig := cfg.MetricsSettings.TLS
194+
go utils.StartPromServer(tlsConfig, promServer, !cfg.MetricsSettings.NoPanic)
195+
180196
// Create new flows pipeline
181197
mainPipeline, err = pipeline.NewPipeline(&cfg)
182198
if err != nil {
@@ -198,6 +214,8 @@ func run() {
198214
// Starts the flows pipeline
199215
mainPipeline.Run()
200216

217+
_ = promServer.Shutdown(context.Background())
218+
201219
// Give all threads a chance to exit and then exit the process
202220
time.Sleep(time.Second)
203221
log.Debugf("exiting main run")

cmd/flowlogs-pipeline/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestPipelineConfigSetup(t *testing.T) {
5252

5353
js := `{
5454
"PipeLine": "[{\"name\":\"grpc\"},{\"follows\":\"grpc\",\"name\":\"enrich\"},{\"follows\":\"enrich\",\"name\":\"loki\"},{\"follows\":\"enrich\",\"name\":\"prometheus\"}]",
55-
"Parameters": "[{\"ingest\":{\"grpc\":{\"port\":2055},\"type\":\"grpc\"},\"name\":\"grpc\"},{\"name\":\"enrich\",\"transform\":{\"network\":{\"rules\":[{\"input\":\"SrcAddr\",\"output\":\"SrcK8S\",\"type\":\"add_kubernetes\"},{\"input\":\"DstAddr\",\"output\":\"DstK8S\",\"type\":\"add_kubernetes\"},{\"input\":\"DstPort\",\"output\":\"Service\",\"parameters\":\"Proto\",\"type\":\"add_service\"},{\"input\":\"SrcAddr\",\"output\":\"SrcSubnet\",\"parameters\":\"/16\",\"type\":\"add_subnet\"}]},\"type\":\"network\"}},{\"name\":\"loki\",\"write\":{\"loki\":{\"batchSize\":102400,\"batchWait\":\"1s\",\"clientConfig\":{\"follow_redirects\":false,\"proxy_url\":null,\"tls_config\":{\"insecure_skip_verify\":false}},\"labels\":[\"SrcK8S_Namespace\",\"SrcK8S_OwnerName\",\"DstK8S_Namespace\",\"DstK8S_OwnerName\",\"FlowDirection\"],\"maxBackoff\":\"5m0s\",\"maxRetries\":10,\"minBackoff\":\"1s\",\"staticLabels\":{\"app\":\"netobserv-flowcollector\"},\"tenantID\":\"netobserv\",\"timeout\":\"10s\",\"timestampLabel\":\"TimeFlowEndMs\",\"timestampScale\":\"1ms\",\"url\":\"http://loki.netobserv.svc:3100/\"},\"type\":\"loki\"}},{\"encode\":{\"prom\":{\"metrics\":[{\"buckets\":null,\"filter\":{\"key\":\"\",\"value\":\"\"},\"labels\":[\"Service\",\"SrcK8S_Namespace\"],\"name\":\"bandwidth_per_network_service_per_namespace\",\"type\":\"counter\",\"valueKey\":\"Bytes\"},{\"buckets\":null,\"filter\":{\"key\":\"\",\"value\":\"\"},\"labels\":[\"SrcSubnet\"],\"name\":\"bandwidth_per_source_subnet\",\"type\":\"counter\",\"valueKey\":\"Bytes\"},{\"buckets\":null,\"filter\":{\"key\":\"\",\"value\":\"\"},\"labels\":[\"Service\"],\"name\":\"network_service_total\",\"type\":\"counter\",\"valueKey\":\"\"}],\"port\":9102,\"prefix\":\"netobserv_\"},\"type\":\"prom\"},\"name\":\"prometheus\"}]",
55+
"Parameters": "[{\"ingest\":{\"grpc\":{\"port\":2055},\"type\":\"grpc\"},\"name\":\"grpc\"},{\"name\":\"enrich\",\"transform\":{\"network\":{\"rules\":[{\"input\":\"SrcAddr\",\"output\":\"SrcK8S\",\"type\":\"add_kubernetes\"},{\"input\":\"DstAddr\",\"output\":\"DstK8S\",\"type\":\"add_kubernetes\"},{\"input\":\"DstPort\",\"output\":\"Service\",\"parameters\":\"Proto\",\"type\":\"add_service\"},{\"input\":\"SrcAddr\",\"output\":\"SrcSubnet\",\"parameters\":\"/16\",\"type\":\"add_subnet\"}]},\"type\":\"network\"}},{\"name\":\"loki\",\"write\":{\"loki\":{\"batchSize\":102400,\"batchWait\":\"1s\",\"clientConfig\":{\"follow_redirects\":false,\"proxy_url\":null,\"tls_config\":{\"insecure_skip_verify\":false}},\"labels\":[\"SrcK8S_Namespace\",\"SrcK8S_OwnerName\",\"DstK8S_Namespace\",\"DstK8S_OwnerName\",\"FlowDirection\"],\"maxBackoff\":\"5m0s\",\"maxRetries\":10,\"minBackoff\":\"1s\",\"staticLabels\":{\"app\":\"netobserv-flowcollector\"},\"tenantID\":\"netobserv\",\"timeout\":\"10s\",\"timestampLabel\":\"TimeFlowEndMs\",\"timestampScale\":\"1ms\",\"url\":\"http://loki.netobserv.svc:3100/\"},\"type\":\"loki\"}},{\"encode\":{\"prom\":{\"metrics\":[{\"buckets\":null,\"filter\":{\"key\":\"\",\"value\":\"\"},\"labels\":[\"Service\",\"SrcK8S_Namespace\"],\"name\":\"bandwidth_per_network_service_per_namespace\",\"type\":\"counter\",\"valueKey\":\"Bytes\"},{\"buckets\":null,\"filter\":{\"key\":\"\",\"value\":\"\"},\"labels\":[\"SrcSubnet\"],\"name\":\"bandwidth_per_source_subnet\",\"type\":\"counter\",\"valueKey\":\"Bytes\"},{\"buckets\":null,\"filter\":{\"key\":\"\",\"value\":\"\"},\"labels\":[\"Service\"],\"name\":\"network_service_total\",\"type\":\"counter\",\"valueKey\":\"\"}],\"prefix\":\"netobserv_\"},\"type\":\"prom\"},\"name\":\"prometheus\"}]",
5656
"Health": {
5757
"Port": "8080"
5858
},

docs/api.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@ Following is the supported API format for prometheus encode:
1717
valueKey: entry key from which to resolve metric value
1818
labels: labels to be associated with the metric
1919
buckets: histogram buckets
20-
address: address to expose "/metrics" endpoint
21-
port: port number to expose "/metrics" endpoint
2220
prefix: prefix added to each metric name
2321
expiryTime: seconds of no-flow to wait before deleting prometheus data item
24-
tls: TLS configuration for the prometheus endpoint
25-
certPath: path to the certificate file
26-
keyPath: path to the key file
2722
maxMetrics: maximum number of metrics to report (default: unlimited)
2823
</pre>
2924
## Kafka encode API

network_definitions/config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ extract:
8888
encode:
8989
prom:
9090
prefix: flp_
91-
port: 9102
9291
write:
9392
type: loki
9493
loki:

pkg/api/encode_prom.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ type PromTLSConf struct {
2424

2525
type PromEncode struct {
2626
Metrics PromMetricsItems `yaml:"metrics,omitempty" json:"metrics,omitempty" doc:"list of prometheus metric definitions, each includes:"`
27-
Address string `yaml:"address,omitempty" json:"address,omitempty" doc:"address to expose \"/metrics\" endpoint"`
28-
Port int `yaml:"port,omitempty" json:"port,omitempty" doc:"port number to expose \"/metrics\" endpoint"`
2927
Prefix string `yaml:"prefix,omitempty" json:"prefix,omitempty" doc:"prefix added to each metric name"`
3028
ExpiryTime int `yaml:"expiryTime,omitempty" json:"expiryTime,omitempty" doc:"seconds of no-flow to wait before deleting prometheus data item"`
31-
TLS *PromTLSConf `yaml:"tls,omitempty" json:"tls,omitempty" doc:"TLS configuration for the prometheus endpoint"`
3229
MaxMetrics int `yaml:"maxMetrics,omitempty" json:"maxMetrics,omitempty" doc:"maximum number of metrics to report (default: unlimited)"`
3330
}
3431

pkg/confgen/confgen_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ func Test_RunShortConfGen(t *testing.T) {
142142
// Expects prom encode
143143
require.Len(t, out.Parameters[3].Encode.Prom.Metrics, 1)
144144
require.Equal(t, &api.PromEncode{
145-
Port: 9102,
146145
Prefix: "flp_",
147146
Metrics: api.PromMetricsItems{{
148147
Name: "test_metric",
@@ -222,7 +221,6 @@ func Test_RunConfGenNoAgg(t *testing.T) {
222221
// Expects prom encode
223222
require.Len(t, out.Parameters[2].Encode.Prom.Metrics, 1)
224223
require.Equal(t, &api.PromEncode{
225-
Port: 9102,
226224
Prefix: "flp_",
227225
Metrics: api.PromMetricsItems{{
228226
Name: "test_metric",
@@ -325,7 +323,6 @@ func Test_RunLongConfGen(t *testing.T) {
325323
// Expects prom encode; make sure type "histogram" is changed to "agg_histogram"
326324
require.Len(t, out.Parameters[4].Encode.Prom.Metrics, 2)
327325
require.Equal(t, &api.PromEncode{
328-
Port: 9102,
329326
Prefix: "flp_",
330327
Metrics: api.PromMetricsItems{{
331328
Name: "test_metric",

pkg/confgen/config_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ func expectedConfig() *Config {
3131
Description: "test description",
3232
Encode: config.Encode{
3333
Prom: &api.PromEncode{
34-
Port: 7777,
3534
Prefix: "prefix",
3635
},
3736
},
@@ -54,7 +53,6 @@ ingest:
5453
port: 8888
5554
encode:
5655
prom:
57-
port: 7777
5856
prefix: prefix
5957
`
6058

pkg/confgen/flowlogs2metrics_config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ func (cg *ConfGen) GenerateFlowlogs2PipelineConfig() *config.ConfigFileStruct {
5656
}
5757
if len(cg.promMetrics) > 0 {
5858
metricsNode.EncodePrometheus("encode_prom", api.PromEncode{
59-
Address: cg.config.Encode.Prom.Address,
60-
Port: cg.config.Encode.Prom.Port,
6159
Prefix: cg.config.Encode.Prom.Prefix,
6260
Metrics: cg.promMetrics,
6361
})
@@ -69,6 +67,10 @@ func (cg *ConfGen) GenerateFlowlogs2PipelineConfig() *config.ConfigFileStruct {
6967
LogLevel: "error",
7068
Pipeline: pipeline.GetStages(),
7169
Parameters: pipeline.GetStageParams(),
70+
MetricsSettings: config.MetricsSettings{
71+
Port: 9102,
72+
Prefix: "flp_op",
73+
},
7274
}
7375
}
7476

pkg/config/config.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ type Options struct {
3636

3737
type ConfigFileStruct struct {
3838
LogLevel string `yaml:"log-level,omitempty" json:"log-level,omitempty"`
39+
MetricsSettings MetricsSettings `yaml:"metricsSettings,omitempty" json:"metricsSettings,omitempty"`
3940
Pipeline []Stage `yaml:"pipeline,omitempty" json:"pipeline,omitempty"`
4041
Parameters []StageParam `yaml:"parameters,omitempty" json:"parameters,omitempty"`
41-
MetricsSettings MetricsSettings `yaml:"metricsSettings,omitempty" json:"metricsSettings,omitempty"`
4242
PerfSettings PerfSettings `yaml:"perfSettings,omitempty" json:"perfSettings,omitempty"`
4343
}
4444

@@ -55,12 +55,11 @@ type Profile struct {
5555
// Also, currently FLP doesn't support defining more than one PromEncode stage. If this feature is added later, these global settings
5656
// will help configuring common setting for all PromEncode stages - PromEncode settings would then act as overrides.
5757
type MetricsSettings struct {
58-
// TODO: manage global metrics server, ie. not coupled to PromEncode, cf https://github.com/netobserv/flowlogs-pipeline/issues/302
59-
// Port int `yaml:"port,omitempty" json:"port,omitempty" doc:"port number to expose \"/metrics\" endpoint"`
60-
// TLS *PromTLSConf `yaml:"tls,omitempty" json:"tls,omitempty" doc:"TLS configuration for the prometheus endpoint"`
61-
// ExpiryTime int `yaml:"expiryTime,omitempty" json:"expiryTime,omitempty" doc:"seconds of no-flow to wait before deleting prometheus data item"`
62-
Prefix string `yaml:"prefix,omitempty" json:"prefix,omitempty"`
63-
NoPanic bool `yaml:"noPanic,omitempty" json:"noPanic,omitempty"`
58+
Address string `yaml:"address,omitempty" json:"address,omitempty" doc:"address to expose \"/metrics\" endpoint"`
59+
Port int `yaml:"port,omitempty" json:"port,omitempty" doc:"port number to expose \"/metrics\" endpoint"`
60+
TLS *api.PromTLSConf `yaml:"tls,omitempty" json:"tls,omitempty" doc:"TLS configuration for the prometheus endpoint"`
61+
Prefix string `yaml:"prefix,omitempty" json:"prefix,omitempty"`
62+
NoPanic bool `yaml:"noPanic,omitempty" json:"noPanic,omitempty"`
6463
}
6564

6665
// PerfSettings allows setting some internal configuration parameters

0 commit comments

Comments
 (0)