# encoding: utf-8 require 'one_apm/agent/inbound_request_monitor' module OneApm module Agent class SyntheticsMonitor < InboundRequestMonitor OA_SYNTHETICS_HEADER_KEY = 'HTTP_X_BLUEWARE_SYNTHETICS'.freeze OA_SUPPORTED_VERSION = 1 OA_EXPECTED_PAYLOAD_LENGTH = 5 def on_finished_configuring(events) events.subscribe(:before_call, &method(:on_before_call)) end def on_before_call(request) encoded_header = request[OA_SYNTHETICS_HEADER_KEY] return unless encoded_header incoming_payload = deserialize_header(encoded_header, OA_SYNTHETICS_HEADER_KEY) return unless incoming_payload && is_valid_payload?(incoming_payload) && is_supported_version?(incoming_payload) && is_trusted?(incoming_payload) state = OneApm::TransactionState.tl_get txn = state.current_transaction txn.raw_synthetics_header = encoded_header txn.synthetics_payload = incoming_payload end def is_supported_version?(incoming_payload) incoming_payload.first == OA_SUPPORTED_VERSION end def is_trusted?(incoming_payload) account_id = incoming_payload[1] OneApm::Manager.config[:trusted_account_ids].include?(account_id) end def is_valid_payload?(incoming_payload) incoming_payload.length == OA_EXPECTED_PAYLOAD_LENGTH end end end end