Skip to content

Commit

Permalink
Update user agent info to match spec (#1182)
Browse files Browse the repository at this point in the history
* Update user agent info to match spec

* Add changelog entry
  • Loading branch information
estolfo committed Mar 2, 2023
1 parent 07f0f1e commit 03c19b4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 25 deletions.
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

0 comments on commit 03c19b4

Please sign in to comment.