Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/elastic_apm/config/server_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class Config
class ServerInfo
attr_reader :payload, :config, :http

VERSION_8_0 = '8.0'
VERSION_0 = '0'

def initialize(config)
@config = config
@http = Transport::Connection::Http.new(config)
Expand All @@ -32,12 +35,14 @@ def execute
resp = http.get(config.server_url)
@payload = JSON.parse(resp.body)
rescue
@payload = {"version" => nil}
@payload = { "version" => VERSION_0 }
end

def version
execute unless payload
payload["version"]
@version ||= begin
execute
payload["version"] ? payload["version"].to_s : VERSION_0
end
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/elastic_apm/instrumenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ def end_transaction(result = nil)

transaction.done result

enqueue.call transaction
if transaction.sampled? || @config.version < Config::ServerInfo::VERSION_8_0
enqueue.call transaction
end

update_transaction_metrics(transaction)

Expand Down
54 changes: 54 additions & 0 deletions spec/elastic_apm/config/server_info_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# frozen_string_literal: true

require "spec_helper"

module ElasticAPM
class Config
RSpec.describe ServerInfo do
subject { described_class.new(Config.new) }

context 'when the request is successful' do
context 'when the version is in the response' do
it 'sets the version' do
WebMock.stub_request(:get, %r{^http://localhost:8200/$}).
to_return(body: '{"version":"7.17.4"}')
expect(subject.version).to eq('7.17.4')
end
end

context 'when the version is not in the response' do
it 'sets the version to 0' do
WebMock.stub_request(:get, %r{^http://localhost:8200/$}).
to_return(body: nil)
expect(subject.version).to eq('0')
end
end
end

context 'when the request is not successful' do
it 'sets the version to 0' do
WebMock.stub_request(:get, %r{^http://localhost:8200/$}).
to_raise(StandardError)
expect(subject.version).to eq('0')
end
end
end
end
end
8 changes: 4 additions & 4 deletions spec/elastic_apm/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,16 @@ module ElasticAPM
end

describe "#version" do
it "has no version if the server does not respond" do
it "has version 0 if the server does not respond" do
WebMock.stub_request(:get, "http://localhost:8200/")
.to_return(status: 404, body: "")
expect(Config.new.version).to be_nil
expect(Config.new.version).to eq '0'
end

it "returns the version from the server" do
WebMock.stub_request(:get, "http://localhost:8200/")
.to_return(status: 200, body: '{"version": 8.0}')
expect(Config.new.version).to eq 8.0
.to_return(status: 200, body: '{"version": 8.5}')
expect(Config.new.version).to eq '8.5'
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions spec/elastic_apm/instrumenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,5 +445,29 @@ module ElasticAPM
subject.stop
end
end

describe 'unsampled transactions' do
let(:config) { Config.new(transaction_sample_rate: '0') }

context 'when the server version is less than 8.0' do
it 'enqueues the transaction' do
stub_request(:get, "http://localhost:8200/")
.to_return(status: 200, body: '{"version": "7.17.4"}')
expect(subject.enqueue).to receive(:call).and_call_original
subject.start_transaction 'Test', config: config
subject.end_transaction
end
end

context 'when the server version is at least 8.0' do
it 'does not enqueue the transaction' do
stub_request(:get, "http://localhost:8200/")
.to_return(status: 200, body: '{"version": "8.5.3"}')
expect(subject.enqueue).not_to receive(:call)
subject.start_transaction 'Test', config: config
subject.end_transaction
end
end
end
end
end
20 changes: 20 additions & 0 deletions spec/elastic_apm/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ class MiddlewareTestError < StandardError; end
context 'with valid header' do
it 'recognizes trace_context' do
with_agent do
# The apm server version must be < 8.0 in order for a transaction
# to be enqueued when it's unsampled.
# See issue https://github.com/elastic/apm-agent-ruby/issues/1340
WebMock.stub_request(:get, %r{^http://localhost:8200/$}).
to_return(body: '{"version":"7.17.4"}')
app.call(
Rack::MockRequest.env_for(
'/',
Expand All @@ -136,6 +141,11 @@ class MiddlewareTestError < StandardError; end
context 'with tracestate' do
it 'recognizes trace_context' do
with_agent do
# The apm server version must be < 8.0 in order for a transaction
# to be enqueued when it's unsampled.
# See issue https://github.com/elastic/apm-agent-ruby/issues/1340
WebMock.stub_request(:get, %r{^http://localhost:8200/$}).
to_return(body: '{"version":"7.17.4"}')
app.call(
Rack::MockRequest.env_for(
'/',
Expand Down Expand Up @@ -191,6 +201,11 @@ class MiddlewareTestError < StandardError; end
context 'with a prefix-less header' do
it 'recognizes trace_context' do
with_agent do
# The apm server version must be < 8.0 in order for a transaction
# to be enqueued when it's unsampled.
# See issue https://github.com/elastic/apm-agent-ruby/issues/1340
WebMock.stub_request(:get, %r{^http://localhost:8200/$}).
to_return(body: '{"version":"7.17.4"}')
app.call(
Rack::MockRequest.env_for(
'/',
Expand All @@ -212,6 +227,11 @@ class MiddlewareTestError < StandardError; end
context 'with both types of headers' do
it 'picks the prefixed' do
with_agent do
# The apm server version must be < 8.0 in order for a transaction
# to be enqueued when it's unsampled.
# See issue https://github.com/elastic/apm-agent-ruby/issues/1340
WebMock.stub_request(:get, %r{^http://localhost:8200/$}).
to_return(body: '{"version":"7.17.4"}')
app.call(
Rack::MockRequest.env_for(
'/',
Expand Down
2 changes: 1 addition & 1 deletion spec/support/with_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def with_agent(klass: ElasticAPM, args: [], **config)

@server_version_stub =
WebMock.stub_request(:get, %r{^http://localhost:8200/$}).
to_return(body: '{"version":8.0}')
to_return(body: '{"version":"8.0"}')

klass.start(*args, **config)
yield
Expand Down