Class: VarnishLog

Inherits:
Object
  • Object
show all
Defined in:
lib/varnishlog/request.rb,
lib/varnishlog/response.rb,
lib/varnishlog/varnishlog.rb

Overview

VarnishLog - A class to define objects that contain a transaction captured

by `varnishlog`.

Defined Under Namespace

Classes: Request, Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVarnishLog

Create a new VarnishLog object.

Returns a VarnishLog object with two attributes that are request and response objects.



17
18
19
20
# File 'lib/varnishlog/varnishlog.rb', line 17

def initialize
  @request = VarnishLog::Request.new
  @response = VarnishLog::Response.new
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



12
13
14
# File 'lib/varnishlog/varnishlog.rb', line 12

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



12
13
14
# File 'lib/varnishlog/varnishlog.rb', line 12

def response
  @response
end

Instance Method Details

#parse(transaction) ⇒ Object

Parses the output of a transaction captured by ‘varnishlog` into attributes that are assigned to a VarnishLog object.



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
# File 'lib/varnishlog/varnishlog.rb', line 36

def parse(transaction)
  items = transaction.split("\n")

  # Requests
  ## Request headers.
  request_headers = items.grep(/ReqHeader/)
  request_headers.each do |header|
    if match = /-\s+ReqHeader\s+(?<header_name>.*): (?<header_value>.*)/.match(header)
      @request.add_header(match['header_name'], match['header_value'])
    end
  end

  ## Match ReqMethod.
  if method_match = /-\s+ReqMethod\s+(?<method>.*)/.match(items.grep(/ReqMethod/)[0])
    @request.method = method_match['method']
  end
  ## Match ReqURL.
  if url_match = /-\s+ReqURL\s+(?<url>\/.*)/.match(items.grep(/ReqURL/)[0])
    @request.url = url_match['url']
  end
  ## Match ReqProtocol.
  if protocol_match = /-\s+ReqProtocol\s+(?<protocol>.*)/.match(items.grep(/ReqProtocol/)[0])
    @request.protocol = protocol_match['protocol']
  end

  # Response.
  ## Response headers.
  response_headers = items.grep(/RespHeader/)
  response_headers.each do |header|
    if match = /-\s+RespHeader\s+(?<header_name>.*): (?<header_value>.*)/.match(header)
      @response.add_header(match['header_name'], match['header_value'])
    end
  end

end

#reap_threadObject



29
30
31
32
# File 'lib/varnishlog/varnishlog.rb', line 29

def reap_thread
  transaction = @varnishlog_thread.value
  parse(transaction)
end

#start_varnishlog_thread(user_agent = "shellac") ⇒ Object



22
23
24
25
26
27
# File 'lib/varnishlog/varnishlog.rb', line 22

def start_varnishlog_thread(user_agent="shellac")
  @varnishlog_thread = Thread.new {
    output = `varnishlog -q 'ReqHeader:User-Agent eq "#{user_agent}"' -k 1`
  }
  @varnishlog_thread
end