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

Commit

Permalink
trace the scope as a JSON object (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jun 20, 2022
1 parent 3f28daf commit a18fe79
Showing 1 changed file with 138 additions and 19 deletions.
157 changes: 138 additions & 19 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -46,11 +48,125 @@ const (
traceRemoveConnEvt = "remove_conn"
)

type scopeClass struct {
name string
}

func (s scopeClass) MarshalJSON() ([]byte, error) {
name := s.name
var span string
if idx := strings.Index(name, "span:"); idx > -1 {
name = name[:idx-1]
span = name[idx+5:]
}
// System and Transient scope
if name == "system" || name == "transient" {
return json.Marshal(struct {
Class string
Span string `json:",omitempty"`
}{
Class: name,
Span: span,
})
}
// Connection scope
if strings.HasPrefix(name, "conn-") {
return json.Marshal(struct {
Class string
Conn string
Span string `json:",omitempty"`
}{
Class: "conn",
Conn: name[5:],
Span: span,
})
}
// Stream scope
if strings.HasPrefix(name, "stream-") {
return json.Marshal(struct {
Class string
Stream string
Span string `json:",omitempty"`
}{
Class: "stream",
Stream: name[7:],
Span: span,
})
}
// Peer scope
if strings.HasPrefix(name, "peer:") {
return json.Marshal(struct {
Class string
Peer string
Span string `json:",omitempty"`
}{
Class: "peer",
Peer: name[5:],
Span: span,
})
}

if strings.HasPrefix(name, "service:") {
if idx := strings.Index(name, "peer:"); idx > 0 { // Peer-Service scope
return json.Marshal(struct {
Class string
Service string
Peer string
Span string `json:",omitempty"`
}{
Class: "service-peer",
Service: name[8 : idx-1],
Peer: name[idx+5:],
Span: span,
})
} else { // Service scope
return json.Marshal(struct {
Class string
Service string
Span string `json:",omitempty"`
}{
Class: "service",
Service: name[8:],
Span: span,
})
}
}

if strings.HasPrefix(name, "protocol:") {
if idx := strings.Index(name, "peer:"); idx > -1 { // Peer-Protocol scope
return json.Marshal(struct {
Class string
Protocol string
Peer string
Span string `json:",omitempty"`
}{
Class: "protocol-peer",
Protocol: name[9 : idx-1],
Peer: name[idx+5:],
Span: span,
})
} else { // Protocol scope
return json.Marshal(struct {
Class string
Protocol string
Span string `json:",omitempty"`
}{
Class: "protocol",
Protocol: name[9:],
Span: span,
})
}
}

return nil, fmt.Errorf("unrecognized scope: %s", name)
}

type TraceEvt struct {
Time string
Type string

Scope string `json:",omitempty"`
Scope *scopeClass `json:",omitempty"`
Name string `json:",omitempty"`

Limit interface{} `json:",omitempty"`

Expand Down Expand Up @@ -79,6 +195,9 @@ func (t *trace) push(evt TraceEvt) {
return
}
evt.Time = time.Now().Format(time.RFC3339Nano)
if evt.Name != "" {
evt.Scope = &scopeClass{name: evt.Name}
}

t.pend = append(t.pend, evt)
}
Expand Down Expand Up @@ -211,7 +330,7 @@ func (t *trace) CreateScope(scope string, limit Limit) {

t.push(TraceEvt{
Type: traceCreateScopeEvt,
Scope: scope,
Name: scope,
Limit: limit,
})
}
Expand All @@ -222,8 +341,8 @@ func (t *trace) DestroyScope(scope string) {
}

t.push(TraceEvt{
Type: traceDestroyScopeEvt,
Scope: scope,
Type: traceDestroyScopeEvt,
Name: scope,
})
}

Expand All @@ -238,7 +357,7 @@ func (t *trace) ReserveMemory(scope string, prio uint8, size, mem int64) {

t.push(TraceEvt{
Type: traceReserveMemoryEvt,
Scope: scope,
Name: scope,
Priority: prio,
Delta: size,
Memory: mem,
Expand All @@ -256,7 +375,7 @@ func (t *trace) BlockReserveMemory(scope string, prio uint8, size, mem int64) {

t.push(TraceEvt{
Type: traceBlockReserveMemoryEvt,
Scope: scope,
Name: scope,
Priority: prio,
Delta: size,
Memory: mem,
Expand All @@ -274,7 +393,7 @@ func (t *trace) ReleaseMemory(scope string, size, mem int64) {

t.push(TraceEvt{
Type: traceReleaseMemoryEvt,
Scope: scope,
Name: scope,
Delta: size,
Memory: mem,
})
Expand All @@ -294,7 +413,7 @@ func (t *trace) AddStream(scope string, dir network.Direction, nstreamsIn, nstre

t.push(TraceEvt{
Type: traceAddStreamEvt,
Scope: scope,
Name: scope,
DeltaIn: deltaIn,
DeltaOut: deltaOut,
StreamsIn: nstreamsIn,
Expand All @@ -316,7 +435,7 @@ func (t *trace) BlockAddStream(scope string, dir network.Direction, nstreamsIn,

t.push(TraceEvt{
Type: traceBlockAddStreamEvt,
Scope: scope,
Name: scope,
DeltaIn: deltaIn,
DeltaOut: deltaOut,
StreamsIn: nstreamsIn,
Expand All @@ -338,7 +457,7 @@ func (t *trace) RemoveStream(scope string, dir network.Direction, nstreamsIn, ns

t.push(TraceEvt{
Type: traceRemoveStreamEvt,
Scope: scope,
Name: scope,
DeltaIn: deltaIn,
DeltaOut: deltaOut,
StreamsIn: nstreamsIn,
Expand All @@ -357,7 +476,7 @@ func (t *trace) AddStreams(scope string, deltaIn, deltaOut, nstreamsIn, nstreams

t.push(TraceEvt{
Type: traceAddStreamEvt,
Scope: scope,
Name: scope,
DeltaIn: deltaIn,
DeltaOut: deltaOut,
StreamsIn: nstreamsIn,
Expand All @@ -376,7 +495,7 @@ func (t *trace) BlockAddStreams(scope string, deltaIn, deltaOut, nstreamsIn, nst

t.push(TraceEvt{
Type: traceBlockAddStreamEvt,
Scope: scope,
Name: scope,
DeltaIn: deltaIn,
DeltaOut: deltaOut,
StreamsIn: nstreamsIn,
Expand All @@ -395,7 +514,7 @@ func (t *trace) RemoveStreams(scope string, deltaIn, deltaOut, nstreamsIn, nstre

t.push(TraceEvt{
Type: traceRemoveStreamEvt,
Scope: scope,
Name: scope,
DeltaIn: -deltaIn,
DeltaOut: -deltaOut,
StreamsIn: nstreamsIn,
Expand All @@ -420,7 +539,7 @@ func (t *trace) AddConn(scope string, dir network.Direction, usefd bool, nconnsI

t.push(TraceEvt{
Type: traceAddConnEvt,
Scope: scope,
Name: scope,
DeltaIn: deltaIn,
DeltaOut: deltaOut,
Delta: int64(deltafd),
Expand All @@ -447,7 +566,7 @@ func (t *trace) BlockAddConn(scope string, dir network.Direction, usefd bool, nc

t.push(TraceEvt{
Type: traceBlockAddConnEvt,
Scope: scope,
Name: scope,
DeltaIn: deltaIn,
DeltaOut: deltaOut,
Delta: int64(deltafd),
Expand All @@ -474,7 +593,7 @@ func (t *trace) RemoveConn(scope string, dir network.Direction, usefd bool, ncon

t.push(TraceEvt{
Type: traceRemoveConnEvt,
Scope: scope,
Name: scope,
DeltaIn: deltaIn,
DeltaOut: deltaOut,
Delta: int64(deltafd),
Expand All @@ -495,7 +614,7 @@ func (t *trace) AddConns(scope string, deltaIn, deltaOut, deltafd, nconnsIn, nco

t.push(TraceEvt{
Type: traceAddConnEvt,
Scope: scope,
Name: scope,
DeltaIn: deltaIn,
DeltaOut: deltaOut,
Delta: int64(deltafd),
Expand All @@ -516,7 +635,7 @@ func (t *trace) BlockAddConns(scope string, deltaIn, deltaOut, deltafd, nconnsIn

t.push(TraceEvt{
Type: traceBlockAddConnEvt,
Scope: scope,
Name: scope,
DeltaIn: deltaIn,
DeltaOut: deltaOut,
Delta: int64(deltafd),
Expand All @@ -537,7 +656,7 @@ func (t *trace) RemoveConns(scope string, deltaIn, deltaOut, deltafd, nconnsIn,

t.push(TraceEvt{
Type: traceRemoveConnEvt,
Scope: scope,
Name: scope,
DeltaIn: -deltaIn,
DeltaOut: -deltaOut,
Delta: -int64(deltafd),
Expand Down

0 comments on commit a18fe79

Please sign in to comment.