Class: Pf2::Reporter::Protobuf

Inherits:
Object
  • Object
show all
Defined in:
lib/pf2/reporter/pprof.rb

Overview

A minimal Protobuf encoder.

Constant Summary collapse

WIRETYPE_VARINT =

Wire types Not implemented: I64, SGROUP, EGROUP, I32

0
WIRETYPE_LEN =
2

Instance Method Summary collapse

Constructor Details

#initializeProtobuf

Returns a new instance of Protobuf.



10
11
12
# File 'lib/pf2/reporter/pprof.rb', line 10

def initialize
  @data = +"".b
end

Instance Method Details

#bool(field, val) ⇒ Object



70
71
72
# File 'lib/pf2/reporter/pprof.rb', line 70

def bool(field, val)
  wire_varint(field, val ? 1 : 0)
end

#int32(field, val) ⇒ Object

Scalar value types Not implemented:

double, float, uint32, sint32, sint64,
fixed32, fixed64, sfixed32, sfixed64, bytes


56
57
58
59
# File 'lib/pf2/reporter/pprof.rb', line 56

def int32(field, val)
  twos_comp = val & 0xFFFF_FFFF
  wire_varint(field, twos_comp)
end

#int64(field, val) ⇒ Object



61
62
63
64
# File 'lib/pf2/reporter/pprof.rb', line 61

def int64(field, val)
  twos_comp = val & 0xFFFF_FFFF_FFFF_FFFF
  wire_varint(field, twos_comp)
end

#put_byte(b) ⇒ Object



24
25
26
# File 'lib/pf2/reporter/pprof.rb', line 24

def put_byte(b)
  @data << (b & 0xFF).chr(Encoding::BINARY)
end

#put_varint(val) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/pf2/reporter/pprof.rb', line 28

def put_varint(val)
  # Emit 7-bit chunks
  # msb=1 for continuation
  while val > 0x7F
    put_byte((val & 0x7F) | 0x80)
    val >>= 7
  end
  put_byte(val & 0xFF)
end

#string(field, val) ⇒ Object



74
75
76
# File 'lib/pf2/reporter/pprof.rb', line 74

def string(field, val)
  wire_len(field, val)
end

#submessage(field, submsg) ⇒ Object




80
81
82
# File 'lib/pf2/reporter/pprof.rb', line 80

def submessage(field, submsg)
  wire_len(field, submsg)
end

#to_bytesObject



14
15
16
# File 'lib/pf2/reporter/pprof.rb', line 14

def to_bytes
  @data
end

#uint64(field, val) ⇒ Object



66
67
68
# File 'lib/pf2/reporter/pprof.rb', line 66

def uint64(field, val)
  wire_varint(field, val)
end

#wire_len(field, bytes) ⇒ Object



43
44
45
46
47
# File 'lib/pf2/reporter/pprof.rb', line 43

def wire_len(field, bytes)
  put_varint((field << 3) | WIRETYPE_LEN) # tag
  put_varint(bytes.bytesize)
  @data << bytes.b
end

#wire_varint(field, val) ⇒ Object



38
39
40
41
# File 'lib/pf2/reporter/pprof.rb', line 38

def wire_varint(field, val)
  put_varint((field << 3) | WIRETYPE_VARINT) # tag
  put_varint(val)
end