Class: Sciosano::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sciosano/client.rb

Overview

Main client for sending reports to sciosano API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sciosano/client.rb', line 8

def initialize(configuration)
  @configuration = configuration
  @configuration.validate!
  @context = Context.new
  @breadcrumbs = Concurrent::Array.new
  @executor = Concurrent::ThreadPoolExecutor.new(
    min_threads: 1,
    max_threads: 5,
    max_queue: 100,
    fallback_policy: :caller_runs # Don't silently discard - run in caller thread if queue full
  )

  log("sciosano initialized with endpoint: #{configuration.endpoint}")
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



6
7
8
# File 'lib/sciosano/client.rb', line 6

def configuration
  @configuration
end

Instance Method Details

#add_breadcrumb(breadcrumb) ⇒ Object

Add a breadcrumb

Parameters:



62
63
64
65
66
67
68
69
# File 'lib/sciosano/client.rb', line 62

def add_breadcrumb(breadcrumb)
  @breadcrumbs << breadcrumb

  # Keep only max_breadcrumbs
  while @breadcrumbs.size > configuration.max_breadcrumbs
    @breadcrumbs.shift
  end
end

Get current breadcrumbs



101
102
103
# File 'lib/sciosano/client.rb', line 101

def breadcrumbs
  @breadcrumbs.to_a
end

#capture_exception(exception, context = {}) ⇒ String?

Capture an exception

Parameters:

  • (defaults to: {})

Returns:

  • Report ID



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sciosano/client.rb', line 28

def capture_exception(exception, context = {})
  return nil unless should_capture?(exception)

  report = Report.build_from_exception(
    exception,
    context: @context.merge(context),
    breadcrumbs: @breadcrumbs.to_a,
    configuration: configuration
  )

  send_report(report)
end

#capture_message(message, level: :info, context: {}) ⇒ String?

Capture a message

Parameters:

  • (defaults to: :info)
  • (defaults to: {})

Returns:

  • Report ID



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sciosano/client.rb', line 47

def capture_message(message, level: :info, context: {})
  report = Report.build_from_message(
    message,
    level: level,
    context: @context.merge(context),
    breadcrumbs: @breadcrumbs.to_a,
    configuration: configuration
  )

  send_report(report)
end

#clear_breadcrumbsObject

Clear breadcrumbs



106
107
108
# File 'lib/sciosano/client.rb', line 106

def clear_breadcrumbs
  @breadcrumbs.clear
end

#clear_userObject

Clear user context



77
78
79
# File 'lib/sciosano/client.rb', line 77

def clear_user
  @context.user = nil
end

#set_extra(key, value) ⇒ Object

Set extra context



82
83
84
# File 'lib/sciosano/client.rb', line 82

def set_extra(key, value)
  @context.extra[key.to_s] = value
end

#set_tags(tags) ⇒ Object

Set tags



87
88
89
# File 'lib/sciosano/client.rb', line 87

def set_tags(tags)
  @context.tags.merge!(tags.transform_keys(&:to_s))
end

#set_user(id: nil, email: nil, name: nil, **extra) ⇒ Object

Set user context



72
73
74
# File 'lib/sciosano/client.rb', line 72

def set_user(id: nil, email: nil, name: nil, **extra)
  @context.user = { id: id, email: email, name: name, **extra }.compact
end

#with_context(context = {}) ⇒ Object

Execute block with additional context



92
93
94
95
96
97
98
# File 'lib/sciosano/client.rb', line 92

def with_context(context = {})
  old_context = @context.dup
  @context.merge!(context)
  yield
ensure
  @context = old_context
end