From 03142241eebd50c90a1517106cd445da5dae7751 Mon Sep 17 00:00:00 2001 From: yantang Date: Mon, 2 Apr 2018 14:04:13 -0700 Subject: [PATCH] Update telemetry context data contract to match current schema --- .../channel/contracts/cloud.rb | 14 ++++++ .../channel/contracts/location.rb | 7 ++- .../channel/contracts/operation.rb | 5 ++- .../channel/telemetry_channel.rb | 1 + .../channel/telemetry_context.rb | 7 +++ .../channel/contracts/test_cloud.rb | 44 +++++++++++++++++++ .../channel/contracts/test_location.rb | 41 ++++++++++++++++- .../channel/contracts/test_operation.rb | 15 ++++++- .../channel/test_telemetry_channel.rb | 26 +++++++++++ .../channel/test_telemetry_context.rb | 8 ++++ .../rack/test_track_request.rb | 2 +- 11 files changed, 163 insertions(+), 7 deletions(-) create mode 100644 lib/application_insights/channel/contracts/cloud.rb create mode 100644 test/application_insights/channel/contracts/test_cloud.rb diff --git a/lib/application_insights/channel/contracts/cloud.rb b/lib/application_insights/channel/contracts/cloud.rb new file mode 100644 index 0000000..1885acd --- /dev/null +++ b/lib/application_insights/channel/contracts/cloud.rb @@ -0,0 +1,14 @@ +require_relative 'json_serializable' + +module ApplicationInsights::Channel::Contracts + class Cloud + include JsonSerializable + + attr_accessor :role_name, :role_instance + + attribute_mapping( + role_name: 'ai.cloud.role', + role_instance: 'ai.cloud.roleInstance' + ) + end +end diff --git a/lib/application_insights/channel/contracts/location.rb b/lib/application_insights/channel/contracts/location.rb index 4136c86..17d157d 100644 --- a/lib/application_insights/channel/contracts/location.rb +++ b/lib/application_insights/channel/contracts/location.rb @@ -4,10 +4,13 @@ module ApplicationInsights::Channel::Contracts class Location include JsonSerializable - attr_accessor :ip + attr_accessor :ip, :country, :province, :city attribute_mapping( - ip: 'ai.location.ip' + ip: 'ai.location.ip', + country: 'ai.location.country', + province: 'ai.location.province', + city: 'ai.location.city' ) end end diff --git a/lib/application_insights/channel/contracts/operation.rb b/lib/application_insights/channel/contracts/operation.rb index 29e5d02..0a5142e 100644 --- a/lib/application_insights/channel/contracts/operation.rb +++ b/lib/application_insights/channel/contracts/operation.rb @@ -4,7 +4,7 @@ module ApplicationInsights::Channel::Contracts class Operation include JsonSerializable - attr_accessor :id, :name, :parent_id, :root_id, :synthetic_source, :is_synthetic + attr_accessor :id, :name, :parent_id, :root_id, :synthetic_source, :is_synthetic, :correlation_vector attribute_mapping( id: 'ai.operation.id', @@ -12,7 +12,8 @@ module ApplicationInsights::Channel::Contracts parent_id: 'ai.operation.parentId', root_id: 'ai.operation.rootId', synthetic_source: 'ai.operation.syntheticSource', - is_synthetic: 'ai.operation.isSynthetic' + is_synthetic: 'ai.operation.isSynthetic', + correlation_vector: "ai.operation.correlationVector" ) end end diff --git a/lib/application_insights/channel/telemetry_channel.rb b/lib/application_insights/channel/telemetry_channel.rb index 7dc187f..a6f1b5f 100644 --- a/lib/application_insights/channel/telemetry_channel.rb +++ b/lib/application_insights/channel/telemetry_channel.rb @@ -92,6 +92,7 @@ module ApplicationInsights [internal_context, context.application, + context.cloud, context.device, context.user, context.session, diff --git a/lib/application_insights/channel/telemetry_context.rb b/lib/application_insights/channel/telemetry_context.rb index 37772f6..bb24af2 100644 --- a/lib/application_insights/channel/telemetry_context.rb +++ b/lib/application_insights/channel/telemetry_context.rb @@ -1,4 +1,5 @@ require_relative 'contracts/application' +require_relative 'contracts/cloud' require_relative 'contracts/device' require_relative 'contracts/user' require_relative 'contracts/session' @@ -26,6 +27,7 @@ module ApplicationInsights def initialize @instrumentation_key = nil @application = Contracts::Application.new + @cloud = Contracts::Cloud.new @device = Contracts::Device.new @user = Contracts::User.new @session = Contracts::Session.new @@ -44,6 +46,11 @@ module ApplicationInsights # @return [Contracts::Application] the context object. attr_accessor :application + # The cloud context. This contains properties of the + # cloud role you are generating telemetry for. + # @return [Contracts::Cloud] the context object. + attr_accessor :cloud + # The device context. This contains properties of the # device you are running on. # @return [Contracts::Device] the context object. diff --git a/test/application_insights/channel/contracts/test_cloud.rb b/test/application_insights/channel/contracts/test_cloud.rb new file mode 100644 index 0000000..95ec148 --- /dev/null +++ b/test/application_insights/channel/contracts/test_cloud.rb @@ -0,0 +1,44 @@ +require_relative '../../../../lib/application_insights/channel/contracts/cloud' +require 'test/unit' + +include ApplicationInsights::Channel + +class TestCloud < Test::Unit::TestCase + def test_initialize + item = Contracts::Cloud.new + assert_not_nil item + end + + def test_role_name_works_as_expected + expected = 'Test string' + item = Contracts::Cloud.new + item.role_name = expected + actual = item.role_name + assert_equal expected, actual + expected = 'Other string' + item.role_name = expected + actual = item.role_name + assert_equal expected, actual + end + + def test_role_instance_works_as_expected + expected = 'Test string' + item = Contracts::Cloud.new + item.role_instance = expected + actual = item.role_instance + assert_equal expected, actual + expected = 'Other string' + item.role_instance = expected + actual = item.role_instance + assert_equal expected, actual + end + + def test_to_json_works_as_expected + item = Contracts::Cloud.new + item.role_name = 'Role name' + item.role_instance = 'Role instance' + actual = item.to_json + expected = '{"ai.cloud.role":"Role name","ai.cloud.roleInstance":"Role instance"}' + assert_equal expected, actual + end +end diff --git a/test/application_insights/channel/contracts/test_location.rb b/test/application_insights/channel/contracts/test_location.rb index d30dd58..8236d67 100644 --- a/test/application_insights/channel/contracts/test_location.rb +++ b/test/application_insights/channel/contracts/test_location.rb @@ -21,11 +21,50 @@ class TestLocation < Test::Unit::TestCase assert_equal expected, actual end + def test_country_works_as_expected + expected = 'Test string' + item = Contracts::Location.new + item.country = expected + actual = item.country + assert_equal expected, actual + expected = 'Other string' + item.country = expected + actual = item.country + assert_equal expected, actual + end + + def test_province_works_as_expected + expected = 'Test string' + item = Contracts::Location.new + item.province = expected + actual = item.province + assert_equal expected, actual + expected = 'Other string' + item.province = expected + actual = item.province + assert_equal expected, actual + end + + def test_city_works_as_expected + expected = 'Test string' + item = Contracts::Location.new + item.city = expected + actual = item.city + assert_equal expected, actual + expected = 'Other string' + item.city = expected + actual = item.city + assert_equal expected, actual + end + def test_to_json_works_as_expected item = Contracts::Location.new item.ip = 'Test string' + item.country = 'US' + item.province = 'WA' + item.city = 'Redmond' actual = item.to_json - expected = '{"ai.location.ip":"Test string"}' + expected = '{"ai.location.ip":"Test string","ai.location.country":"US","ai.location.province":"WA","ai.location.city":"Redmond"}' assert_equal expected, actual end end diff --git a/test/application_insights/channel/contracts/test_operation.rb b/test/application_insights/channel/contracts/test_operation.rb index 4c4b021..bffb18a 100644 --- a/test/application_insights/channel/contracts/test_operation.rb +++ b/test/application_insights/channel/contracts/test_operation.rb @@ -81,6 +81,18 @@ class TestOperation < Test::Unit::TestCase assert_equal expected, actual end + def test_correlation_vector_works_as_expected + expected = 'Test string' + item = Contracts::Operation.new + item.correlation_vector = expected + actual = item.correlation_vector + assert_equal expected, actual + expected = 'Other string' + item.correlation_vector = expected + actual = item.correlation_vector + assert_equal expected, actual + end + def test_to_json_works_as_expected item = Contracts::Operation.new item.id = 'Test string' @@ -89,8 +101,9 @@ class TestOperation < Test::Unit::TestCase item.root_id = 'Test string' item.synthetic_source = 'Test string' item.is_synthetic = 'Test string' + item.correlation_vector = 'Test string' actual = item.to_json - expected = '{"ai.operation.id":"Test string","ai.operation.name":"Test string","ai.operation.parentId":"Test string","ai.operation.rootId":"Test string","ai.operation.syntheticSource":"Test string","ai.operation.isSynthetic":"Test string"}' + expected = '{"ai.operation.id":"Test string","ai.operation.name":"Test string","ai.operation.parentId":"Test string","ai.operation.rootId":"Test string","ai.operation.syntheticSource":"Test string","ai.operation.isSynthetic":"Test string","ai.operation.correlationVector":"Test string"}' assert_equal expected, actual end end diff --git a/test/application_insights/channel/test_telemetry_channel.rb b/test/application_insights/channel/test_telemetry_channel.rb index 8c9edd4..99764e4 100644 --- a/test/application_insights/channel/test_telemetry_channel.rb +++ b/test/application_insights/channel/test_telemetry_channel.rb @@ -71,6 +71,32 @@ class TestTelemetryChannel < Test::Unit::TestCase assert_equal 'MockTelemetryItemData', actual.data.base_type assert_same expected, actual.data.base_data end + + def test_get_tags_works_as_expected + queue = MockTelemetryChannelQueue.new SynchronousSender.new + context = TelemetryContext.new + context.application.ver = 'ver' + context.cloud.role_name = 'role name' + context.device.id = 'device id' + context.user.id = 'user id' + context.session.id = 'session id' + context.location.ip = 'ip' + context.operation.id = 'operation id' + channel = TelemetryChannel.new context, queue + expected = MockTelemetryItemData.new + channel.write expected + + assert_equal 1, queue.queue.count + tags = queue.queue[0].tags + assert_equal 'rb:'+ ApplicationInsights::VERSION, tags['ai.internal.sdkVersion'] + assert_equal 'ver', tags['ai.application.ver'] + assert_equal 'role name', tags['ai.cloud.role'] + assert_equal 'device id', tags['ai.device.id'] + assert_equal 'user id', tags['ai.user.id'] + assert_equal 'session id', tags['ai.session.id'] + assert_equal 'ip', tags['ai.location.ip'] + assert_equal 'operation id', tags['ai.operation.id'] + end end class MockTelemetryItemData diff --git a/test/application_insights/channel/test_telemetry_context.rb b/test/application_insights/channel/test_telemetry_context.rb index 5a92354..4a77fae 100644 --- a/test/application_insights/channel/test_telemetry_context.rb +++ b/test/application_insights/channel/test_telemetry_context.rb @@ -24,6 +24,14 @@ class TestTelemetryContext < Test::Unit::TestCase assert_same expected, context.application end + def test_cloud_works_as_expected + context = TelemetryContext.new + assert_not_nil context.cloud + expected = Contracts::Cloud.new + context.cloud = expected + assert_same expected, context.cloud + end + def test_device_works_as_expected context = TelemetryContext.new assert_not_nil context.device diff --git a/test/application_insights/rack/test_track_request.rb b/test/application_insights/rack/test_track_request.rb index 8c58670..06a09cc 100644 --- a/test/application_insights/rack/test_track_request.rb +++ b/test/application_insights/rack/test_track_request.rb @@ -11,7 +11,7 @@ class TestTrackRequest < Test::Unit::TestCase def test_call_works_as_expected response_code = rand(200..204) - app = Proc.new {|env| sleep(2); [response_code, {"Content-Type" => "text/html"}, ["Hello Rack!"]]} + app = Proc.new {|env| sleep(2.5); [response_code, {"Content-Type" => "text/html"}, ["Hello Rack!"]]} url = "http://localhost:8080/foo?a=b" http_method = 'PUT' env = Rack::MockRequest.env_for(url, :method => http_method)