Merge pull request #37 from yantang-msft/AddCloudContext

Update telemetry context data contract to match current schema
This commit is contained in:
Sergey Kanzhelev 2018-04-23 12:04:03 -07:00 коммит произвёл GitHub
Родитель 32b098548f 03142241ee
Коммит 6cfa983d44
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
11 изменённых файлов: 163 добавлений и 7 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -92,6 +92,7 @@ module ApplicationInsights
[internal_context,
context.application,
context.cloud,
context.device,
context.user,
context.session,

Просмотреть файл

@ -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.

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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)