Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update user agent info to match spec #1182

Merged
merged 2 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ endif::[]

- Fixed span context fields for DynamoDB instrumentation {pull}1178[#1178]
- Fixed span context fields for S3 instrumentation {pull}1179[#1179]
- Update user agent info to match spec {pull}1182[#1182]

[[release-notes-4.4.0]]
==== 4.4.0
Expand Down
2 changes: 1 addition & 1 deletion lib/elastic_apm/metadata/service_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def initialize(config)
)
@language = Language.new(name: 'ruby', version: RUBY_VERSION)
@runtime = lookup_runtime
@version = @config.service_version || Util.git_sha
@version = @config.service_version
end

attr_reader :name, :node_name, :environment, :agent, :framework,
Expand Down
18 changes: 12 additions & 6 deletions lib/elastic_apm/transport/user_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ def build(config)

[
"elastic-apm-ruby/#{@version}",
HTTP::Request::USER_AGENT,
[
service.runtime.name,
service.runtime.version
].join('/')
].join(' ')
formatted_service_info(service)
].compact.join(' ')
end

def formatted_service_info(service)
if service.name
"(#{[
service.name,
service.version
].compact.join(' ')
})"
end
end
end
end
Expand Down
4 changes: 0 additions & 4 deletions spec/elastic_apm/metadata/service_info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ module ElasticAPM
expect(subject.runtime.name).to eq 'jruby'
expect(subject.runtime.version).to_not be_nil
end

it 'has a version from git' do
expect(subject.version).to match(/[a-z0-9]{16}/) # git sha
end
end
end
end
4 changes: 1 addition & 3 deletions spec/elastic_apm/transport/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ module Transport
headers: {
'User-Agent' =>
%r{
\Aelastic-apm-ruby/(\d+\.)+\d([a-z0-9\.]+)?+\s
http.rb/(\d+\.)+\d+\s
j?ruby/(\d+\.)+\d+\z
\Aelastic-apm-ruby/(\d+\.)+\d([a-z0-9\.]+)?+
}x
}
)
Expand Down
54 changes: 43 additions & 11 deletions spec/elastic_apm/transport/user_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,56 @@
module ElasticAPM
module Transport
RSpec.describe UserAgent do
REGEXP =
%r{
\Aelastic-apm-ruby/(\d+\.)+\d([a-z0-9\.]+)?+\s
http.rb/(\d+\.)+\d+\s
j?ruby/(\d+\.)+\d+\z
}x

let(:config) { Config.new }
subject { described_class.new(config) }

describe 'to_s' do
context 'when there is no service name or version' do
let(:regexp) do
%r{
\Aelastic-apm-ruby\/(\d+\.)+\d([a-z0-9\.]+)?+
}x
end

let(:config) { Config.new }

it 'builds a string' do
expect(subject.to_s).to match(REGEXP)
expect(subject.to_s).to match(regexp)
end

it 'handles beta versions' do
subject = described_class.new(config, version: '12.13.14.beta.20')
expect(subject.to_s).to match(REGEXP)
expect(subject.to_s).to match(regexp)
end
end

context 'when there is service name only' do
let(:regexp) do
%r{
\Aelastic-apm-ruby/(\d+\.)+\d([a-z0-9\.]+)?+\s
\(MyService\)
}x
end

let(:config) { Config.new(service_name: "MyService") }

it 'builds a string' do
expect(subject.to_s).to match(regexp)
end
end

context 'when there is service name and version' do
let(:regexp) do
%r{
\Aelastic-apm-ruby\/(\d+\.)+\d([a-z0-9\.]+)?+\s
\(MyService\sv42\)
}x
end

let(:config) do
Config.new(service_name: "MyService", service_version: 'v42')
end

it 'builds a string' do
expect(subject.to_s).to match(regexp)
end
end
end
Expand Down