Skip to content

Commit

Permalink
metrics on relayed bandwidth
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcloughlin committed Oct 29, 2017
1 parent 01b3d45 commit 960708e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
10 changes: 8 additions & 2 deletions circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (c *CircuitCryptoState) Encrypt(b []byte) {
type TransverseCircuit struct {
Router *Router
Conn *Connection
Metrics *Metrics
Forward *CircuitCryptoState
Backward *CircuitCryptoState

Expand All @@ -116,6 +117,7 @@ func NewTransverseCircuit(conn *Connection, id CircID, fwd, back *CircuitCryptoS
circ := &TransverseCircuit{
Router: r,
Conn: conn,
Metrics: r.metrics,
Forward: fwd,
Backward: back,

Expand All @@ -129,7 +131,7 @@ func NewTransverseCircuit(conn *Connection, id CircID, fwd, back *CircuitCryptoS
logger: log.ForComponent(l, "transverse_circuit").With("circid", id),
}

r.metrics.Circuits.Alloc()
circ.Metrics.Circuits.Alloc()

circ.wg.Add(1)
go circ.loop()
Expand Down Expand Up @@ -216,7 +218,7 @@ func (t *TransverseCircuit) cleanup() error {
}

t.logger.Info("cleanup circuit")
t.Router.metrics.Circuits.Free()
t.Metrics.Circuits.Free()

return result
}
Expand Down Expand Up @@ -285,6 +287,8 @@ func (t *TransverseCircuit) handleUnrecognizedCell(c Cell) error {
return t.destroy(CircuitErrorConnectfailed)
}

t.Metrics.RelayForward.Inc(int64(len(c.Payload())))

return nil
}

Expand Down Expand Up @@ -431,6 +435,8 @@ func (t *TransverseCircuit) handleBackwardRelay(c Cell) error {
return t.destroy(CircuitErrorConnectfailed)
}

t.Metrics.RelayBackward.Inc(int64(len(c.Payload())))

return nil
}

Expand Down
20 changes: 12 additions & 8 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ import (
)

type Metrics struct {
Connections telemetry.ResourceMetric
Circuits telemetry.ResourceMetric
Inbound *telemetry.Bandwidth
Outbound *telemetry.Bandwidth
Connections telemetry.ResourceMetric
Circuits telemetry.ResourceMetric
Inbound *telemetry.Bandwidth
Outbound *telemetry.Bandwidth
RelayForward *telemetry.Bandwidth
RelayBackward *telemetry.Bandwidth
}

func NewMetrics(scope tally.Scope, l log.Logger) *Metrics {
return &Metrics{
Connections: telemetry.NewResourceMetric(scope, l, "connections"),
Circuits: telemetry.NewResourceMetric(scope, l, "circuits"),
Inbound: telemetry.NewBandwidth(scope.Counter("inbound_bytes")),
Outbound: telemetry.NewBandwidth(scope.Counter("outbound_bytes")),
Connections: telemetry.NewResourceMetric(scope, l, "connections"),
Circuits: telemetry.NewResourceMetric(scope, l, "circuits"),
Inbound: telemetry.NewBandwidth(scope.Counter("inbound_bytes")),
Outbound: telemetry.NewBandwidth(scope.Counter("outbound_bytes")),
RelayForward: telemetry.NewBandwidth(scope.Counter("relay_forward_bytes")),
RelayBackward: telemetry.NewBandwidth(scope.Counter("relay_backward_bytes")),
}
}
6 changes: 3 additions & 3 deletions telemetry/bandwidth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
)

type Bandwidth struct {
c tally.Counter
tally.Counter
}

func NewBandwidth(c tally.Counter) *Bandwidth {
return &Bandwidth{
c: c,
Counter: c,
}
}

func (b *Bandwidth) Write(d []byte) (int, error) {
n := len(d)
b.c.Inc(int64(n))
b.Inc(int64(n))
return n, nil
}

Expand Down

0 comments on commit 960708e

Please sign in to comment.