Class: Hypertube::Sdk::Tools::MessageHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/hypertube-ruby-sdk/utils/message_helper.rb

Constant Summary collapse

ADDRESS =
'https://dc.services.visualstudio.com/v2/track'
DEFAULT_INSTRUMENTATION_KEY =
'2c751560-90c8-40e9-b5dd-534566514723'
REQUEST_TIMEOUT_SECONDS =
3
CALLING_RUNTIME_NAME =
'Ruby'
VERSION =
Gem.loaded_specs['hypertube-ruby-sdk']&.version&.to_s || 'Unknown'
NODE_NAME =
begin
  Socket.gethostname
rescue StandardError
  'Unknown Host'
end
OS_NAME =
case RUBY_PLATFORM
when /darwin/ then 'MacOS'
when /linux/ then 'Linux'
when /freebsd/ then 'FreeBSD'
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/ then 'Windows'
else 'Unknown'
end

Class Method Summary collapse

Class Method Details

.send_message_to_app_insights(operation_name, message) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/hypertube-ruby-sdk/utils/message_helper.rb', line 29

def self.send_message_to_app_insights(operation_name, message)
  thread = Thread.new do
    send_message_to_app_insights_func(operation_name, message)
  end
  thread.abort_on_exception = false
  thread.report_on_exception = false if thread.respond_to?(:report_on_exception=)
  thread
rescue StandardError
  nil
end

.send_message_to_app_insights_func(operation_name, message) ⇒ Object



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hypertube-ruby-sdk/utils/message_helper.rb', line 40

def self.send_message_to_app_insights_func(operation_name, message)
  license_key = Hypertube::Sdk::Tools::ActivationHelper.get_license_key
  if message.include?('BinariesUnloader exception') ||
     message.include?('BinariesExtractor exception')
    return 0 if license_key == 'functional-tests-key'
  end

  instrumentation_key = ENV['HYPERTUBE_INSTRUMENTATION_KEY']
  instrumentation_key = DEFAULT_INSTRUMENTATION_KEY if instrumentation_key.nil? || instrumentation_key.empty?

  payload = {
    'name' => 'AppEvents',
    'time' => Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ'),
    'iKey' => instrumentation_key,
    'tags' => {
      'ai.application.ver' => VERSION,
      'ai.cloud.roleInstance' => NODE_NAME,
      'ai.operation.id' => '0',
      'ai.operation.parentId' => '0',
      'ai.operation.name' => operation_name,
      'ai.internal.sdkVersion' => 'hypertube',
      'ai.internal.nodeName' => NODE_NAME
    },
    'data' => {
      'baseType' => 'EventData',
      'baseData' => {
        'ver' => 2,
        'name' => message,
        'properties' => {
          'OperatingSystem' => OS_NAME,
          'LicenseKey' => license_key,
          'CallingTechnology' => CALLING_RUNTIME_NAME
        }
      },
    }
  }

  uri = URI(ADDRESS)
  request = Net::HTTP::Post.new(uri)
  request['Content-Type'] = 'application/json'
  request.body = JSON.generate(payload)

  response = Net::HTTP.start(
    uri.hostname,
    uri.port,
    use_ssl: true,
    open_timeout: REQUEST_TIMEOUT_SECONDS,
    read_timeout: REQUEST_TIMEOUT_SECONDS
  ) { |http| http.request(request) }

  response.code.to_i
rescue StandardError
  -1
end