Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions lib/elastic_apm/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
require 'elastic_apm/config/regexp_list'
require 'elastic_apm/config/wildcard_pattern_list'
require 'elastic_apm/deprecations'
require 'elastic_apm/config/server_info'

module ElasticAPM
# @api private
Expand Down Expand Up @@ -243,6 +244,10 @@ def inspect
super.split.first + '>'
end

def version
@version ||= ServerInfo.new(self).version
end

private

def load_config_file
Expand Down
45 changes: 45 additions & 0 deletions lib/elastic_apm/config/server_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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

module ElasticAPM
class Config
# @api private
class ServerInfo
attr_reader :payload, :config, :http

def initialize(config)
@config = config
@http = Transport::Connection::Http.new(config)
end

def execute
resp = http.get(config.server_url)
@payload = JSON.parse(resp.body)
rescue
@payload = {"version" => nil}
end

def version
execute unless payload
payload["version"]
end
end
end
end

14 changes: 14 additions & 0 deletions spec/elastic_apm/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,5 +294,19 @@ module ElasticAPM
expect(subject.logger.level).to eq(Logger::INFO)
end
end

describe "#version" do
it "has no version 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
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
end
end
end
end
5 changes: 5 additions & 0 deletions spec/support/mock_intake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def stub!
:get, %r{^http://localhost:8200/config/v1/agents/?$}
).to_return(body: '{}')

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

@request_stub =
WebMock.stub_request(
:post, %r{^http://localhost:8200/intake/v2/events/?$}
Expand Down Expand Up @@ -95,6 +99,7 @@ def reset!
@request_stub = nil
@central_config_stub = nil
@cloud_provider_stubs = nil
@server_version_stub = nil
end

def call(env)
Expand Down
5 changes: 5 additions & 0 deletions spec/support/with_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ def with_agent(klass: ElasticAPM, args: [], **config)
:get, %r{^http://localhost:8200/config/v1/agents/?$}
).to_return(body: '{}')

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

klass.start(*args, **config)
yield
ensure
ElasticAPM.stop

@central_config_stub = nil
@server_version_stub = nil
end
end