-
Notifications
You must be signed in to change notification settings - Fork 6
/
tracer.go
256 lines (223 loc) · 7.31 KB
/
tracer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package tracing
import (
"fmt"
"log"
"math/rand"
"reflect"
"sync"
"time"
"encoding/json"
"io/ioutil"
"net/rpc"
"github.com/DistributedClocks/GoVector/govec"
"github.com/DistributedClocks/GoVector/govec/vclock"
)
// TracingToken is an abstract token to be used when tracing
// message passing between network nodes.
//
// A one-time-use token can be retrieved using Trace.GenerateToken,
// and the "reception" of that token can be recorded using
// Tracer.ReceiveToken.
type TracingToken []byte
// TracerConfig contains the necessary configuration options for a tracer.
type TracerConfig struct {
ServerAddress string // address of the server to send traces to
TracerIdentity string // a unique string identifying the tracer
Secret []byte // TODO
}
// Tracer is the tracing client.
type Tracer struct {
lock sync.Mutex
identity string
client *rpc.Client
secret []byte
shouldPrint bool
logger *govec.GoLog
}
// NewTracerFromFile instantiates a fresh tracer client from a configuration file.
//
// Configuration is loaded from the JSON-formatted configFile, which should specify:
// - ServerAddress, an ip:port pair identifying a tracing server, as one might pass to rpc.Dial
// - TracerIdentity, a unique string giving the tracer an identity that tracks which tracer reported which action
// - Secret [TODO]
//
// Note that each instance of Tracer is thread-safe.
func NewTracerFromFile(configFile string) *Tracer {
configData, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatal("reading config file: ", err)
}
config := new(TracerConfig)
err = json.Unmarshal(configData, config)
if err != nil {
log.Fatal("parsing config data: ", err)
}
return NewTracer(*config)
}
// NewTracer instantiates a fresh tracer client.
func NewTracer(config TracerConfig) *Tracer {
client, err := rpc.Dial("tcp", config.ServerAddress)
if err != nil {
log.Fatal("dialing server: ", err)
}
goLogConfig := govec.GetDefaultConfig()
goLogConfig.LogToFile = false
// TODO: make this call optional
var initialVC vclock.VClock
err = client.Call("RPCProvider.GetLastVC", config.TracerIdentity, &initialVC)
if err == nil {
goLogConfig.InitialVC = initialVC.Copy()
}
tracer := &Tracer{
client: client,
identity: config.TracerIdentity,
shouldPrint: true,
logger: govec.InitGoVector(config.TracerIdentity,
"GoVector-"+config.TracerIdentity, goLogConfig),
}
return tracer
}
// NewTracer instantiates a fresh tracer client.
// Not calling Log.Fatal when rpc connection fails
func NewTracerNonFatal(config TracerConfig) *Tracer {
client, err := rpc.Dial("tcp", config.ServerAddress)
if err != nil {
return nil
}
goLogConfig := govec.GetDefaultConfig()
goLogConfig.LogToFile = false
// TODO: make this call optional
var initialVC vclock.VClock
err = client.Call("RPCProvider.GetLastVC", config.TracerIdentity, &initialVC)
if err == nil {
goLogConfig.InitialVC = initialVC.Copy()
}
tracer := &Tracer{
client: client,
identity: config.TracerIdentity,
shouldPrint: true,
logger: govec.InitGoVector(config.TracerIdentity,
"GoVector-"+config.TracerIdentity, goLogConfig),
}
return tracer
}
var (
seededIDGen = rand.New(rand.NewSource(time.Now().UnixNano()))
// NewSource returns a new pseudo-random Source seeded with the given value.
// Unlike the default Source used by top-level functions, this source is not
// safe for concurrent use by multiple goroutines. Hence the need for a mutex.
seededIDLock sync.Mutex
)
// CreateTrace is an action that indicates creation of a trace.
type CreateTrace struct{}
// CreateTrace creates a new trace object with a unique ID. Also, it records a
// CreateTrace action.
func (tracer *Tracer) CreateTrace() *Trace {
seededIDLock.Lock()
traceID := seededIDGen.Int63()
seededIDLock.Unlock()
trace := &Trace{
ID: uint64(traceID),
Tracer: tracer,
}
trace.RecordAction(CreateTrace{})
return trace
}
// getLogString returns a human-readable representation,
// of the form:
// [TracerID] TraceID=ID StructType field1=val1, field2=val2, ...
// Note that we are not logging vector clock, but we send it to the
// tracing server.
func (tracer *Tracer) getLogString(trace *Trace, record interface{}) string {
recVal := reflect.ValueOf(record)
recType := reflect.TypeOf(record)
numFields := recVal.NumField()
logFormat := "[%s] %s"
logParams := []interface{}{tracer.identity, recType.Name()}
if trace != nil {
logFormat = "[%s] TraceID=%d %s"
logParams = []interface{}{tracer.identity, trace.ID, recType.Name()}
}
{
isFirst := true
for i := 0; i < numFields; i++ {
if !isFirst {
logFormat += ", "
} else {
logFormat += " "
isFirst = false
}
logFormat += recType.Field(i).Name + "=%v"
// strip all pointer types (when not nil), so we log the pointed-to value
valueToLog := recVal.Field(i)
for valueToLog.Kind() == reflect.Ptr && !valueToLog.IsNil() {
valueToLog = reflect.Indirect(valueToLog)
}
logParams = append(logParams, valueToLog.Interface())
}
}
return fmt.Sprintf(logFormat, logParams...)
}
func (tracer *Tracer) recordAction(trace *Trace, record interface{}, isLocalEvent bool) {
if isLocalEvent {
tracer.logger.LogLocalEvent(tracer.getLogString(trace, record), govec.GetDefaultLogOptions())
}
if tracer.shouldPrint {
log.Print(tracer.getLogString(trace, record))
}
// send data to tracer server
marshaledRecord, err := json.Marshal(record)
if err != nil {
log.Print("error marshaling record: ", err)
}
err = tracer.client.Call("RPCProvider.RecordAction", RecordActionArg{
TracerIdentity: tracer.identity,
TraceID: trace.ID,
RecordName: reflect.TypeOf(record).Name(),
Record: marshaledRecord,
VectorClock: tracer.logger.GetCurrentVC(),
}, nil)
if err != nil {
log.Print("error recording action to remote: ", err)
}
}
// ReceiveTokenTrace is an action that indicated receiption of a token.
type ReceiveTokenTrace struct {
Token TracingToken // the token that was received.
}
// ReceiveToken records the token by calling RecordAction with
// ReceiveTokenTrace.
func (tracer *Tracer) ReceiveToken(token TracingToken) *Trace {
tracer.lock.Lock()
defer tracer.lock.Unlock()
record := ReceiveTokenTrace{Token: token}
var traceID uint64
tracer.logger.UnpackReceive(tracer.getLogString(nil, record),
token, &traceID, govec.GetDefaultLogOptions())
trace := &Trace{
ID: traceID,
Tracer: tracer,
}
tracer.recordAction(trace, record, false)
return trace
}
// Close cleans up the connection to the tracing server.
// To allow for tracing long-running processes and Ctrl^C, this call is
// unnecessary, as there is no connection state. After this call, the use of
// any previously generated local Trace instances leads to undefined behavior.
func (tracer *Tracer) Close() error {
tracer.lock.Lock()
defer tracer.lock.Unlock()
return tracer.client.Close()
}
// SetShouldPrint determines whether RecordAction should log the action being
// recorded as it sends the action to the tracing server. In other words, it
// indicates that the Tracer instance should log (print to stdout) the recorded
// actions or not.
// For more complex applications which have long, involved traces, it may be
// helpful to silence trace logging.
func (tracer *Tracer) SetShouldPrint(shouldPrint bool) {
tracer.lock.Lock()
defer tracer.lock.Unlock()
tracer.shouldPrint = shouldPrint
}