From f139c2bda9cce6fe962088db26835f9b335bf780 Mon Sep 17 00:00:00 2001 From: Saka-Aiyedun Segun Date: Tue, 8 Oct 2024 09:50:39 -0400 Subject: [PATCH 1/8] feat: add loki username and password Signed-off-by: Saka-Aiyedun Segun --- cmd/agent/main.go | 170 ++++++++++++++++++++++++++++++---------------- 1 file changed, 112 insertions(+), 58 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 3665e57..35a5ac4 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -1,38 +1,39 @@ package main import ( - "bytes" - "flag" - "fmt" - "log" - "net/url" - "os" - "strings" - - networksConfig "github.com/grafana/agent/internal/static/config/networks" - - "github.com/go-kit/log/level" - "github.com/grafana/agent/internal/boringcrypto" - "github.com/grafana/agent/internal/build" - "github.com/grafana/agent/internal/static/config" - "github.com/grafana/agent/internal/static/server" - util_log "github.com/grafana/agent/internal/util/log" - "github.com/spf13/cobra" - "github.com/spf13/viper" - "gopkg.in/yaml.v3" - - "github.com/prometheus/client_golang/prometheus" - - // Register Prometheus SD components - _ "github.com/grafana/loki/clients/pkg/promtail/discovery/consulagent" - _ "github.com/prometheus/prometheus/discovery/install" - - // Register integrations - _ "github.com/grafana/agent/internal/static/integrations/install" - - // Embed a set of fallback X.509 trusted roots - // Allows the app to work correctly even when the OS does not provide a verifier or systems roots pool - _ "golang.org/x/crypto/x509roots/fallback" + + "bytes" + "flag" + "fmt" + "log" + "net/url" + "os" + "strings" + + networksConfig "github.com/grafana/agent/internal/static/config/networks" + + "github.com/go-kit/log/level" + "github.com/grafana/agent/internal/boringcrypto" + "github.com/grafana/agent/internal/build" + "github.com/grafana/agent/internal/static/config" + "github.com/grafana/agent/internal/static/server" + util_log "github.com/grafana/agent/internal/util/log" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "gopkg.in/yaml.v3" + + "github.com/prometheus/client_golang/prometheus" + + // Register Prometheus SD components + _ "github.com/grafana/loki/clients/pkg/promtail/discovery/consulagent" + _ "github.com/prometheus/prometheus/discovery/install" + + // Register integrations + _ "github.com/grafana/agent/internal/static/integrations/install" + + // Embed a set of fallback X.509 trusted roots + // Allows the app to work correctly even when the OS does not provide a verifier or systems roots pool + _ "golang.org/x/crypto/x509roots/fallback" ) var network string @@ -43,19 +44,34 @@ var cmd = &cobra.Command{ Long: `Gain full insights into the performance of your dApps, nodes and onchain events with Telescope.`, Run: func(cmd *cobra.Command, args []string) { var config TelescopeConfig - fmt.Println(config) if err := config.loadConfig(); err != nil { fmt.Println(err) os.Exit(1) } + // Proceed to generate and write configuration, then start the agent + network = viper.GetString("network") + projectName := viper.GetString("project-name") + networkConfig := getNetworkConfig(network) + + fmt.Println("Starting Telescope Agent", networkConfig) + scrapeConfigs := networkConfig.GenerateScrapeConfigs(projectName, network) + fullConfig := generateFullConfig(config, scrapeConfigs) + configFilePath := "telescope_config.yaml" + if err := writeConfigToFile(fullConfig, configFilePath); err != nil { + log.Fatalf("Failed to write config to file: %v", err) + } + + fmt.Printf("Configuration written to %s\n", configFilePath) + + agent(configFilePath) }, } var helperFunction = func(cmd *cobra.Command, args []string) { fmt.Println("Custom Help:") - fmt.Println("Usage: app start [flags]") + fmt.Println("Usage: telescope start [flags]") fmt.Println("\nFlags:") fmt.Println(" --metrics\t\tEnable metrics") fmt.Println(" --network\t\tSpecify the network") @@ -65,7 +81,10 @@ var helperFunction = func(cmd *cobra.Command, args []string) { fmt.Println(" --telescope-password\tSpecify the telescope password") fmt.Println(" --remote-write-url\tSpecify the remote write URL") fmt.Println(" --config-file\t\tSpecify the config file") - fmt.Println(" --logs\t\tEnable logs") + fmt.Println(" --enable-logs\t\tEnable logs") + fmt.Println(" --logs-sink-url\tSpecify the Log Sink URL") + fmt.Println(" --telescope-loki-username\tSpecify the Loki username") + fmt.Println(" --telescope-loki-password\tSpecify the Loki password") } var cfgFile string @@ -163,6 +182,9 @@ type TelescopeConfig struct { TelescopeUsername string TelescopePassword string RemoteWriteUrl string + LokiUsername string + LokiPassword string + LogsSinkURL string } func handleErr(err error, msg string) { @@ -185,8 +207,6 @@ func getNetworkConfig(network string) networksConfig.NetworkConfig { default: LoadNetworkConfig() return nil - // log.Fatalf("Unsupported network: %s", network) - // return nil } } @@ -197,8 +217,10 @@ func generateFullConfig(config TelescopeConfig, networkScrapeConfigs []networksC cTelescopeUsername := viper.GetString("telescope-username") cTelescopePassword := viper.GetString("telescope-password") cRemoteWriteUrl := viper.GetString("remote-write-url") - // isEnableLogs := viper.GetString("enable-logs") - // cLogSinkURL := viper.GetString("logs-sink-url") + // isEnableLogs := viper.GetBool("enable-logs") + // cLogsSinkURL := viper.GetString("logs-sink-url") + // cLokiUsername := viper.GetString("telescope-loki-username") + // cLokiPassword := viper.GetString("telescope-loki-password") // Convert networksConfig.ScrapeConfig to local ScrapeConfig scrapeConfigs := make([]ScrapeConfig, len(networkScrapeConfigs)) @@ -244,8 +266,8 @@ func generateFullConfig(config TelescopeConfig, networkScrapeConfigs []networksC }, }, Integrations: map[string]interface{}{ - "agent": map[string]bool{"enabled": false}, - "node_exporter": map[string]bool{"enabled": true}, + "agent": ToIntegrate{Enabled: false}, + "node_exporter": ToIntegrate{Enabled: true}, }, } } @@ -260,6 +282,7 @@ func writeConfigToFile(config Config, filePath string) error { } data := buf.String() + // Optional: Perform any necessary string replacements or adjustments fixedData := strings.ReplaceAll(data, "job_name", "job_name") fixedData = strings.ReplaceAll(fixedData, "static_configs", "static_configs") @@ -269,7 +292,7 @@ func writeConfigToFile(config Config, filePath string) error { func networkDiscovery(network string) ([]string, error) { scrapeConfig, ok := networkConfigs[network] if !ok { - return nil, fmt.Errorf("invalid network. Please choose from: ethereum, polkadot, hyperbridge") + return nil, fmt.Errorf("invalid network. Please choose from: ethereum, polkadot, hyperbridge, ssv") } return scrapeConfig.NetworkDiscovery() } @@ -281,8 +304,10 @@ func generateNetworkConfig() Config { cTelescopeUsername := viper.GetString("telescope-username") cTelescopePassword := viper.GetString("telescope-password") cRemoteWriteUrl := viper.GetString("remote-write-url") - isEnableLogs := viper.GetString("enable-logs") - cLogSinkURL := viper.GetString("logs-sink-url") + cLogsSinkURL := viper.GetString("logs-sink-url") + cLokiUsername := viper.GetString("telescope-loki-username") + cLokiPassword := viper.GetString("telescope-loki-password") + isEnableLogs := viper.GetBool("enable-logs") ports, err := networkDiscovery(cNetwork) handleErr(err, "Failed to discover blockchain port") @@ -292,7 +317,6 @@ func generateNetworkConfig() Config { } networkConfig, ok := networkConfigs[cNetwork] - if !ok { log.Fatalf("Invalid network configuration for: %v", cNetwork) } @@ -317,7 +341,7 @@ func generateNetworkConfig() Config { LogLevel: "info", }, Metrics: MetricsConfig{ - Wal_Directory: "/tmp/telescope", //TODO: make this configurable + Wal_Directory: "/tmp/telescope", // TODO: make this configurable Global: GlobalConfig{ ScrapeInterval: "15s", ExternalLabels: map[string]string{ @@ -348,17 +372,18 @@ func generateNetworkConfig() Config { }, } - if isEnableLogs == "true" { + if isEnableLogs { + // Include only Loki-specific LogClient config.Logs = LogsConfig{ Configs: []LogConfig{ { Name: "telescope_logs", Clients: []LogClient{ { - URL: cLogSinkURL, + URL: cLogsSinkURL, // Replace with your actual Loki URL if different BasicAuth: BasicAuth{ - Username: cTelescopeUsername, - Password: cTelescopePassword, + Username: cLokiUsername, // Loki Username + Password: cLokiPassword, // Loki Password }, ExternalLabels: map[string]string{ "project_id": cProjectId, @@ -367,7 +392,7 @@ func generateNetworkConfig() Config { }, }, Positions: Positions{ - Filename: "/tmp/telescope_logs", + Filename: "/tmp/telescope_logs", // Ensure this path is writable }, }, }, @@ -375,11 +400,9 @@ func generateNetworkConfig() Config { } return config - } func LoadNetworkConfig() string { - config := generateNetworkConfig() // Create a custom marshaler @@ -404,7 +427,18 @@ func LoadNetworkConfig() string { } func checkRequiredFlags() error { - requiredFlags := []string{"metrics", "enable-logs", "network", "project-id", "project-name", "telescope-username", "telescope-password", "remote-write-url"} + requiredFlags := []string{ + "metrics", + "enable-logs", + "network", + "project-id", + "project-name", + "telescope-username", + "telescope-password", + "remote-write-url", + "telescope-loki-username", // New required flag + "telescope-loki-password", // New required flag + } missingFlags := []string{} for _, flag := range requiredFlags { @@ -435,9 +469,14 @@ func init() { cmd.Flags().String("remote-write-url", "", "Specify the remote write URL") cmd.Flags().String("logs-sink-url", "", "Specify the Log Sink URL") - // Bind flags with viper + // Define new flags for Loki credentials + cmd.Flags().String("telescope-loki-username", "", "Specify the Loki username") // New flag + cmd.Flags().String("telescope-loki-password", "", "Specify the Loki password") // New flag + + // Bind flags with Viper viper.BindPFlag("config-file", cmd.Flags().Lookup("config-file")) viper.BindPFlag("metrics", cmd.Flags().Lookup("metrics")) + viper.BindPFlag("enable-logs", cmd.Flags().Lookup("enable-logs")) viper.BindPFlag("network", cmd.Flags().Lookup("network")) viper.BindPFlag("project-id", cmd.Flags().Lookup("project-id")) viper.BindPFlag("project-name", cmd.Flags().Lookup("project-name")) @@ -445,7 +484,10 @@ func init() { viper.BindPFlag("telescope-password", cmd.Flags().Lookup("telescope-password")) viper.BindPFlag("remote-write-url", cmd.Flags().Lookup("remote-write-url")) viper.BindPFlag("logs-sink-url", cmd.Flags().Lookup("logs-sink-url")) - viper.BindPFlag("enable-logs", cmd.Flags().Lookup("enable-logs")) + + // Bind new Loki flags + viper.BindPFlag("telescope-loki-username", cmd.Flags().Lookup("telescope-loki-username")) // New binding + viper.BindPFlag("telescope-loki-password", cmd.Flags().Lookup("telescope-loki-password")) // New binding } func initConfig() { @@ -474,6 +516,19 @@ func (c *TelescopeConfig) loadConfig() error { return err } + // Assign the fields from Viper to the struct + c.Metrics = viper.GetBool("metrics") + c.Logs = viper.GetBool("enable-logs") + c.Network = viper.GetString("network") + c.ProjectId = viper.GetString("project-id") + c.ProjectName = viper.GetString("project-name") + c.TelescopeUsername = viper.GetString("telescope-username") + c.TelescopePassword = viper.GetString("telescope-password") + c.RemoteWriteUrl = viper.GetString("remote-write-url") + c.LokiUsername = viper.GetString("telescope-loki-username") // New field + c.LokiPassword = viper.GetString("telescope-loki-password") // New field + c.LogsSinkURL = viper.GetString("logs-sink-url") // Assign LogsSinkURL + return nil } @@ -487,7 +542,6 @@ func agent(configPath string) { // fs.String("telescope", configPath, "Path to configuration file") // fs.Parse([]string{"--config.file=" + configPath}) // Ensure the flag is set return config.Load(fs, []string{"-config.file", configPath}, log) - } cfg, err := reloader(logger) if err != nil { @@ -541,4 +595,4 @@ func main() { fmt.Printf("Configuration written to %s\n", configFilePath) agent(configFilePath) -} +} \ No newline at end of file From e9eb1fb9766d5ab7b4496b4d7bb576c8f6476a76 Mon Sep 17 00:00:00 2001 From: segunjkf Date: Tue, 8 Oct 2024 09:55:36 -0400 Subject: [PATCH 2/8] wip: test push Signed-off-by: segunjkf --- .github/workflows/docker.yml | 4 ++-- cmd/agent/main.go | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 18d982b..a58beb6 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -59,7 +59,7 @@ jobs: tags: | type=raw,value={{date 'YYYY-MM-DD'}}-{{sha}} type=ref,event=tag - type=edge + type=dev - name: free disk space run: | @@ -71,7 +71,7 @@ jobs: file: ./cmd/agent/Dockerfile tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - push: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') }} + push: true # Compile Docker image only for AMD64 for a regular PR to save some CI time. platforms: ${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) && 'linux/amd64,linux/arm64' || 'linux/amd64' }} diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 35a5ac4..8a85643 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -380,10 +380,10 @@ func generateNetworkConfig() Config { Name: "telescope_logs", Clients: []LogClient{ { - URL: cLogsSinkURL, // Replace with your actual Loki URL if different + URL: cLogsSinkURL, BasicAuth: BasicAuth{ - Username: cLokiUsername, // Loki Username - Password: cLokiPassword, // Loki Password + Username: cLokiUsername, + Password: cLokiPassword, }, ExternalLabels: map[string]string{ "project_id": cProjectId, @@ -392,7 +392,7 @@ func generateNetworkConfig() Config { }, }, Positions: Positions{ - Filename: "/tmp/telescope_logs", // Ensure this path is writable + Filename: "/tmp/telescope_logs", }, }, }, @@ -486,8 +486,8 @@ func init() { viper.BindPFlag("logs-sink-url", cmd.Flags().Lookup("logs-sink-url")) // Bind new Loki flags - viper.BindPFlag("telescope-loki-username", cmd.Flags().Lookup("telescope-loki-username")) // New binding - viper.BindPFlag("telescope-loki-password", cmd.Flags().Lookup("telescope-loki-password")) // New binding + viper.BindPFlag("telescope-loki-username", cmd.Flags().Lookup("telescope-loki-username")) + viper.BindPFlag("telescope-loki-password", cmd.Flags().Lookup("telescope-loki-password")) } func initConfig() { @@ -505,7 +505,7 @@ func (c *TelescopeConfig) loadConfig() error { cfgFile = viper.GetString("config-file") viper.SetConfigFile(cfgFile) if err := viper.ReadInConfig(); err != nil { - return fmt.Errorf("Error reading config file: %s", err) + return fmt.Errorf("error reading config file: %s", err) } } else { LoadNetworkConfig() @@ -525,9 +525,9 @@ func (c *TelescopeConfig) loadConfig() error { c.TelescopeUsername = viper.GetString("telescope-username") c.TelescopePassword = viper.GetString("telescope-password") c.RemoteWriteUrl = viper.GetString("remote-write-url") - c.LokiUsername = viper.GetString("telescope-loki-username") // New field - c.LokiPassword = viper.GetString("telescope-loki-password") // New field - c.LogsSinkURL = viper.GetString("logs-sink-url") // Assign LogsSinkURL + c.LokiUsername = viper.GetString("telescope-loki-username") + c.LokiPassword = viper.GetString("telescope-loki-password") + c.LogsSinkURL = viper.GetString("logs-sink-url") return nil } From 8ac42e714cadebe289b30d0eb42ccbc9aedf64cb Mon Sep 17 00:00:00 2001 From: segunjkf Date: Tue, 8 Oct 2024 09:59:41 -0400 Subject: [PATCH 3/8] wip: test push Signed-off-by: segunjkf --- .github/workflows/docker.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index a58beb6..505ffb6 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -59,7 +59,7 @@ jobs: tags: | type=raw,value={{date 'YYYY-MM-DD'}}-{{sha}} type=ref,event=tag - type=dev + type=test - name: free disk space run: | @@ -70,7 +70,7 @@ jobs: with: file: ./cmd/agent/Dockerfile tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} + labels: test push: true # Compile Docker image only for AMD64 for a regular PR to save some CI time. platforms: ${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) && 'linux/amd64,linux/arm64' || 'linux/amd64' }} From 4feaf60b7691226e7345ab3316756f66e4fd906c Mon Sep 17 00:00:00 2001 From: segunjkf Date: Tue, 8 Oct 2024 10:01:27 -0400 Subject: [PATCH 4/8] wip: test push Signed-off-by: segunjkf --- .github/workflows/docker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 505ffb6..a81419b 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -59,7 +59,7 @@ jobs: tags: | type=raw,value={{date 'YYYY-MM-DD'}}-{{sha}} type=ref,event=tag - type=test + ttype=edge - name: free disk space run: | From a4ca7b67822d0862d97e717027966724dc331153 Mon Sep 17 00:00:00 2001 From: segunjkf Date: Tue, 8 Oct 2024 10:03:44 -0400 Subject: [PATCH 5/8] wip: test push Signed-off-by: segunjkf --- .github/workflows/docker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index a81419b..c9dd631 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -59,7 +59,7 @@ jobs: tags: | type=raw,value={{date 'YYYY-MM-DD'}}-{{sha}} type=ref,event=tag - ttype=edge + type=edge - name: free disk space run: | From bdd6a1b617e71ae48d11304264733f9f6ae66d19 Mon Sep 17 00:00:00 2001 From: segunjkf Date: Tue, 8 Oct 2024 10:28:14 -0400 Subject: [PATCH 6/8] wip: fix nil pointer error Signed-off-by: segunjkf --- cmd/agent/main.go | 816 +++++++++++++++++++++++----------------------- 1 file changed, 406 insertions(+), 410 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 8a85643..1fe6aa7 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -39,533 +39,529 @@ import ( var network string var cmd = &cobra.Command{ - Use: "telescope", - Short: "An All-in-One Web3 Observability tooling", - Long: `Gain full insights into the performance of your dApps, nodes and onchain events with Telescope.`, - Run: func(cmd *cobra.Command, args []string) { - var config TelescopeConfig + Use: "telescope", + Short: "An All-in-One Web3 Observability tooling", + Long: `Gain full insights into the performance of your dApps, nodes and onchain events with Telescope.`, + Run: func(cmd *cobra.Command, args []string) { + var config TelescopeConfig - if err := config.loadConfig(); err != nil { - fmt.Println(err) - os.Exit(1) - } + if err := config.loadConfig(); err != nil { + fmt.Println(err) + os.Exit(1) + } - // Proceed to generate and write configuration, then start the agent - network = viper.GetString("network") - projectName := viper.GetString("project-name") - networkConfig := getNetworkConfig(network) + // Proceed to generate and write configuration, then start the agent + network = viper.GetString("network") + projectName := viper.GetString("project-name") + networkConfig := getNetworkConfig(network) - fmt.Println("Starting Telescope Agent", networkConfig) - scrapeConfigs := networkConfig.GenerateScrapeConfigs(projectName, network) - fullConfig := generateFullConfig(config, scrapeConfigs) - configFilePath := "telescope_config.yaml" - if err := writeConfigToFile(fullConfig, configFilePath); err != nil { - log.Fatalf("Failed to write config to file: %v", err) - } + fmt.Println("Starting Telescope Agent", networkConfig) + scrapeConfigs := networkConfig.GenerateScrapeConfigs(projectName, network) + fullConfig := generateFullConfig(config, scrapeConfigs) + configFilePath := "telescope_config.yaml" + if err := writeConfigToFile(fullConfig, configFilePath); err != nil { + log.Fatalf("Failed to write config to file: %v", err) + } - fmt.Printf("Configuration written to %s\n", configFilePath) + fmt.Printf("Configuration written to %s\n", configFilePath) - agent(configFilePath) - }, + agent(configFilePath) + }, } var helperFunction = func(cmd *cobra.Command, args []string) { - fmt.Println("Custom Help:") - fmt.Println("Usage: telescope start [flags]") - fmt.Println("\nFlags:") - fmt.Println(" --metrics\t\tEnable metrics") - fmt.Println(" --network\t\tSpecify the network") - fmt.Println(" --project-id\t\tSpecify the project ID") - fmt.Println(" --project-name\tSpecify the project name") - fmt.Println(" --telescope-username\tSpecify the telescope username") - fmt.Println(" --telescope-password\tSpecify the telescope password") - fmt.Println(" --remote-write-url\tSpecify the remote write URL") - fmt.Println(" --config-file\t\tSpecify the config file") - fmt.Println(" --enable-logs\t\tEnable logs") - fmt.Println(" --logs-sink-url\tSpecify the Log Sink URL") - fmt.Println(" --telescope-loki-username\tSpecify the Loki username") - fmt.Println(" --telescope-loki-password\tSpecify the Loki password") + fmt.Println("Custom Help:") + fmt.Println("Usage: telescope start [flags]") + fmt.Println("\nFlags:") + fmt.Println(" --metrics\t\tEnable metrics") + fmt.Println(" --network\t\tSpecify the network") + fmt.Println(" --project-id\t\tSpecify the project ID") + fmt.Println(" --project-name\tSpecify the project name") + fmt.Println(" --telescope-username\tSpecify the telescope username") + fmt.Println(" --telescope-password\tSpecify the telescope password") + fmt.Println(" --remote-write-url\tSpecify the remote write URL") + fmt.Println(" --config-file\t\tSpecify the config file") + fmt.Println(" --enable-logs\t\tEnable logs") + fmt.Println(" --logs-sink-url\tSpecify the Log Sink URL") + fmt.Println(" --telescope-loki-username\tSpecify the Loki username") + fmt.Println(" --telescope-loki-password\tSpecify the Loki password") } var cfgFile string var networkConfigs = map[string]networksConfig.NetworkConfig{ - "ethereum": networksConfig.NewEthereumConfig(), - "polkadot": networksConfig.NewPolkadotConfig(), - "hyperbridge": networksConfig.NewHyperbridgeConfig(), - "ssv": networksConfig.NewSSVConfig(), + "ethereum": networksConfig.NewEthereumConfig(), + "polkadot": networksConfig.NewPolkadotConfig(), + "hyperbridge": networksConfig.NewHyperbridgeConfig(), + "ssv": networksConfig.NewSSVConfig(), } type Config struct { - Server ServerConfig `yaml:"server"` - Metrics MetricsConfig `yaml:"metrics"` - Logs LogsConfig `yaml:"logs"` - Integrations map[string]interface{} `yaml:"integrations"` + Server ServerConfig `yaml:"server"` + Metrics MetricsConfig `yaml:"metrics"` + Logs LogsConfig `yaml:"logs"` + Integrations map[string]interface{} `yaml:"integrations"` } type ServerConfig struct { - LogLevel string `yaml:"log_level"` + LogLevel string `yaml:"log_level"` } type MetricsConfig struct { - Global GlobalConfig `yaml:"global"` - Wal_Directory string `yaml:"wal_directory"` - Configs []MetricConfig `yaml:"configs"` + Global GlobalConfig `yaml:"global"` + Wal_Directory string `yaml:"wal_directory"` + Configs []MetricConfig `yaml:"configs"` } type GlobalConfig struct { - ScrapeInterval string `yaml:"scrape_interval"` - ExternalLabels map[string]string `yaml:"external_labels"` - RemoteWrite []RemoteWrite `yaml:"remote_write"` + ScrapeInterval string `yaml:"scrape_interval"` + ExternalLabels map[string]string `yaml:"external_labels"` + RemoteWrite []RemoteWrite `yaml:"remote_write"` } type MetricConfig struct { - Name string `yaml:"name"` - HostFilter bool `yaml:"host_filter"` - ScrapeConfigs []ScrapeConfig `yaml:"scrape_configs"` + Name string `yaml:"name"` + HostFilter bool `yaml:"host_filter"` + ScrapeConfigs []ScrapeConfig `yaml:"scrape_configs"` } type ScrapeConfig struct { - JobName string `yaml:"job_name"` - StaticConfigs []StaticConfig `yaml:"static_configs"` + JobName string `yaml:"job_name"` + StaticConfigs []StaticConfig `yaml:"static_configs"` } type BasicAuth struct { - Username string `yaml:"username"` - Password string `yaml:"password"` + Username string `yaml:"username"` + Password string `yaml:"password"` } type IntegrationsConfig struct { - Agent ToIntegrate `yaml:"agent"` - NodeExporter ToIntegrate `yaml:"node_exporter"` + Agent ToIntegrate `yaml:"agent"` + NodeExporter ToIntegrate `yaml:"node_exporter"` } type ToIntegrate struct { - Enabled bool `yaml:"enabled"` + Enabled bool `yaml:"enabled"` } type StaticConfig struct { - Targets []string `yaml:"targets"` + Targets []string `yaml:"targets"` } type RemoteWrite struct { - URL string `yaml:"url"` - BasicAuth map[string]string `yaml:"basic_auth"` + URL string `yaml:"url"` + BasicAuth map[string]string `yaml:"basic_auth"` } type LogsConfig struct { - Configs []LogConfig `yaml:"configs"` + Configs []LogConfig `yaml:"configs"` } type LogConfig struct { - Name string `yaml:"name"` - Clients []LogClient `yaml:"clients"` - Positions Positions `yaml:"positions"` + Name string `yaml:"name"` + Clients []LogClient `yaml:"clients"` + Positions Positions `yaml:"positions"` } type Positions struct { - Filename string `yaml:"filename"` + Filename string `yaml:"filename"` } type LogClient struct { - URL string `yaml:"url"` - BasicAuth BasicAuth `yaml:"basic_auth"` - ExternalLabels map[string]string `yaml:"external_labels"` + URL string `yaml:"url"` + BasicAuth BasicAuth `yaml:"basic_auth"` + ExternalLabels map[string]string `yaml:"external_labels"` } type TelescopeConfig struct { - Metrics bool - Logs bool - Network string - ProjectId string - ProjectName string - TelescopeUsername string - TelescopePassword string - RemoteWriteUrl string - LokiUsername string - LokiPassword string - LogsSinkURL string + Metrics bool + Logs bool + Network string + ProjectId string + ProjectName string + TelescopeUsername string + TelescopePassword string + RemoteWriteUrl string + LokiUsername string + LokiPassword string + LogsSinkURL string } func handleErr(err error, msg string) { - if err != nil { - log.Fatalf("%s: %v", msg, err) - } + if err != nil { + log.Fatalf("%s: %v", msg, err) + } } func toLowerAndEscape(input string) string { - lowercase := strings.ToLower(input) - escaped := url.QueryEscape(lowercase) - return escaped + lowercase := strings.ToLower(input) + escaped := url.QueryEscape(lowercase) + return escaped } func getNetworkConfig(network string) networksConfig.NetworkConfig { - switch network { - case "ssv": - return networksConfig.NewSSVConfig() - // Add cases for other networks here - default: - LoadNetworkConfig() - return nil - } + config, exists := networkConfigs[network] + if !exists { + handleErr(fmt.Errorf("unsupported network: %s", network), "Invalid network") + } + return config } func generateFullConfig(config TelescopeConfig, networkScrapeConfigs []networksConfig.ScrapeConfig) Config { - cNetwork := viper.GetString("network") - cProjectId := viper.GetString("project-id") - cProjectName := viper.GetString("project-name") - cTelescopeUsername := viper.GetString("telescope-username") - cTelescopePassword := viper.GetString("telescope-password") - cRemoteWriteUrl := viper.GetString("remote-write-url") - // isEnableLogs := viper.GetBool("enable-logs") + cNetwork := viper.GetString("network") + cProjectId := viper.GetString("project-id") + cProjectName := viper.GetString("project-name") + cTelescopeUsername := viper.GetString("telescope-username") + cTelescopePassword := viper.GetString("telescope-password") + cRemoteWriteUrl := viper.GetString("remote-write-url") + // isEnableLogs := viper.GetBool("enable-logs") // cLogsSinkURL := viper.GetString("logs-sink-url") // cLokiUsername := viper.GetString("telescope-loki-username") // cLokiPassword := viper.GetString("telescope-loki-password") - // Convert networksConfig.ScrapeConfig to local ScrapeConfig - scrapeConfigs := make([]ScrapeConfig, len(networkScrapeConfigs)) - for i, nsc := range networkScrapeConfigs { - scrapeConfigs[i] = ScrapeConfig{ - JobName: nsc.JobName, - StaticConfigs: []StaticConfig{ - { - Targets: nsc.StaticConfigs[0].Targets, - }, - }, - } - } - - return Config{ - Server: ServerConfig{ - LogLevel: "info", - }, - Metrics: MetricsConfig{ - Wal_Directory: "/tmp/telescope", // TODO: make this configurable - Global: GlobalConfig{ - ScrapeInterval: "15s", - ExternalLabels: map[string]string{ - "project_id": cProjectId, - "project_name": cProjectName, - }, - RemoteWrite: []RemoteWrite{ - { - URL: cRemoteWriteUrl, - BasicAuth: map[string]string{ - "username": cTelescopeUsername, - "password": cTelescopePassword, - }, - }, - }, - }, - Configs: []MetricConfig{ - { - Name: toLowerAndEscape(cProjectName + "_" + cNetwork + "_metrics"), - HostFilter: false, - ScrapeConfigs: scrapeConfigs, - }, - }, - }, - Integrations: map[string]interface{}{ - "agent": ToIntegrate{Enabled: false}, - "node_exporter": ToIntegrate{Enabled: true}, - }, - } + // Convert networksConfig.ScrapeConfig to local ScrapeConfig + scrapeConfigs := make([]ScrapeConfig, len(networkScrapeConfigs)) + for i, nsc := range networkScrapeConfigs { + scrapeConfigs[i] = ScrapeConfig{ + JobName: nsc.JobName, + StaticConfigs: []StaticConfig{ + { + Targets: nsc.StaticConfigs[0].Targets, + }, + }, + } + } + + return Config{ + Server: ServerConfig{ + LogLevel: "info", + }, + Metrics: MetricsConfig{ + Wal_Directory: "/tmp/telescope", // TODO: make this configurable + Global: GlobalConfig{ + ScrapeInterval: "15s", + ExternalLabels: map[string]string{ + "project_id": cProjectId, + "project_name": cProjectName, + }, + RemoteWrite: []RemoteWrite{ + { + URL: cRemoteWriteUrl, + BasicAuth: map[string]string{ + "username": cTelescopeUsername, + "password": cTelescopePassword, + }, + }, + }, + }, + Configs: []MetricConfig{ + { + Name: toLowerAndEscape(cProjectName + "_" + cNetwork + "_metrics"), + HostFilter: false, + ScrapeConfigs: scrapeConfigs, + }, + }, + }, + Integrations: map[string]interface{}{ + "agent": ToIntegrate{Enabled: false}, + "node_exporter": ToIntegrate{Enabled: true}, + }, + } } func writeConfigToFile(config Config, filePath string) error { - var buf strings.Builder - encoder := yaml.NewEncoder(&buf) - encoder.SetIndent(2) + var buf strings.Builder + encoder := yaml.NewEncoder(&buf) + encoder.SetIndent(2) - if err := encoder.Encode(&config); err != nil { - return fmt.Errorf("error marshaling to YAML: %v", err) - } + if err := encoder.Encode(&config); err != nil { + return fmt.Errorf("error marshaling to YAML: %v", err) + } - data := buf.String() - // Optional: Perform any necessary string replacements or adjustments - fixedData := strings.ReplaceAll(data, "job_name", "job_name") - fixedData = strings.ReplaceAll(fixedData, "static_configs", "static_configs") + data := buf.String() + // Optional: Perform any necessary string replacements or adjustments + fixedData := strings.ReplaceAll(data, "job_name", "job_name") + fixedData = strings.ReplaceAll(fixedData, "static_configs", "static_configs") - return os.WriteFile(filePath, []byte(fixedData), 0644) + return os.WriteFile(filePath, []byte(fixedData), 0644) } func networkDiscovery(network string) ([]string, error) { - scrapeConfig, ok := networkConfigs[network] - if !ok { - return nil, fmt.Errorf("invalid network. Please choose from: ethereum, polkadot, hyperbridge, ssv") - } - return scrapeConfig.NetworkDiscovery() + scrapeConfig, ok := networkConfigs[network] + if !ok { + return nil, fmt.Errorf("invalid network. Please choose from: ethereum, polkadot, hyperbridge, ssv") + } + return scrapeConfig.NetworkDiscovery() } func generateNetworkConfig() Config { - cNetwork := viper.GetString("network") - cProjectId := viper.GetString("project-id") - cProjectName := viper.GetString("project-name") - cTelescopeUsername := viper.GetString("telescope-username") - cTelescopePassword := viper.GetString("telescope-password") - cRemoteWriteUrl := viper.GetString("remote-write-url") - cLogsSinkURL := viper.GetString("logs-sink-url") - cLokiUsername := viper.GetString("telescope-loki-username") - cLokiPassword := viper.GetString("telescope-loki-password") - isEnableLogs := viper.GetBool("enable-logs") - - ports, err := networkDiscovery(cNetwork) - handleErr(err, "Failed to discover blockchain port") - - for _, port := range ports { - viper.Set("scrape_port", port) - } - - networkConfig, ok := networkConfigs[cNetwork] - if !ok { - log.Fatalf("Invalid network configuration for: %v", cNetwork) - } - - networkScrapeConfigs := networkConfig.GenerateScrapeConfigs(cProjectName, cNetwork) - - // Convert networksConfig.ScrapeConfig to local ScrapeConfig - scrapeConfigs := make([]ScrapeConfig, len(networkScrapeConfigs)) - for i, nsc := range networkScrapeConfigs { - scrapeConfigs[i] = ScrapeConfig{ - JobName: nsc.JobName, - StaticConfigs: []StaticConfig{ - { - Targets: nsc.StaticConfigs[0].Targets, - }, - }, - } - } - - config := Config{ - Server: ServerConfig{ - LogLevel: "info", - }, - Metrics: MetricsConfig{ - Wal_Directory: "/tmp/telescope", // TODO: make this configurable - Global: GlobalConfig{ - ScrapeInterval: "15s", - ExternalLabels: map[string]string{ - "project_id": cProjectId, - "project_name": cProjectName, - }, - RemoteWrite: []RemoteWrite{ - { - URL: cRemoteWriteUrl, - BasicAuth: map[string]string{ - "username": cTelescopeUsername, - "password": cTelescopePassword, - }, - }, - }, - }, - Configs: []MetricConfig{ - { - Name: toLowerAndEscape(cProjectName + "_" + cNetwork + "_metrics"), - HostFilter: false, - ScrapeConfigs: scrapeConfigs, - }, - }, - }, - Integrations: map[string]interface{}{ - "agent": ToIntegrate{Enabled: false}, - "node_exporter": ToIntegrate{Enabled: true}, - }, - } - - if isEnableLogs { - // Include only Loki-specific LogClient - config.Logs = LogsConfig{ - Configs: []LogConfig{ - { - Name: "telescope_logs", - Clients: []LogClient{ - { - URL: cLogsSinkURL, - BasicAuth: BasicAuth{ - Username: cLokiUsername, - Password: cLokiPassword, - }, - ExternalLabels: map[string]string{ - "project_id": cProjectId, - "project_name": cProjectName, - }, - }, - }, - Positions: Positions{ - Filename: "/tmp/telescope_logs", - }, - }, - }, - } - } - - return config + cNetwork := viper.GetString("network") + cProjectId := viper.GetString("project-id") + cProjectName := viper.GetString("project-name") + cTelescopeUsername := viper.GetString("telescope-username") + cTelescopePassword := viper.GetString("telescope-password") + cRemoteWriteUrl := viper.GetString("remote-write-url") + cLogsSinkURL := viper.GetString("logs-sink-url") + cLokiUsername := viper.GetString("telescope-loki-username") + cLokiPassword := viper.GetString("telescope-loki-password") + isEnableLogs := viper.GetBool("enable-logs") + + ports, err := networkDiscovery(cNetwork) + handleErr(err, "Failed to discover blockchain port") + + for _, port := range ports { + viper.Set("scrape_port", port) + } + + networkConfig, ok := networkConfigs[cNetwork] + if !ok { + log.Fatalf("Invalid network configuration for: %v", cNetwork) + } + + networkScrapeConfigs := networkConfig.GenerateScrapeConfigs(cProjectName, cNetwork) + + // Convert networksConfig.ScrapeConfig to local ScrapeConfig + scrapeConfigs := make([]ScrapeConfig, len(networkScrapeConfigs)) + for i, nsc := range networkScrapeConfigs { + scrapeConfigs[i] = ScrapeConfig{ + JobName: nsc.JobName, + StaticConfigs: []StaticConfig{ + { + Targets: nsc.StaticConfigs[0].Targets, + }, + }, + } + } + + config := Config{ + Server: ServerConfig{ + LogLevel: "info", + }, + Metrics: MetricsConfig{ + Wal_Directory: "/tmp/telescope", // TODO: make this configurable + Global: GlobalConfig{ + ScrapeInterval: "15s", + ExternalLabels: map[string]string{ + "project_id": cProjectId, + "project_name": cProjectName, + }, + RemoteWrite: []RemoteWrite{ + { + URL: cRemoteWriteUrl, + BasicAuth: map[string]string{ + "username": cTelescopeUsername, + "password": cTelescopePassword, + }, + }, + }, + }, + Configs: []MetricConfig{ + { + Name: toLowerAndEscape(cProjectName + "_" + cNetwork + "_metrics"), + HostFilter: false, + ScrapeConfigs: scrapeConfigs, + }, + }, + }, + Integrations: map[string]interface{}{ + "agent": ToIntegrate{Enabled: false}, + "node_exporter": ToIntegrate{Enabled: true}, + }, + } + + if isEnableLogs { + // Include only Loki-specific LogClient + config.Logs = LogsConfig{ + Configs: []LogConfig{ + { + Name: "telescope_logs", + Clients: []LogClient{ + { + URL: cLogsSinkURL, + BasicAuth: BasicAuth{ + Username: cLokiUsername, + Password: cLokiPassword, + }, + ExternalLabels: map[string]string{ + "project_id": cProjectId, + "project_name": cProjectName, + }, + }, + }, + Positions: Positions{ + Filename: "/tmp/telescope_logs", + }, + }, + } + } + + return config } func LoadNetworkConfig() string { - config := generateNetworkConfig() + config := generateNetworkConfig() - // Create a custom marshaler - var buf bytes.Buffer - encoder := yaml.NewEncoder(&buf) - encoder.SetIndent(2) + // Create a custom marshaler + var buf bytes.Buffer + encoder := yaml.NewEncoder(&buf) + encoder.SetIndent(2) - if err := encoder.Encode(&config); err != nil { - log.Fatalf("error marshaling to YAML: %v", err) - } + if err := encoder.Encode(&config); err != nil { + log.Fatalf("error marshaling to YAML: %v", err) + } - data := buf.String() - // fixedData := strings.ReplaceAll(string(data), "job_name", "job_name") - // fixedData = strings.ReplaceAll(fixedData, "static_configs", "static_configs") + data := buf.String() + // fixedData := strings.ReplaceAll(string(data), "job_name", "job_name") + // fixedData = strings.ReplaceAll(fixedData, "static_configs", "static_configs") - configFilePath := "telescope_config.yaml" - if err := os.WriteFile(configFilePath, []byte(data), 0644); err != nil { - log.Fatalf("error writing to file: %v", err) - } + configFilePath := "telescope_config.yaml" + if err := os.WriteFile(configFilePath, []byte(data), 0644); err != nil { + log.Fatalf("error writing to file: %v", err) + } - return configFilePath + return configFilePath } func checkRequiredFlags() error { - requiredFlags := []string{ - "metrics", - "enable-logs", - "network", - "project-id", - "project-name", - "telescope-username", - "telescope-password", - "remote-write-url", - "telescope-loki-username", // New required flag - "telescope-loki-password", // New required flag - } - missingFlags := []string{} - - for _, flag := range requiredFlags { - if viper.GetString(flag) == "" { - missingFlags = append(missingFlags, flag) - } - } - - if len(missingFlags) > 0 && viper.GetString("config-file") == "" { - return fmt.Errorf("error: missing required flags: %s", strings.Join(missingFlags, ", ")) - } - - return nil + requiredFlags := []string{ + "metrics", + "enable-logs", + "network", + "project-id", + "project-name", + "telescope-username", + "telescope-password", + "remote-write-url", + "telescope-loki-username", // New required flag + "telescope-loki-password", // New required flag + } + missingFlags := []string{} + + for _, flag := range requiredFlags { + if viper.GetString(flag) == "" && !viper.GetBool(flag) { + missingFlags = append(missingFlags, flag) + } + } + + if len(missingFlags) > 0 && viper.GetString("config-file") == "" { + return fmt.Errorf("error: missing required flags: %s", strings.Join(missingFlags, ", ")) + } + + return nil } func init() { - prometheus.MustRegister(build.NewCollector("agent")) - cobra.OnInitialize(initConfig) - cmd.SetHelpFunc(helperFunction) - cmd.Flags().StringVar(&cfgFile, "config-file", "", "Specify the config file") - cmd.Flags().Bool("metrics", true, "Enable metrics") - cmd.Flags().Bool("enable-logs", false, "Enable logs") - cmd.Flags().String("network", "", "Specify the network") - cmd.Flags().String("project-id", "", "Specify the project ID") - cmd.Flags().String("project-name", "", "Specify the project name") - cmd.Flags().String("telescope-username", "", "Specify the telescope username") - cmd.Flags().String("telescope-password", "", "Specify the telescope password") - cmd.Flags().String("remote-write-url", "", "Specify the remote write URL") - cmd.Flags().String("logs-sink-url", "", "Specify the Log Sink URL") - - // Define new flags for Loki credentials - cmd.Flags().String("telescope-loki-username", "", "Specify the Loki username") // New flag - cmd.Flags().String("telescope-loki-password", "", "Specify the Loki password") // New flag - - // Bind flags with Viper - viper.BindPFlag("config-file", cmd.Flags().Lookup("config-file")) - viper.BindPFlag("metrics", cmd.Flags().Lookup("metrics")) - viper.BindPFlag("enable-logs", cmd.Flags().Lookup("enable-logs")) - viper.BindPFlag("network", cmd.Flags().Lookup("network")) - viper.BindPFlag("project-id", cmd.Flags().Lookup("project-id")) - viper.BindPFlag("project-name", cmd.Flags().Lookup("project-name")) - viper.BindPFlag("telescope-username", cmd.Flags().Lookup("telescope-username")) - viper.BindPFlag("telescope-password", cmd.Flags().Lookup("telescope-password")) - viper.BindPFlag("remote-write-url", cmd.Flags().Lookup("remote-write-url")) - viper.BindPFlag("logs-sink-url", cmd.Flags().Lookup("logs-sink-url")) - - // Bind new Loki flags - viper.BindPFlag("telescope-loki-username", cmd.Flags().Lookup("telescope-loki-username")) - viper.BindPFlag("telescope-loki-password", cmd.Flags().Lookup("telescope-loki-password")) + prometheus.MustRegister(build.NewCollector("agent")) + cobra.OnInitialize(initConfig) + cmd.SetHelpFunc(helperFunction) + cmd.Flags().StringVar(&cfgFile, "config-file", "", "Specify the config file") + cmd.Flags().Bool("metrics", true, "Enable metrics") + cmd.Flags().Bool("enable-logs", false, "Enable logs") + cmd.Flags().String("network", "", "Specify the network") + cmd.Flags().String("project-id", "", "Specify the project ID") + cmd.Flags().String("project-name", "", "Specify the project name") + cmd.Flags().String("telescope-username", "", "Specify the telescope username") + cmd.Flags().String("telescope-password", "", "Specify the telescope password") + cmd.Flags().String("remote-write-url", "", "Specify the remote write URL") + cmd.Flags().String("logs-sink-url", "", "Specify the Log Sink URL") + + // Define new flags for Loki credentials + cmd.Flags().String("telescope-loki-username", "", "Specify the Loki username") // New flag + cmd.Flags().String("telescope-loki-password", "", "Specify the Loki password") // New flag + + // Bind flags with Viper + viper.BindPFlag("config-file", cmd.Flags().Lookup("config-file")) + viper.BindPFlag("metrics", cmd.Flags().Lookup("metrics")) + viper.BindPFlag("enable-logs", cmd.Flags().Lookup("enable-logs")) + viper.BindPFlag("network", cmd.Flags().Lookup("network")) + viper.BindPFlag("project-id", cmd.Flags().Lookup("project-id")) + viper.BindPFlag("project-name", cmd.Flags().Lookup("project-name")) + viper.BindPFlag("telescope-username", cmd.Flags().Lookup("telescope-username")) + viper.BindPFlag("telescope-password", cmd.Flags().Lookup("telescope-password")) + viper.BindPFlag("remote-write-url", cmd.Flags().Lookup("remote-write-url")) + viper.BindPFlag("logs-sink-url", cmd.Flags().Lookup("logs-sink-url")) + + // Bind new Loki flags + viper.BindPFlag("telescope-loki-username", cmd.Flags().Lookup("telescope-loki-username")) + viper.BindPFlag("telescope-loki-password", cmd.Flags().Lookup("telescope-loki-password")) } func initConfig() { - if cfgFile != "" { - // Use config file from the flag. - viper.SetConfigFile(cfgFile) - handleErr(viper.ReadInConfig(), "Failed to read config file") - } else { - viper.AutomaticEnv() // read in environment variables that match - } + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + handleErr(viper.ReadInConfig(), "Failed to read config file") + } else { + viper.AutomaticEnv() // read in environment variables that match + } } func (c *TelescopeConfig) loadConfig() error { - if viper.GetString("config-file") != "" { - cfgFile = viper.GetString("config-file") - viper.SetConfigFile(cfgFile) - if err := viper.ReadInConfig(); err != nil { - return fmt.Errorf("error reading config file: %s", err) - } - } else { - LoadNetworkConfig() - } - - // Check for required fields only if config-file is not provided - if err := checkRequiredFlags(); err != nil { - return err - } - - // Assign the fields from Viper to the struct - c.Metrics = viper.GetBool("metrics") - c.Logs = viper.GetBool("enable-logs") - c.Network = viper.GetString("network") - c.ProjectId = viper.GetString("project-id") - c.ProjectName = viper.GetString("project-name") - c.TelescopeUsername = viper.GetString("telescope-username") - c.TelescopePassword = viper.GetString("telescope-password") - c.RemoteWriteUrl = viper.GetString("remote-write-url") - c.LokiUsername = viper.GetString("telescope-loki-username") - c.LokiPassword = viper.GetString("telescope-loki-password") - c.LogsSinkURL = viper.GetString("logs-sink-url") - - return nil + if viper.GetString("config-file") != "" { + cfgFile = viper.GetString("config-file") + viper.SetConfigFile(cfgFile) + if err := viper.ReadInConfig(); err != nil { + return fmt.Errorf("Error reading config file: %s", err) + } + } else { + LoadNetworkConfig() + } + + // Check for required fields only if config-file is not provided + if err := checkRequiredFlags(); err != nil { + return err + } + + // Assign the fields from Viper to the struct + c.Metrics = viper.GetBool("metrics") + c.Logs = viper.GetBool("enable-logs") + c.Network = viper.GetString("network") + c.ProjectId = viper.GetString("project-id") + c.ProjectName = viper.GetString("project-name") + c.TelescopeUsername = viper.GetString("telescope-username") + c.TelescopePassword = viper.GetString("telescope-password") + c.RemoteWriteUrl = viper.GetString("remote-write-url") + c.LokiUsername = viper.GetString("telescope-loki-username") + c.LokiPassword = viper.GetString("telescope-loki-password") + c.LogsSinkURL = viper.GetString("logs-sink-url") + + return nil } func agent(configPath string) { - defaultCfg := server.DefaultConfig() - logger := server.NewLogger(&defaultCfg) + defaultCfg := server.DefaultConfig() + logger := server.NewLogger(&defaultCfg) - reloader := func(log *server.Logger) (*config.Config, error) { - fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError) - // fs.String("telescope", configPath, "Path to configuration file") - // fs.Parse([]string{"--config.file=" + configPath}) // Ensure the flag is set - return config.Load(fs, []string{"-config.file", configPath}, log) - } - cfg, err := reloader(logger) - if err != nil { - log.Fatalln(err) - } + reloader := func(log *server.Logger) (*config.Config, error) { + fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError) + // fs.String("telescope", configPath, "Path to configuration file") + // fs.Parse([]string{"--config.file=" + configPath}) // Ensure the flag is set + return config.Load(fs, []string{"-config.file", configPath}, log) + } + cfg, err := reloader(logger) + if err != nil { + log.Fatalln(err) + } - // After this point we can start using go-kit logging. - logger = server.NewLogger(cfg.Server) - util_log.Logger = logger + // After this point we can start using go-kit logging. + logger = server.NewLogger(cfg.Server) + util_log.Logger = logger - level.Info(logger).Log("boringcrypto enabled", boringcrypto.Enabled) - ep, err := NewEntrypoint(logger, cfg, reloader) - if err != nil { - level.Error(logger).Log("msg", "error creating the agent server entrypoint", "err", err) - os.Exit(1) - } + level.Info(logger).Log("boringcrypto enabled", boringcrypto.Enabled) + ep, err := NewEntrypoint(logger, cfg, reloader) + if err != nil { + level.Error(logger).Log("msg", "error creating the agent server entrypoint", "err", err) + os.Exit(1) + } - if err = ep.Start(); err != nil { - level.Error(logger).Log("msg", "error running agent", "err", err) - // Don't os.Exit here; we want to do cleanup by stopping promMetrics - } + if err = ep.Start(); err != nil { + level.Error(logger).Log("msg", "error running agent", "err", err) + // Don't os.Exit here; we want to do cleanup by stopping promMetrics + } - ep.Stop() - level.Info(logger).Log("msg", "agent exiting") + ep.Stop() + level.Info(logger).Log("msg", "agent exiting") } func main() { @@ -595,4 +591,4 @@ func main() { fmt.Printf("Configuration written to %s\n", configFilePath) agent(configFilePath) -} \ No newline at end of file +} From 935b853f768e8668d295615ff8def0e7e3e1b597 Mon Sep 17 00:00:00 2001 From: segunjkf Date: Tue, 8 Oct 2024 11:00:09 -0400 Subject: [PATCH 7/8] wip: fix build error Signed-off-by: segunjkf --- cmd/agent/main.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 1fe6aa7..02d2c60 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -51,11 +51,11 @@ var cmd = &cobra.Command{ } // Proceed to generate and write configuration, then start the agent - network = viper.GetString("network") + network := viper.GetString("network") projectName := viper.GetString("project-name") networkConfig := getNetworkConfig(network) - fmt.Println("Starting Telescope Agent", networkConfig) + fmt.Println("Starting Telescope Agent with network config:", networkConfig) scrapeConfigs := networkConfig.GenerateScrapeConfigs(projectName, network) fullConfig := generateFullConfig(config, scrapeConfigs) configFilePath := "telescope_config.yaml" @@ -214,10 +214,9 @@ func generateFullConfig(config TelescopeConfig, networkScrapeConfigs []networksC cTelescopeUsername := viper.GetString("telescope-username") cTelescopePassword := viper.GetString("telescope-password") cRemoteWriteUrl := viper.GetString("remote-write-url") - // isEnableLogs := viper.GetBool("enable-logs") - // cLogsSinkURL := viper.GetString("logs-sink-url") - // cLokiUsername := viper.GetString("telescope-loki-username") - // cLokiPassword := viper.GetString("telescope-loki-password") + // cLogsSinkURL := viper.GetString("logs-sink-url") + // cLokiUsername := viper.GetString("telescope-loki-username") + // cLokiPassword := viper.GetString("telescope-loki-password") // Convert networksConfig.ScrapeConfig to local ScrapeConfig scrapeConfigs := make([]ScrapeConfig, len(networkScrapeConfigs)) @@ -390,6 +389,7 @@ func generateNetworkConfig() Config { }, Positions: Positions{ Filename: "/tmp/telescope_logs", + }, }, }, } @@ -438,7 +438,7 @@ func checkRequiredFlags() error { missingFlags := []string{} for _, flag := range requiredFlags { - if viper.GetString(flag) == "" && !viper.GetBool(flag) { + if viper.GetString(flag) == "" && !viper.GetBool(flag) { // For bool flags missingFlags = append(missingFlags, flag) } } @@ -482,8 +482,8 @@ func init() { viper.BindPFlag("logs-sink-url", cmd.Flags().Lookup("logs-sink-url")) // Bind new Loki flags - viper.BindPFlag("telescope-loki-username", cmd.Flags().Lookup("telescope-loki-username")) - viper.BindPFlag("telescope-loki-password", cmd.Flags().Lookup("telescope-loki-password")) + viper.BindPFlag("telescope-loki-username", cmd.Flags().Lookup("telescope-loki-username")) // New binding + viper.BindPFlag("telescope-loki-password", cmd.Flags().Lookup("telescope-loki-password")) // New binding } func initConfig() { @@ -503,8 +503,10 @@ func (c *TelescopeConfig) loadConfig() error { if err := viper.ReadInConfig(); err != nil { return fmt.Errorf("Error reading config file: %s", err) } + log.Println("Configuration loaded from file:", cfgFile) } else { LoadNetworkConfig() + log.Println("Configuration generated and written to telescope_config.yaml") } // Check for required fields only if config-file is not provided @@ -523,7 +525,7 @@ func (c *TelescopeConfig) loadConfig() error { c.RemoteWriteUrl = viper.GetString("remote-write-url") c.LokiUsername = viper.GetString("telescope-loki-username") c.LokiPassword = viper.GetString("telescope-loki-password") - c.LogsSinkURL = viper.GetString("logs-sink-url") + c.LogsSinkURL = viper.GetString("logs-sink-url") return nil } From 77a6de60bf5b77bf66e555d2580021934ee7bea0 Mon Sep 17 00:00:00 2001 From: segunjkf Date: Tue, 8 Oct 2024 11:46:50 -0400 Subject: [PATCH 8/8] wip: revert test push Signed-off-by: segunjkf --- .github/workflows/docker.yml | 6 ++---- cmd/agent/main.go | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index c9dd631..a68d2e5 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -70,9 +70,7 @@ jobs: with: file: ./cmd/agent/Dockerfile tags: ${{ steps.meta.outputs.tags }} - labels: test - push: true + labels: ${{ steps.meta.outputs.labels }} + push: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') }} # Compile Docker image only for AMD64 for a regular PR to save some CI time. platforms: ${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) && 'linux/amd64,linux/arm64' || 'linux/amd64' }} - - diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 02d2c60..7483035 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -62,7 +62,7 @@ var cmd = &cobra.Command{ if err := writeConfigToFile(fullConfig, configFilePath); err != nil { log.Fatalf("Failed to write config to file: %v", err) } - + fmt.Printf("Configuration written to %s\n", configFilePath) agent(configFilePath)