Skip to content

Commit

Permalink
Merge pull request #784 from linxiulei/fix
Browse files Browse the repository at this point in the history
prom-to-sd: Fix not respecting --api-override
  • Loading branch information
CatherineF-dev authored Oct 18, 2024
2 parents 46dd9ce + 9f93522 commit bf6d5f1
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions prometheus-to-sd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -149,6 +150,15 @@ func main() {
options = append(options, option.WithTokenSource(ts))
}

if *apioverride != "" {
// option.WithEndpoint() only works with "host:port"
endpoint, err := getEndpoint(*apioverride)
if err != nil {
glog.Fatalf("Error parsing --api-override: %s, with error: %v", *apioverride, err)
}
options = append(options, option.WithEndpoint(endpoint))
}

ctx := context.Background()
client, err := monitoring.NewMetricClient(ctx, options...)
if err != nil {
Expand Down Expand Up @@ -259,3 +269,32 @@ func parseMonitoredResourceLabels(monitoredResourceLabelsStr string) map[string]
}
return labels
}

func getEndpoint(input string) (string, error) {
u, err := url.Parse(fixScheme(input))
if err != nil {
return "", fmt.Errorf("Failed to parse api override: %v", err)
}
port := u.Port()
if port == "" {
switch u.Scheme {
case "https":
port = "443"
case "http":
port = "80"
case "":
port = "80"
default:
return "", fmt.Errorf("Unrecognized scheme: %s in --api-override", u.Scheme)
}
return fmt.Sprintf("%s:%s", u.Host, port), nil
}
return u.Host, nil
}

func fixScheme(baseURL string) string {
if !strings.Contains(baseURL, "://") {
return "https://" + baseURL
}
return baseURL
}

0 comments on commit bf6d5f1

Please sign in to comment.