Skip to content

Commit

Permalink
Moved telemetry to a policy (Azure#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina authored Nov 29, 2018
1 parent bfeb32d commit fbed8bb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
36 changes: 29 additions & 7 deletions Azure.Configuration.Test/ConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using static System.Buffers.Text.Encodings;

namespace Azure.Configuration.Tests
{
Expand All @@ -32,9 +34,9 @@ public async Task SetKeyValue()
var transport = new SetKeyValueMockTransport();
transport.KeyValue = s_testKey;

var pipeline = new ServicePipeline(transport, new RetryPolicy());
var service = new ConfigurationService(uri, credential, secret);
service.Pipeline.Transport = transport;

var service = new ConfigurationService(uri, credential, secret, pipeline);
Response<KeyValue> added = await service.SetKeyValueAsync(s_testKey, CancellationToken.None);

Assert.AreEqual(s_testKey.Key, added.Result.Key);
Expand All @@ -52,9 +54,9 @@ public async Task GetKeyValue()
var transport = new GetKeyValueMockTransport();
transport.KeyValue = s_testKey;

var pipeline = new ServicePipeline(transport, new RetryPolicy());
var service = new ConfigurationService(uri, credential, secret);
service.Pipeline.Transport = transport;

var service = new ConfigurationService(uri, credential, secret, pipeline);
Response<KeyValue> added = await service.GetKeyValueAsync("test", default, CancellationToken.None);

Assert.AreEqual(s_testKey.Key, added.Result.Key);
Expand All @@ -64,16 +66,35 @@ public async Task GetKeyValue()
}
}

class SetKeyValueMockTransport : HttpClientTransport
abstract class MockHttpClientTransport: HttpClientTransport
{
protected static void VerifyUserAgentHeader(HttpRequestMessage request)
{
var expected = Utf8.ToString(Header.Common.CreateUserAgent("Azure-Configuration", "1.0.0").Value);

Assert.True(request.Headers.Contains("User-Agent"));
var userAgentValues = request.Headers.GetValues("User-Agent");

foreach(var value in userAgentValues)
{
if (expected.StartsWith(value)) return;
}
Assert.Fail("could not find User-Agent header value " + expected);
}
}

class SetKeyValueMockTransport : MockHttpClientTransport
{
public KeyValue KeyValue;

protected override Task<HttpResponseMessage> ProcessCoreAsync(CancellationToken cancellation, HttpRequestMessage request)
{
Assert.AreEqual(HttpMethod.Put, request.Method);
VerifyUserAgentHeader(request);

HttpResponseMessage response = new HttpResponseMessage();
if (request.RequestUri.AbsolutePath.StartsWith($"/kv/{KeyValue.Key}")) {
if (request.RequestUri.AbsolutePath.StartsWith($"/kv/{KeyValue.Key}"))
{
response.StatusCode = HttpStatusCode.OK;
string json = JsonConvert.SerializeObject(KeyValue).ToLowerInvariant();
long jsonBytes = Encoding.UTF8.GetByteCount(json);
Expand All @@ -85,13 +106,14 @@ protected override Task<HttpResponseMessage> ProcessCoreAsync(CancellationToken
}
}

class GetKeyValueMockTransport : HttpClientTransport
class GetKeyValueMockTransport : MockHttpClientTransport
{
public KeyValue KeyValue;

protected override Task<HttpResponseMessage> ProcessCoreAsync(CancellationToken cancellation, HttpRequestMessage request)
{
Assert.AreEqual(HttpMethod.Get, request.Method);
VerifyUserAgentHeader(request);

HttpResponseMessage response = new HttpResponseMessage();
if (request.RequestUri.AbsolutePath.StartsWith($"/kv/{KeyValue.Key}")) {
Expand Down
41 changes: 23 additions & 18 deletions Azure.Configuration/ConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,45 @@
using Azure.Core.Net.Pipeline;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static System.Buffers.Text.Encodings;

namespace Azure.Configuration
{
public partial class ConfigurationService
{
readonly ServicePipeline _pipeline;
internal const string SdkName = "Azure-Configuration";
internal const string SdkVersion = "1.0.0";
readonly Uri _baseUri;
readonly string _credential;
readonly byte[] _secret;

public ServicePipeline Pipeline { get; }

public readonly ConfigurationServiceOptions Options = new ConfigurationServiceOptions("v1.0");

static ServicePipeline CreateDefaultPipeline()
{
var pipeline = new ServicePipeline(
new HttpClientTransport(),
new LoggingPolicy(),
new RetryPolicy(),
new TelemetryPolicy(SdkName, SdkVersion, null)
);
return pipeline;
}

public ConfigurationService(Uri baseUri, string credential, byte[] secret, ServicePipeline pipeline)
{
_pipeline = pipeline;
Pipeline = pipeline;
_baseUri = baseUri;
_credential = credential;
_secret = secret;
}

public ConfigurationService(Uri baseUri, string credential, byte[] secret)
: this(baseUri, credential, secret, new ServicePipeline(new HttpClientTransport(), new LoggingPolicy(), new RetryPolicy()))
: this(baseUri, credential, secret, CreateDefaultPipeline())
{ }

public async Task<Response<KeyValue>> SetKeyValueAsync(KeyValue setting, CancellationToken cancellation)
Expand All @@ -45,15 +54,14 @@ public async Task<Response<KeyValue>> SetKeyValueAsync(KeyValue setting, Cancell
ServiceCallContext context = null;
try
{
context = _pipeline.CreateContext(cancellation, ServiceMethod.Put, url);
context.AddHeader(Options.UserAgentHeader);
context = Pipeline.CreateContext(cancellation, ServiceMethod.Put, url);

context.AddHeader("Accept", MediaTypeKeyValueApplication);
context.AddHeader(Header.Common.JsonContentType);

WriteJsonContent(setting, context);

await _pipeline.ProcessAsync(context).ConfigureAwait(false);
await Pipeline.ProcessAsync(context).ConfigureAwait(false);

ServiceResponse response = context.Response;
if (!response.TryGetHeader(Header.Constants.ContentLength, out long contentLength))
Expand Down Expand Up @@ -87,8 +95,7 @@ public async Task<Response<KeyValue>> GetKeyValueAsync(string key, GetKeyValueOp

ServiceCallContext context = null;
try {
context = _pipeline.CreateContext(cancellation, ServiceMethod.Get, url);
context.AddHeader(Options.UserAgentHeader);
context = Pipeline.CreateContext(cancellation, ServiceMethod.Get, url);

context.AddHeader("Accept", MediaTypeKeyValueApplication);

Expand All @@ -97,7 +104,7 @@ public async Task<Response<KeyValue>> GetKeyValueAsync(string key, GetKeyValueOp
context.AddHeader(AcceptDatetimeHeader, dateTime);
}

await _pipeline.ProcessAsync(context).ConfigureAwait(false);
await Pipeline.ProcessAsync(context).ConfigureAwait(false);

ServiceResponse response = context.Response;
if (!response.TryGetHeader(Header.Constants.ContentLength, out long contentLength)) {
Expand All @@ -123,13 +130,11 @@ public struct ConfigurationServiceOptions
{
internal Header UserAgentHeader;
string _applicationId;
string _apiVersion;

public ConfigurationServiceOptions(string apiVersion)
{
_apiVersion = apiVersion;
_applicationId = default;
UserAgentHeader = Header.Common.CreateUserAgent(sdkName: "Azure-Configuration", sdkVersion: "1.0.0", _applicationId);
UserAgentHeader = Header.Common.CreateUserAgent(SdkName, SdkVersion, _applicationId);
}

public string ApplicationId
Expand All @@ -138,7 +143,7 @@ public string ApplicationId
set {
if (string.Equals(_applicationId, value, StringComparison.Ordinal)) return;
_applicationId = value;
UserAgentHeader = Header.Common.CreateUserAgent(sdkName: "Azure-CognitiveServices-Face", sdkVersion: "1.0.0", _applicationId);
UserAgentHeader = Header.Common.CreateUserAgent(SdkName, SdkVersion, _applicationId);
}
}

Expand Down

0 comments on commit fbed8bb

Please sign in to comment.