-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathredis_backend.rb
More file actions
65 lines (59 loc) · 1.74 KB
/
redis_backend.rb
File metadata and controls
65 lines (59 loc) · 1.74 KB
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
# frozen_string_literal: true
module GraphQL
module Tracing
class PerfettoSampler
class RedisBackend
KEY_PREFIX = "gql:trace:"
def initialize(redis:)
@redis = redis
end
def traces
keys = @redis.scan_each(match: "#{KEY_PREFIX}*").to_a
keys.sort!
keys.map do |k|
h = @redis.hgetall(k)
StoredTrace.new(
id: k.sub(KEY_PREFIX, ""),
operation_name: h["operation_name"],
duration_ms: h["duration_ms"].to_f,
timestamp: Time.at(h["timestamp"].to_i),
trace_data: h["trace_data"],
)
end
end
def delete_trace(id)
@redis.del("#{KEY_PREFIX}#{id}")
nil
end
def delete_all_traces
keys = @redis.scan_each(match: "#{KEY_PREFIX}*")
@redis.del(*keys)
end
def find_trace(id)
redis_h = @redis.hgetall("#{KEY_PREFIX}#{id}")
if redis_h.empty?
nil
else
StoredTrace.new(
id: id,
operation_name: redis_h["operation_name"],
duration_ms: redis_h["duration_ms"].to_f,
timestamp: Time.at(redis_h["timestamp"].to_i),
trace_data: redis_h["trace_data"],
)
end
end
def save_trace(operation_name, duration_ms, timestamp, trace_data)
id = (timestamp.to_i * 1000) + rand(1000)
@redis.hmset("#{KEY_PREFIX}#{id}",
"operation_name", operation_name,
"duration_ms", duration_ms,
"timestamp", timestamp.to_i,
"trace_data", trace_data,
)
id
end
end
end
end
end