Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
Merge pull request #207 from DataDog/nicolas/listening-port
Browse files Browse the repository at this point in the history
change the listening port
  • Loading branch information
galdor authored Feb 2, 2017
2 parents c03b93c + b88cce2 commit 4cabe01
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ RUN echo "deb http://apt-trace.datad0g.com.s3.amazonaws.com/ stable main" > /etc
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

EXPOSE 7777/tcp
EXPOSE 7777/tcp 8126/tcp

ENTRYPOINT ["/opt/datadog-agent/bin/trace-agent"]
15 changes: 12 additions & 3 deletions agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func handleSignal(exit chan struct{}) {
}
}

// die logs an error message and makes the program exit immediately.
func die(format string, args ...interface{}) {
log.Errorf(format, args...)
log.Flush()
os.Exit(1)
}

// opts are the command-line options
var opts struct {
ddConfigFile string
Expand Down Expand Up @@ -126,6 +133,7 @@ func main() {
legacyConf, err := config.NewIfExists(opts.configFile)
if err != nil {
log.Errorf("%s: %v", opts.configFile, err)
log.Warnf("ignoring %s", opts.configFile)
}
if legacyConf != nil {
log.Infof("using legacy configuration from %s", opts.configFile)
Expand All @@ -134,14 +142,15 @@ func main() {
conf, err := config.NewIfExists(opts.ddConfigFile)
if err != nil {
log.Errorf("%s: %v", opts.ddConfigFile, err)
log.Warnf("ignoring %s", opts.ddConfigFile)
}
if conf != nil {
log.Infof("using configuration from %s", opts.ddConfigFile)
}

agentConf, err = config.NewAgentConfig(conf, legacyConf)
if err != nil {
panic(err)
die("%v", err)
}

// Exit if tracing is not enabled
Expand All @@ -164,13 +173,13 @@ func main() {
// replace the default logger
err = config.NewLoggerLevelCustom(level, agentConf.LogFilePath)
if err != nil {
panic(fmt.Errorf("error with logger: %v", err))
die("cannot create logger: %v", err)
}

// Initialize dogstatsd client
err = statsd.Configure(agentConf)
if err != nil {
fmt.Printf("Error configuring dogstatsd: %v", err)
die("cannot configure dogstatsd: %v", err)
}

// Seed rand
Expand Down
46 changes: 34 additions & 12 deletions agent/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
log "github.com/cihub/seelog"
)

// The trace agent used to listen on port 7777, but now uses port 8126. Keep
// listening on 7777 during the transition.
const legacyReceiverPort = 7777

// APIVersion is a dumb way to version our collector handlers
type APIVersion int

Expand Down Expand Up @@ -87,29 +91,47 @@ func (r *HTTPReceiver) Run() {
http.HandleFunc("/v0.3/services", r.httpHandleWithVersion(v03, r.handleServices))

addr := fmt.Sprintf("%s:%d", r.conf.ReceiverHost, r.conf.ReceiverPort)
log.Infof("listening for traces at http://%s/", addr)
if err := r.Listen(addr, ""); err != nil {
die("%v", err)
}

legacyAddr := fmt.Sprintf("%s:%d", r.conf.ReceiverHost, legacyReceiverPort)
if err := r.Listen(legacyAddr, " (legacy)"); err != nil {
log.Error(err)
}

go r.logStats()
}

tcpL, err := net.Listen("tcp", addr)
// Listen creates a new HTTP server listening on the provided address.
func (r *HTTPReceiver) Listen(addr, logExtra string) error {
listener, err := net.Listen("tcp", addr)
if err != nil {
log.Error("could not create TCP listener")
panic(err)
return fmt.Errorf("cannot listen on %s: %v", addr, err)
}

sl, err := NewStoppableListener(tcpL, r.exit, r.conf.ConnectionLimit)
// some clients might use keep-alive and keep open their connections too long
// avoid leaks
timeout := 5
stoppableListener, err := NewStoppableListener(listener, r.exit,
r.conf.ConnectionLimit)
if err != nil {
return fmt.Errorf("cannot create stoppable listener: %v", err)
}

timeout := 5 * time.Second
if r.conf.ReceiverTimeout > 0 {
timeout = r.conf.ReceiverTimeout
timeout = time.Duration(r.conf.ReceiverTimeout) * time.Second
}

server := http.Server{
ReadTimeout: time.Second * time.Duration(timeout),
WriteTimeout: time.Second * time.Duration(timeout),
}

go r.logStats()
go sl.Refresh(r.conf.ConnectionLimit)
go server.Serve(sl)
log.Infof("listening for traces at http://%s%s", addr, logExtra)

go stoppableListener.Refresh(r.conf.ConnectionLimit)
go server.Serve(stoppableListener)

return nil
}

func (r *HTTPReceiver) httpHandle(fn http.HandlerFunc) http.HandlerFunc {
Expand Down
2 changes: 1 addition & 1 deletion agent/trace-agent.ini
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ oldest_span_cutoff_seconds=30
###################################################
[trace.receiver]
# the port that the Receiver should listen
receiver_port=7777
receiver_port=8126
# how many unique connections to allow during one 30 second lease period
connection_limit=2000
2 changes: 1 addition & 1 deletion config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ max_traces_per_second=10
[trace.receiver]
# the port that the Receiver should listen on
receiver_port=7777
receiver_port=8126
# how many unique client connections to allow during one 30 second lease period
connection_limit=2000
Expand Down
2 changes: 1 addition & 1 deletion config/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func NewDefaultAgentConfig() *AgentConfig {
MaxTPS: 10,

ReceiverHost: "localhost",
ReceiverPort: 7777,
ReceiverPort: 8126,
ConnectionLimit: 2000,

StatsdHost: "localhost",
Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestDefaultConfig(t *testing.T) {

// assert that some sane defaults are set
assert.Equal(agentConfig.ReceiverHost, "localhost")
assert.Equal(agentConfig.ReceiverPort, 7777)
assert.Equal(agentConfig.ReceiverPort, 8126)

assert.Equal(agentConfig.StatsdHost, "localhost")
assert.Equal(agentConfig.StatsdPort, 8125)
Expand Down

0 comments on commit 4cabe01

Please sign in to comment.