Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prom-to-sd: Fix not respecting --api-override #784

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
CatherineF-dev marked this conversation as resolved.
Show resolved Hide resolved
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
}
Loading