Class: SousVide::Outputs::JsonHTTP
- Inherits:
-
Object
- Object
- SousVide::Outputs::JsonHTTP
- Defined in:
- lib/sous_vide/outputs/json_http.rb
Overview
Makes a POST request to a configured endpoint. Logstash & Elasticsearch friendly format.
It uses Net::HTTP to perform requests, it can be customized via :http_client accessor.
Instance Attribute Summary collapse
-
#http_client ⇒ Net::HTTP
Provides access to Net::HTTP client object.
Instance Method Summary collapse
-
#call(run_data:, node_data:, resources_data:) ⇒ void
Sends a POST request with a JSON payload using @http_client object per resource.
-
#initialize(url:, max_retries: 2, logger: nil) ⇒ JsonHTTP
constructor
A new instance of JsonHTTP.
Constructor Details
#initialize(url:, max_retries: 2, logger: nil) ⇒ JsonHTTP
Returns a new instance of JsonHTTP.
20 21 22 23 24 25 26 |
# File 'lib/sous_vide/outputs/json_http.rb', line 20 def initialize(url:, max_retries: 2, logger: nil) @endpoint = URI(url) @logger = logger @retry = 0 @max_retries = max_retries @http_client = Net::HTTP.new(@endpoint.host, @endpoint.port) end |
Instance Attribute Details
#http_client ⇒ Net::HTTP
Provides access to Net::HTTP client object. Use it to enable SSL or pass your own client.
17 18 19 |
# File 'lib/sous_vide/outputs/json_http.rb', line 17 def http_client @http_client end |
Instance Method Details
#call(run_data:, node_data:, resources_data:) ⇒ void
This method returns an undefined value.
Sends a POST request with a JSON payload using @http_client object per resource.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/sous_vide/outputs/json_http.rb', line 30 def call(run_data:, node_data:, resources_data:) log "=============== #{self.class.name} ===============" log "" log "Processing #{resources_data.size} resources." log "Target: #{@endpoint.to_s}" resources_data.each do |tracked| _path = @endpoint.path == "" ? "/" : @endpoint.path post_request = Net::HTTP::Post.new(_path, "Content-Type" => "application/json") payload = tracked.to_h.merge(node_data).merge(run_data) payload["@timestamp"] = Time.parse(payload[:chef_resource_started_at]).iso8601(3) post_request.body = payload.to_json call_with_retries(post_request) end log "All resources processed." log "" end |