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

Fix senders.NewSender() for direct ingestion. #85

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
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
25 changes: 19 additions & 6 deletions senders/client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ type configuration struct {
SDKMetricsTags map[string]string
}

func (c *configuration) setDefaultPort(port int) {
c.MetricsPort = port
c.TracesPort = port
}

// NewSender creates Wavefront client
func NewSender(wfURL string, setters ...Option) (Sender, error) {
cfg, err := CreateConfig(wfURL, setters...)
Expand All @@ -67,22 +72,30 @@ func CreateConfig(wfURL string, setters ...Option) (*configuration, error) {
return nil, err
}

if !strings.HasPrefix(strings.ToLower(u.Scheme), "http") {
return nil, fmt.Errorf("invalid scheme '%s' in '%s', only 'http' is supported", u.Scheme, u)
}

if len(u.User.String()) > 0 {
cfg.Token = u.User.String()
u.User = nil
}

switch strings.ToLower(u.Scheme) {
case "http":
if cfg.Token != "" {
cfg.setDefaultPort(80)
}
case "https":
if cfg.Token != "" {
cfg.setDefaultPort(443)
}
default:
return nil, fmt.Errorf("invalid scheme '%s' in '%s', only 'http' is supported", u.Scheme, u)
}

if u.Port() != "" {
port, err := strconv.Atoi(u.Port())
if err != nil {
return nil, fmt.Errorf("unable to convert port to integer: %s", err)
}
cfg.MetricsPort = port
cfg.TracesPort = port
cfg.setDefaultPort(port)
u.Host = u.Hostname()
}
cfg.Server = u.String()
Expand Down
21 changes: 21 additions & 0 deletions senders/client_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ func TestScheme(t *testing.T) {
require.Error(t, err)
}

func TestDefaultPortsProxy(t *testing.T) {
cfg, err := senders.CreateConfig("http://localhost")
require.NoError(t, err)
assert.Equal(t, 2878, cfg.MetricsPort)
assert.Equal(t, 30001, cfg.TracesPort)
}

func TestDefaultPortsDIHttp(t *testing.T) {
cfg, err := senders.CreateConfig("http://11111111-2222-3333-4444-555555555555@localhost")
require.NoError(t, err)
assert.Equal(t, 80, cfg.MetricsPort)
assert.Equal(t, 80, cfg.TracesPort)
}

func TestDefaultPortsDIHttps(t *testing.T) {
cfg, err := senders.CreateConfig("https://11111111-2222-3333-4444-555555555555@localhost")
require.NoError(t, err)
assert.Equal(t, 443, cfg.MetricsPort)
assert.Equal(t, 443, cfg.TracesPort)
}

func TestPortExtractedFromURL(t *testing.T) {
cfg, err := senders.CreateConfig("http://localhost:1234")
require.NoError(t, err)
Expand Down