Skip to content

Commit

Permalink
fix: Syntax error in custom interval SQL clause since 7.2 upgrade
Browse files Browse the repository at this point in the history
Intervals in the latency filter scopes must not be coded as `interval ?`
anymore, but only `?`, despite being passed as strings only. Otherwise,
the following error is raised:

    ActiveRecord::StatementInvalid:
      PG::SyntaxError: ERROR:  syntax error at or near "$1"
      LINE 1: ...traces".* FROM "traces" WHERE ((stop - start) < interval $1)
                                                                          ^

This commit further extracts the two latency param filters into model
scopes, and adds specs for both. Since we have no request/system specs
here yet, testing the scopes on the models is easier, and will continue
to ensure that these scopes do work.
  • Loading branch information
jgraichen committed Oct 21, 2024
1 parent 0572903 commit cead481
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/controllers/traces_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ class TracesController < ApplicationController
end

has_scope :ls do |_, scope, value|
scope.where("(stop - start) >= interval ?", ::ActiveSupport::Duration.parse_string(value).iso8601)
scope.latency_above(value)
end

has_scope :le do |_, scope, value|
scope.where("(stop - start) < interval ?", ::ActiveSupport::Duration.parse_string(value).iso8601)
scope.latency_below(value)
end

has_scope :meta, type: :hash do |_, scope, value|
Expand Down
10 changes: 10 additions & 0 deletions app/models/trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ def retention(period, time = Time.zone.now)
where t[:stop].lt(tlimit).and(t[:store].eq(false))
end

def latency_above(value)
duration = ::ActiveSupport::Duration.parse_string(value)
where("(stop - start) >= ?", duration.iso8601)
end

def latency_below(value)
duration = ::ActiveSupport::Duration.parse_string(value)
where("(stop - start) < ?", duration.iso8601)
end

alias t arel_table
end
end
42 changes: 42 additions & 0 deletions spec/models/trace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,47 @@
end
end
end

describe ".latency_above" do
subject(:traces) { described_class.latency_above(value) }

let(:value) { "5s" }

let!(:expected) do
create(:trace, :w_spans, start: Time.zone.now, stop: Time.zone.now + 10)
end

before do
create(:trace, :w_spans, start: Time.zone.now, stop: Time.zone.now + 4)
create(:trace, :w_spans, start: Time.zone.now, stop: Time.zone.now + 2)
end

it "returns only traces above 5 seconds in length" do
expect(described_class.count).to eq 3

expect(traces).to contain_exactly(expected)
end
end

describe ".latency_below" do
subject(:traces) { described_class.latency_below(value) }

let(:value) { "5s" }

let!(:expected) do
create(:trace, :w_spans, start: Time.zone.now, stop: Time.zone.now + 4)
end

before do
create(:trace, :w_spans, start: Time.zone.now, stop: Time.zone.now + 6)
create(:trace, :w_spans, start: Time.zone.now, stop: Time.zone.now + 10)
end

it "returns only traces above 5 seconds in length" do
expect(described_class.count).to eq 3

expect(traces).to contain_exactly(expected)
end
end
end
end

0 comments on commit cead481

Please sign in to comment.