Skip to content

Commit 56a79bf

Browse files
authored
Merge pull request #19 from blockopsnetwork/enable-logs-sink
Enable logs sink
2 parents 47a4d99 + 434e742 commit 56a79bf

File tree

2 files changed

+121
-49
lines changed

2 files changed

+121
-49
lines changed

cmd/agent/main.go

Lines changed: 98 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var cfgFile string
3838
type Config struct {
3939
Server ServerConfig `yaml:"server"`
4040
Metrics MetricsConfig `yaml:"metrics"`
41+
Logs LogsConfig `yaml:"logs"`
4142
Integrations map[string]interface{} `yaml:"integrations"`
4243

4344
}
@@ -93,9 +94,30 @@ type RemoteWrite struct {
9394

9495
}
9596

97+
type LogsConfig struct {
98+
Configs []LogConfig `yaml:"configs"`
99+
}
100+
101+
type LogConfig struct {
102+
Name string `yaml:"name"`
103+
Clients []LogClient `yaml:"clients"`
104+
Positions Positions `yaml:"positions"`
105+
}
106+
107+
type Positions struct {
108+
Filename string `yaml:"filename"`
109+
}
110+
111+
type LogClient struct {
112+
URL string `yaml:"url"`
113+
BasicAuth BasicAuth `yaml:"basic_auth"`
114+
ExternalLabels map[string]string `yaml:"external_labels"`
115+
}
116+
96117

97118
type TelescopeConfig struct {
98119
Metrics bool
120+
Logs bool
99121
Network string
100122
ProjectId string
101123
ProjectName string
@@ -175,7 +197,6 @@ var cmd = &cobra.Command{
175197
}
176198

177199

178-
179200
func generateNetworkConfig() Config {
180201
// var telescope_config TelescopeConfig
181202
// cMetrics := viper.GetBool("metrics")
@@ -185,6 +206,8 @@ func generateNetworkConfig() Config {
185206
cTelescopeUsername := viper.GetString("telescope-username")
186207
cTelescopePassword := viper.GetString("telescope-password")
187208
cRemoteWriteUrl := viper.GetString("remote-write-url")
209+
isEnableLogs := viper.GetString("enable-logs")
210+
cLogSinkURL := viper.GetString("logs-sink-url")
188211

189212
ports, err := networkDiscovery(cNetwork)
190213

@@ -218,7 +241,7 @@ func generateNetworkConfig() Config {
218241
idx++
219242
}
220243

221-
return Config{
244+
config := Config{
222245
Server: ServerConfig{
223246
LogLevel: "info",
224247
},
@@ -227,7 +250,7 @@ func generateNetworkConfig() Config {
227250
Global: GlobalConfig{
228251
ScrapeInterval: "15s",
229252
ExternalLabels: map[string]string{
230-
"project_id": cProjectId,
253+
"project_id": cProjectId,
231254
"project_name": cProjectName,
232255
},
233256
RemoteWrite: []RemoteWrite{
@@ -238,13 +261,12 @@ func generateNetworkConfig() Config {
238261
"password": cTelescopePassword,
239262
},
240263
},
241-
242264
},
243265
},
244266
Configs: []MetricConfig{
245267
{
246-
Name: toLowerAndEscape(cProjectName+cNetwork+"_metrics"),
247-
HostFilter: false,
268+
Name: toLowerAndEscape(cProjectName + cNetwork + "_metrics"),
269+
HostFilter: false,
248270
ScrapeConfigs: scrapeConfigs,
249271
},
250272
},
@@ -257,8 +279,36 @@ func generateNetworkConfig() Config {
257279
Enabled: true,
258280
},
259281
},
260-
261282
}
283+
284+
if isEnableLogs == "true" {
285+
config.Logs = LogsConfig{
286+
Configs: []LogConfig{
287+
{
288+
Name: "telescope_logs",
289+
Clients: []LogClient{
290+
{
291+
URL: cLogSinkURL,
292+
BasicAuth: BasicAuth{
293+
Username: cTelescopeUsername,
294+
Password: cTelescopePassword,
295+
},
296+
ExternalLabels: map[string]string{
297+
"project_id": cProjectId,
298+
"project_name": cProjectName,
299+
},
300+
},
301+
},
302+
Positions: Positions{
303+
Filename: "/tmp/telescope_logs",
304+
},
305+
},
306+
},
307+
}
308+
}
309+
310+
return config
311+
262312
}
263313

264314
func LoadNetworkConfig() string {
@@ -290,11 +340,13 @@ var helperFunction = func(cmd *cobra.Command, args []string) {
290340
fmt.Println(" --telescope-username\tSpecify the telescope username")
291341
fmt.Println(" --telescope-password\tSpecify the telescope password")
292342
fmt.Println(" --remote-write-url\tSpecify the remote write URL")
343+
fmt.Println(" --config-file\t\tSpecify the config file")
344+
fmt.Println(" --logs\t\tEnable logs")
293345
}
294346

295347

296348
func checkRequiredFlags() error {
297-
requiredFlags := []string{"metrics", "network", "project-id", "project-name", "telescope-username", "telescope-password", "remote-write-url"}
349+
requiredFlags := []string{"metrics", "enable-logs", "network", "project-id", "project-name", "telescope-username", "telescope-password", "remote-write-url"}
298350
missingFlags := []string{}
299351

300352
for _, flag := range requiredFlags {
@@ -303,7 +355,7 @@ func checkRequiredFlags() error {
303355
}
304356
}
305357

306-
if len(missingFlags) > 0 {
358+
if len(missingFlags) > 0 && viper.GetString("config-file") == "" {
307359
return fmt.Errorf("Error: Missing required flags: %s", strings.Join(missingFlags, ", "))
308360
}
309361

@@ -314,22 +366,28 @@ func init() {
314366
prometheus.MustRegister(build.NewCollector("agent"))
315367
cobra.OnInitialize(initConfig)
316368
// cmd.SetHelpFunc(helperFunction)
317-
cmd.Flags().Bool("metrics", false, "Enable metrics")
369+
cmd.Flags().StringVar(&cfgFile, "config-file", "", "Specify the config file")
370+
cmd.Flags().Bool("metrics", true, "Enable metrics")
371+
cmd.Flags().Bool("enable-logs", false, "Enable logs")
318372
cmd.Flags().String("network", "", "Specify the network")
319373
cmd.Flags().String("project-id", "", "Specify the project ID")
320374
cmd.Flags().String("project-name", "", "Specify the project name")
321375
cmd.Flags().String("telescope-username", "", "Specify the telescope username")
322376
cmd.Flags().String("telescope-password", "", "Specify the telescope password")
323377
cmd.Flags().String("remote-write-url", "", "Specify the remote write URL")
378+
cmd.Flags().String("logs-sink-url", "", "Specify the Log Sink URL")
324379

325380
// Bind flags with viper
381+
viper.BindPFlag("config-file", cmd.Flags().Lookup("config-file"))
326382
viper.BindPFlag("metrics", cmd.Flags().Lookup("metrics"))
327383
viper.BindPFlag("network", cmd.Flags().Lookup("network"))
328384
viper.BindPFlag("project-id", cmd.Flags().Lookup("project-id"))
329385
viper.BindPFlag("project-name", cmd.Flags().Lookup("project-name"))
330386
viper.BindPFlag("telescope-username", cmd.Flags().Lookup("telescope-username"))
331387
viper.BindPFlag("telescope-password", cmd.Flags().Lookup("telescope-password"))
332388
viper.BindPFlag("remote-write-url", cmd.Flags().Lookup("remote-write-url"))
389+
viper.BindPFlag("logs-sink-url", cmd.Flags().Lookup("logs-sink-url"))
390+
viper.BindPFlag("enable-logs", cmd.Flags().Lookup("enable-logs"))
333391

334392

335393
// cmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cmd.yaml)")
@@ -340,38 +398,33 @@ func initConfig() {
340398
if cfgFile != "" {
341399
// Use config file from the flag.
342400
viper.SetConfigFile(cfgFile)
401+
if err := viper.ReadInConfig(); err != nil {
402+
log.Fatalf("Error reading config file: %v", err)
403+
}
343404
} else {
344-
// Find home directory.
345-
home, err := os.UserHomeDir()
346-
cobra.CheckErr(err)
347-
348-
// Search config in home directory with name ".cmd" (without extension).
349-
viper.AddConfigPath(home)
350-
viper.SetConfigType("yaml")
351-
viper.SetConfigName(".cmd")
405+
viper.AutomaticEnv() // read in environment variables that match
352406
}
407+
}
353408

354-
viper.AutomaticEnv() // read in environment variables that match
409+
func (c *TelescopeConfig) loadConfig() error {
355410

356-
// If a config file is found, read it in.
357-
if err := viper.ReadInConfig(); err == nil {
358-
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
411+
// Check if config-file is provided
412+
if viper.GetString("config-file") != "" {
413+
cfgFile = viper.GetString("config-file")
414+
viper.SetConfigFile(cfgFile)
415+
if err := viper.ReadInConfig(); err != nil {
416+
return fmt.Errorf("Error reading config file: %s", err)
417+
}
418+
} else {
419+
// generateNetworkConfig
420+
LoadNetworkConfig()
421+
}
422+
423+
// Check for required fields only if config-file is not provided
424+
if err := checkRequiredFlags(); err != nil {
425+
return err
359426
}
360-
}
361427

362-
func (c *TelescopeConfig) loadConfig() error {
363-
c.Metrics = viper.GetBool("metrics")
364-
c.Network = viper.GetString("network")
365-
c.ProjectId = viper.GetString("project-id")
366-
c.ProjectName = viper.GetString("project-name")
367-
c.TelescopeUsername = viper.GetString("telescope-username")
368-
c.TelescopePassword = viper.GetString("telescope-password")
369-
c.RemoteWriteUrl = viper.GetString("remote-write-url")
370-
371-
// Check for required fields
372-
if err := checkRequiredFlags(); err != nil {
373-
return err
374-
}
375428

376429
return nil
377430

@@ -423,10 +476,16 @@ func agent(configPath string) {
423476
func main() {
424477

425478
err := cmd.Execute()
426-
427-
configPath := LoadNetworkConfig()
428-
agent(configPath)
429479
if err != nil {
430480
os.Exit(1)
431481
}
482+
483+
var configPath string
484+
if cfgFile != "" {
485+
configPath = cfgFile
486+
} else {
487+
configPath = LoadNetworkConfig()
488+
}
489+
490+
agent(configPath)
432491
}

telescope_config.yaml

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,39 @@ metrics:
44
global:
55
scrape_interval: 15s
66
external_labels:
7-
project_id: hyperbridge-nexus-bootnode
8-
project_name: hyperbridge-nexus
7+
project_id: adedayo123
8+
project_name: 0hxNEtwork
99
remote_write:
10-
- url: https://hyperbridge-prometheus.blockops.network/api/v1/write
10+
- url: http://telescope.blockops.network
1111
basic_auth:
12-
password: hyperbridge
13-
username: hyperbridge
12+
password: pass
13+
username: user
1414
wal_directory: /tmp/wal
1515
configs:
16-
- name: hyperbridge-nexushyperbridge_metrics
16+
- name: 0hxnetworkethereum_metrics
1717
host_filter: false
1818
scrape_configs:
19-
- job_name: hyperbridge-nexus_hyperbridge_relaychain_job_0
19+
- job_name: 0hxnetwork_ethereum_execution_job_0
2020
static_configs:
2121
- targets:
22-
- localhost:9615
23-
- job_name: hyperbridge-nexus_hyperbridge_parachain_job_1
22+
- localhost:6060
23+
- job_name: 0hxnetwork_ethereum_consensus_job_1
2424
static_configs:
2525
- targets:
26-
- localhost:9616
26+
- localhost:8008
27+
logs:
28+
configs:
29+
- name: telescope_logs
30+
clients:
31+
- url: https://logs.blockops.network
32+
basic_auth:
33+
username: user
34+
password: pass
35+
external_labels:
36+
project_id: adedayo123
37+
project_name: 0hxNEtwork
38+
positions:
39+
filename: /tmp/telescope_logs
2740
integrations:
2841
agent:
2942
enabled: false

0 commit comments

Comments
 (0)