Skip to content

Commit

Permalink
Support JAEGER_AGENT_HOST/PORT variables
Browse files Browse the repository at this point in the history
This makes it much easier to support different deployment strategies
for the agent. Partial support for jaegertracing#206.

Signed-off-by: Robert Collins <[email protected]>
  • Loading branch information
Robert Collins committed Oct 5, 2017
1 parent 3ad49a1 commit 2ed1651
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
21 changes: 20 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"io"
"os"
"strings"
"time"

Expand Down Expand Up @@ -85,6 +86,7 @@ type ReporterConfig struct {
LogSpans bool `yaml:"logSpans"`

// LocalAgentHostPort instructs reporter to send spans to jaeger-agent at this address
// May be configured via the environment variables JAEGER_AGENT_HOST and JAEGER_AGENT_PORT
LocalAgentHostPort string `yaml:"localAgentHostPort"`
}

Expand Down Expand Up @@ -282,6 +284,23 @@ func (rc *ReporterConfig) NewReporter(
return reporter, err
}

// To permit clean testing.
var newUDPTransport = jaeger.NewUDPTransport

func (rc *ReporterConfig) newTransport() (jaeger.Transport, error) {
return jaeger.NewUDPTransport(rc.LocalAgentHostPort, 0)
hostPort := ""
if len(rc.LocalAgentHostPort) == 0 {
host := os.Getenv("JAEGER_AGENT_HOST")
port := os.Getenv("JAEGER_AGENT_PORT")
if len(host) == 0 {
host = "localhost"
}
if len(port) == 0 {
port = "6831"
}
hostPort = fmt.Sprintf("%s:%s", host, port)
} else {
hostPort = rc.LocalAgentHostPort
}
return newUDPTransport(hostPort, 0)
}
24 changes: 24 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package config

import (
"os"
"testing"
"time"

Expand Down Expand Up @@ -104,6 +105,29 @@ func TestNewReporterError(t *testing.T) {
require.Error(t, err)
}

func TestReporterHostFromEnvironment(t *testing.T) {
oldHost := os.Getenv("JAEGER_AGENT_HOST")
defer os.Setenv("JAEGER_AGENT_HOST", oldHost)
os.Setenv("JAEGER_AGENT_HOST", "127.0.0.2")
oldNewUDPTransport := newUDPTransport
defer func() { newUDPTransport = oldNewUDPTransport }()
usedHostPort := ""
newUDPTransport = func(hostPort string, maxPacketSize int) (jaeger.Transport, error) {
usedHostPort = hostPort
return oldNewUDPTransport(hostPort, maxPacketSize)
}

cfg := Configuration{
// We want to permit environment variables to work even when
// some code based config has been done.
Reporter: &ReporterConfig{},
}
_, closer, err := cfg.New("testService")
defer closer.Close()
require.NoError(t, err)
assert.Equal(t, "127.0.0.2:6831", usedHostPort)
}

func TestInitGlobalTracer(t *testing.T) {
// Save the existing GlobalTracer and replace after finishing function
prevTracer := opentracing.GlobalTracer()
Expand Down

0 comments on commit 2ed1651

Please sign in to comment.