-
Notifications
You must be signed in to change notification settings - Fork 52
/
sample.rb
120 lines (107 loc) · 2.81 KB
/
sample.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true
# Run with 'bundle exec rackup'
require "redis"
require "logster"
require "logster/middleware/reporter"
require "logster/middleware/viewer"
require "json"
require "sinatra"
require "sinatra/base"
# log a few errors
sample_redis = Redis.new
sample_store = Logster.store = Logster::RedisStore.new(sample_redis)
Logster.logger = Logster::Logger.new(sample_store)
class SampleLoader
def initialize
@index = 0
@sample_data_key = "sample_data"
end
def ensure_samples_loaded
sample_redis.del @sample_data_key
data = File.read("data/data.json")
parsed = JSON.parse(data)
parsed.each { |row| sample_redis.rpush @sample_data_key, JSON.fast_generate(row) }
@length = parsed.length
end
def load_samples
Thread.new do
while true
sleep 5
begin
load_next_sample
rescue => e
sample_store.report(4, "logster", e.to_s)
end
end
end
end
def load_next_sample
message = JSON.parse(sample_redis.lindex(@sample_data_key, @index))
@index += 1
@index %= @length
sample_store.report(
message["severity"],
message["progname"],
message["message"],
backtrace: message["backtrace"],
env: message["env"],
count: message["count"],
)
end
def load_error
# 2 = Severity.WARN
params = {}
params["always_present"] = "some_value_#{rand(3)}"
params["key_#{rand(3)}"] = "some_value_#{rand(3)}"
sample_store.report(
2,
"",
"Message message message",
backtrace: "Backtrace backtrace backtrace",
env: {
something: :foo,
random: rand(3),
array: [1, 2, 3],
rand_array: [10, 11, rand(300)],
params: params,
},
)
end
end
sample_loader_instance = SampleLoader.new
sample_loader_instance.ensure_samples_loaded
sample_loader_instance.load_samples unless ENV["NO_DATA"]
Logster.config.allow_grouping = true
Logster.config.enable_custom_patterns_via_ui = ENV["LOGSTER_ENABLE_CUSTOM_PATTERNS_VIA_UI"] == "1"
Logster.config.application_version = "b329e23f8511b7248c0e4aee370a9f8a249e1b84"
Logster.config.gems_dir = "/home/sam/.rbenv/versions/2.1.2.discourse/lib/ruby/gems/2.1.0/gems/"
Logster.config.project_directories = [
{
path: "/home/sam/Source/discourse",
url: "https://github.com/discourse/discourse",
main_app: true,
},
]
class Sample < Sinatra::Base
use Logster::Middleware::Viewer
use Logster::Middleware::Reporter
get "/" do
<<HTML
<html>
<head>
</head>
<body>
<h3>Welcome to logster:</h3>
<ul>
<li><a href='https://github.com/SamSaffron/logster'>Learn about logster</a></li>
<li><a href='/logs/'>View sample dev logs</a></li>
</ul>
</body>
</html>
HTML
end
get "/report_error" do
sample_loader_instance.load_next_sample
sample_loader_instance.load_error
end
end