Skip to content

Commit

Permalink
Add debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Acosta committed Feb 5, 2016
1 parent 00cd193 commit 12b633f
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 119 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ Then, run the local build via

Scope has a collection of built in debugging tools to aid Scope delevopers.

- To get debug information in the logs launch Scope with `--debug`:
```
scope launch --debug
docker logs weavescope
```

- To have the Scope App or Scope Probe dump their goroutine stacks, run:
```
kill -QUIT $(pgrep -f scope-(app|probe))
Expand Down
2 changes: 1 addition & 1 deletion app/api_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func handleWebsocket(
) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
// log.Println("Upgrade:", err)
// Log.Info("Upgrade:", err)
return
}
defer conn.Close()
Expand Down
6 changes: 3 additions & 3 deletions app/controls.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package app

import (
"log"
"math/rand"
"net/http"
"net/rpc"
"sync"

"github.com/gorilla/mux"

. "github.com/weaveworks/scope/common/log"
"github.com/weaveworks/scope/common/xfer"
)

Expand Down Expand Up @@ -73,7 +73,7 @@ func (cr *controlRouter) handleControl(w http.ResponseWriter, r *http.Request) {
)
handler, ok := cr.get(probeID)
if !ok {
log.Printf("Probe %s is not connected right now...", probeID)
Log.Errorf("Probe %s is not connected right now...", probeID)
http.NotFound(w, r)
return
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func (cr *controlRouter) handleProbeWS(w http.ResponseWriter, r *http.Request) {

conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Error upgrading to websocket: %v", err)
Log.Errorf("Error upgrading to websocket: %v", err)
return
}
defer conn.Close()
Expand Down
10 changes: 5 additions & 5 deletions app/pipes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package app

import (
"io"
"log"
"net/http"
"sync"
"time"

"github.com/gorilla/mux"

. "github.com/weaveworks/scope/common/log"
"github.com/weaveworks/scope/common/mtime"
"github.com/weaveworks/scope/common/xfer"
)
Expand Down Expand Up @@ -99,7 +99,7 @@ func (pr *PipeRouter) timeout() {

if (pipe.ui.refCount == 0 && now.Sub(pipe.ui.lastUsedTime) >= pipeTimeout) ||
(pipe.probe.refCount == 0 && now.Sub(pipe.probe.lastUsedTime) >= pipeTimeout) {
log.Printf("Timing out pipe %s", id)
Log.Infof("Timing out pipe %s", id)
pipe.Close()
pipe.tombstoneTime = now
}
Expand All @@ -122,7 +122,7 @@ func (pr *PipeRouter) getOrCreate(id string) (*pipe, bool) {
defer pr.Unlock()
p, ok := pr.pipes[id]
if !ok {
log.Printf("Creating pipe id %s", id)
Log.Infof("Creating pipe id %s", id)
p = &pipe{
ui: end{lastUsedTime: mtime.Now()},
probe: end{lastUsedTime: mtime.Now()},
Expand Down Expand Up @@ -178,7 +178,7 @@ func (pr *PipeRouter) handleWs(endSelector func(*pipe) (*end, io.ReadWriter)) fu

conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Error upgrading to websocket: %v", err)
Log.Errorf("Error upgrading to websocket: %v", err)
return
}
defer conn.Close()
Expand All @@ -191,7 +191,7 @@ func (pr *PipeRouter) delete(w http.ResponseWriter, r *http.Request) {
pipeID := mux.Vars(r)["pipeID"]
pipe, ok := pr.getOrCreate(pipeID)
if ok && pr.retain(pipeID, pipe, &pipe.ui) {
log.Printf("Closing pipe %s", pipeID)
Log.Infof("Closing pipe %s", pipeID)
pipe.Close()
pipe.tombstoneTime = mtime.Now()
pr.release(pipeID, pipe, &pipe.ui)
Expand Down
5 changes: 3 additions & 2 deletions app/server_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package app

import (
"encoding/json"
"log"
"net/http"

. "github.com/weaveworks/scope/common"
)

func respondWith(w http.ResponseWriter, code int, response interface{}) {
w.Header().Set("Content-Type", "application/json")
w.Header().Add("Cache-Control", "no-cache")
w.WriteHeader(code)
if err := json.NewEncoder(w).Encode(response); err != nil {
log.Print(err)
Log.Error(err)
}
}
39 changes: 39 additions & 0 deletions common/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package log

// Logger for Scope piggi-backing on github.com/weaveworks/weave/common.Log

import (
"github.com/Sirupsen/logrus"

weavecommon "github.com/weaveworks/weave/common"
)

var (
prefix []byte
Log *logrus.Logger
)

type prefixFormatter struct {
next logrus.Formatter
}

func (f *prefixFormatter) Format(entry *logrus.Entry) ([]byte, error) {
log, err := f.next.Format(entry)
if err != nil {
return log, err
}
return append(prefix, log...), nil
}

func init() {
Log = weavecommon.Log
Log.Formatter = &prefixFormatter{Log.Formatter}
}

func SetLogLevel(levelname string) {
weavecommon.SetLogLevel(levelname)
}

func SetLogPrefix(s string) {
prefix = []byte(s)
}
5 changes: 3 additions & 2 deletions common/sanitize/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package sanitize

import (
"fmt"
"log"
"net"
"net/url"
"strings"

. "github.com/weaveworks/scope/common/log"
)

// URL returns a function that sanitizes a URL string. It lets underspecified
Expand All @@ -23,7 +24,7 @@ func URL(defaultScheme string, defaultPort int, defaultPath string) func(string)
}
u, err := url.Parse(s)
if err != nil {
log.Printf("%q: %v", s, err)
Log.Errorf("%q: %v", s, err)
return s // oh well
}
if _, port, err := net.SplitHostPort(u.Host); err != nil && defaultPort > 0 {
Expand Down
18 changes: 9 additions & 9 deletions probe/appclient/app_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/rpc"
"sync"
"time"

"github.com/gorilla/websocket"

. "github.com/weaveworks/scope/common/log"
"github.com/weaveworks/scope/common/sanitize"
"github.com/weaveworks/scope/common/xfer"
)
Expand Down Expand Up @@ -170,7 +170,7 @@ func (c *appClient) doWithBackoff(msg string, f func() (bool, error)) {
continue
}

log.Printf("Error doing %s for %s, backing off %s: %v", msg, c.target, backoff, err)
Log.Errorf("Error doing %s for %s, backing off %s: %v", msg, c.target, backoff, err)
select {
case <-time.After(backoff):
case <-c.quit:
Expand All @@ -192,7 +192,7 @@ func (c *appClient) controlConnection() (bool, error) {
return false, err
}
defer func() {
log.Printf("Closing control connection to %s", c.target)
Log.Infof("Closing control connection to %s", c.target)
conn.Close()
}()

Expand All @@ -214,8 +214,8 @@ func (c *appClient) controlConnection() (bool, error) {

func (c *appClient) ControlConnection() {
go func() {
log.Printf("Control connection to %s starting", c.target)
defer log.Printf("Control connection to %s exiting", c.target)
Log.Infof("Control connection to %s starting", c.target)
defer Log.Infof("Control connection to %s exiting", c.target)
c.doWithBackoff("controls", c.controlConnection)
}()
}
Expand All @@ -242,8 +242,8 @@ func (c *appClient) publish(r io.Reader) error {

func (c *appClient) startPublishing() {
go func() {
log.Printf("Publish loop for %s starting", c.target)
defer log.Printf("Publish loop for %s exiting", c.target)
Log.Infof("Publish loop for %s starting", c.target)
defer Log.Infof("Publish loop for %s exiting", c.target)
c.doWithBackoff("publish", func() (bool, error) {
r := <-c.readers
if r == nil {
Expand Down Expand Up @@ -291,8 +291,8 @@ func (c *appClient) pipeConnection(id string, pipe xfer.Pipe) (bool, error) {

func (c *appClient) PipeConnection(id string, pipe xfer.Pipe) {
go func() {
log.Printf("Pipe %s connection to %s starting", id, c.target)
defer log.Printf("Pipe %s connection to %s exiting", id, c.target)
Log.Infof("Pipe %s connection to %s starting", id, c.target)
defer Log.Infof("Pipe %s connection to %s exiting", id, c.target)
c.doWithBackoff(id, func() (bool, error) {
return c.pipeConnection(id, pipe)
})
Expand Down
6 changes: 3 additions & 3 deletions probe/appclient/multi_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"strings"
"sync"

. "github.com/weaveworks/scope/common/log"
"github.com/weaveworks/scope/common/xfer"
"github.com/weaveworks/scope/report"
)
Expand Down Expand Up @@ -75,13 +75,13 @@ func (c *multiClient) Set(hostname string, endpoints []string) {

client, err := c.clientFactory(hostname, endpoint)
if err != nil {
log.Printf("Error creating new app client: %v", err)
Log.Errorf("Error creating new app client: %v", err)
return
}

details, err := client.Details()
if err != nil {
log.Printf("Error fetching app details: %v", err)
Log.Errorf("Error fetching app details: %v", err)
}

clients <- clientTuple{details, client}
Expand Down
4 changes: 2 additions & 2 deletions probe/appclient/resolver.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package appclient

import (
"log"
"net"
"strconv"
"strings"
"time"

. "github.com/weaveworks/scope/common/log"
"github.com/weaveworks/scope/common/xfer"
)

Expand Down Expand Up @@ -74,7 +74,7 @@ func prepare(strs []string) []target {
var err error
host, port, err = net.SplitHostPort(s)
if err != nil {
log.Printf("invalid address %s: %v", s, err)
Log.Errorf("invalid address %s: %v", s, err)
continue
}
} else {
Expand Down
18 changes: 9 additions & 9 deletions probe/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
Expand All @@ -17,6 +16,7 @@ import (

docker "github.com/fsouza/go-dockerclient"

. "github.com/weaveworks/scope/common/log"
"github.com/weaveworks/scope/common/mtime"
"github.com/weaveworks/scope/report"
)
Expand Down Expand Up @@ -162,30 +162,30 @@ func (c *container) StartGatheringStats() error {
}

go func() {
log.Printf("docker container: collecting stats for %s", c.container.ID)
Log.Infof("docker container: collecting stats for %s", c.container.ID)
req, err := http.NewRequest("GET", fmt.Sprintf("/containers/%s/stats", c.container.ID), nil)
if err != nil {
log.Printf("docker container: %v", err)
Log.Errorf("docker container: %v", err)
return
}
req.Header.Set("User-Agent", "weavescope")

url, err := url.Parse(endpoint)
if err != nil {
log.Printf("docker container: %v", err)
Log.Errorf("docker container: %v", err)
return
}

dial, err := DialStub(url.Scheme, url.Path)
if err != nil {
log.Printf("docker container: %v", err)
Log.Errorf("docker container: %v", err)
return
}

conn := NewClientConnStub(dial, nil)
resp, err := conn.Do(req)
if err != nil {
log.Printf("docker container: %v", err)
Log.Errorf("docker container: %v", err)
return
}
defer resp.Body.Close()
Expand All @@ -198,21 +198,21 @@ func (c *container) StartGatheringStats() error {
c.Lock()
defer c.Unlock()

log.Printf("docker container: stopped collecting stats for %s", c.container.ID)
Log.Infof("docker container: stopped collecting stats for %s", c.container.ID)
c.statsConn = nil
}()

var stats docker.Stats
decoder := json.NewDecoder(resp.Body)
for err := decoder.Decode(&stats); err != io.EOF; err = decoder.Decode(&stats) {
if err != nil {
log.Printf("docker container: error reading event, did container stop? %v", err)
Log.Errorf("docker container: error reading event, did container stop? %v", err)
return
}

c.Lock()
if c.numPending >= len(c.pendingStats) {
log.Printf("docker container: dropping stats.")
Log.Warnf("docker container: dropping stats.")
} else {
c.latestStats = stats
c.pendingStats[c.numPending] = stats
Expand Down
Loading

0 comments on commit 12b633f

Please sign in to comment.