Skip to content

Commit

Permalink
Add tests for memory and redis storage
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Feb 19, 2025
1 parent a72de98 commit 96470ee
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 7 deletions.
16 changes: 11 additions & 5 deletions lib/graphql/tracing/perfetto_sampler/memory_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class PerfettoSampler
# It won't work for multi-process deployments and everything is erased when the app is restarted.
class MemoryBackend
def initialize
@traces = []
@traces = {}
end

def traces
@traces.compact
@traces.values
end

def find_trace(id)
Expand All @@ -23,15 +23,21 @@ def delete_trace(id)
nil
end

def delete_all_traces
@traces.clear
nil
end

def save_trace(operation_name, duration, timestamp, trace_data)
@traces << PerfettoSampler::StoredTrace.new(
id: @traces.size,
id = @traces.size
@traces[id] = PerfettoSampler::StoredTrace.new(
id: id,
operation_name: operation_name,
duration_ms: duration,
timestamp: timestamp,
trace_data: trace_data
)
nil
id
end
end
end
Expand Down
10 changes: 8 additions & 2 deletions lib/graphql/tracing/perfetto_sampler/redis_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def initialize(redis:)
end

def traces
keys = @redis.scan_each(match: "#{KEY_PREFIX}*")
keys = @redis.scan_each(match: "#{KEY_PREFIX}*").to_a
keys.sort!
keys.map do |k|
h = @redis.hgetall(k)
StoredTrace.new(
Expand All @@ -28,6 +29,11 @@ def delete_trace(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
Expand All @@ -51,7 +57,7 @@ def save_trace(operation_name, duration_ms, timestamp, trace_data)
"timestamp", timestamp.to_i,
"trace_data", trace_data,
)
nil
id
end
end
end
Expand Down
47 changes: 47 additions & 0 deletions spec/graphql/tracing/perfetto_sampler/backend_assertions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

module GraphQLTracingPerfettoSamplerBackendAssertions
def self.included(child_class)
child_class.instance_eval do
describe "BackendAssertions" do
before do
@backend.delete_all_traces
end

it "can save, retreive, list, and delete traces" do
data = SecureRandom.bytes(1000)
trace_id = @backend.save_trace(
"GetStuff",
100.56,
Time.utc(2024, 01, 01, 04, 44, 33, 695),
data
)

trace = @backend.find_trace(trace_id)
assert_kind_of GraphQL::Tracing::PerfettoSampler::StoredTrace, trace
assert_equal "GetStuff", trace.operation_name
assert_equal 100.56, trace.duration_ms
assert_equal "2024-01-01 04:44:33.000", trace.timestamp.utc.strftime("%Y-%m-%d %H:%M:%S.%L")
assert_equal data, trace.trace_data


@backend.save_trace(
"GetOtherStuff",
200.16,
Time.utc(2024, 01, 03, 04, 44, 33, 695),
data
)

assert_equal ["GetStuff", "GetOtherStuff"], @backend.traces.map(&:operation_name)

@backend.delete_trace(trace_id)

assert_equal ["GetOtherStuff"], @backend.traces.map(&:operation_name)

@backend.delete_all_traces
assert_equal [], @backend.traces
end
end
end
end
end
11 changes: 11 additions & 0 deletions spec/graphql/tracing/perfetto_sampler/memory_backend_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true
require "spec_helper"
require_relative "./backend_assertions"

describe GraphQL::Tracing::PerfettoSampler::MemoryBackend do
include GraphQLTracingPerfettoSamplerBackendAssertions

before do
@backend = GraphQL::Tracing::PerfettoSampler::MemoryBackend.new
end
end
13 changes: 13 additions & 0 deletions spec/graphql/tracing/perfetto_sampler/redis_backend_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true
require "spec_helper"
require_relative "./backend_assertions"

if defined?(::Redis)
describe GraphQL::Tracing::PerfettoSampler::RedisBackend do
include GraphQLTracingPerfettoSamplerBackendAssertions

before do
@backend = GraphQL::Tracing::PerfettoSampler::RedisBackend.new(redis: Redis.new)
end
end
end

0 comments on commit 96470ee

Please # to comment.