Skip to content

Commit

Permalink
Merged PR 885638: Add runtime version to DeviceClient user agent string
Browse files Browse the repository at this point in the history
Add runtime version to DeviceClient user agent string
  • Loading branch information
varunpuranik committed Jun 10, 2018
1 parent 731827e commit 1f7c27b
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ namespace Microsoft.Azure.Devices.Edge.Agent.IoTHub
public class EnvironmentModuleClientProvider : IModuleClientProvider
{
readonly Option<UpstreamProtocol> upstreamProtocol;
readonly Option<string> productInfo;

public EnvironmentModuleClientProvider(Option<UpstreamProtocol> upstreamProtocol)
public EnvironmentModuleClientProvider(Option<UpstreamProtocol> upstreamProtocol, Option<string> productInfo)
{
this.upstreamProtocol = upstreamProtocol;
this.productInfo = productInfo;
}

public Task<IModuleClient> Create(
ConnectionStatusChangesHandler statusChangedHandler,
Func<IModuleClient, Task> initialize) =>
ModuleClient.Create(this.upstreamProtocol, statusChangedHandler, initialize);
ModuleClient.Create(Option.None<string>(), this.upstreamProtocol, statusChangedHandler, initialize, this.productInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Microsoft.Azure.Devices.Edge.Agent.IoTHub
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Client.Edge;
using Microsoft.Azure.Devices.Client.Exceptions;
using Microsoft.Azure.Devices.Edge.Agent.Core;
using Microsoft.Azure.Devices.Edge.Util;
Expand Down Expand Up @@ -35,20 +34,12 @@ public class ModuleClient : IModuleClient
this.deviceClient = deviceClient;
}

public static Task<IModuleClient> Create(Option<UpstreamProtocol> upstreamProtocol,
ConnectionStatusChangesHandler statusChangedHandler,
Func<ModuleClient, Task> initialize)
{
return Create(upstreamProtocol, initialize, t => CreateAndOpenDeviceClient(t, statusChangedHandler));
}

public static Task<IModuleClient> Create(string connectionString,
public static Task<IModuleClient> Create(Option<string> connectionString,
Option<UpstreamProtocol> upstreamProtocol,
ConnectionStatusChangesHandler statusChangedHandler,
Func<ModuleClient, Task> initialize)
{
return Create(upstreamProtocol, initialize, t => CreateAndOpenDeviceClient(t, connectionString, statusChangedHandler));
}
Func<ModuleClient, Task> initialize,
Option<string> productInfo) =>
Create(upstreamProtocol, initialize, t => CreateAndOpenDeviceClient(t, connectionString, statusChangedHandler, productInfo));

static async Task<IModuleClient> Create(Option<UpstreamProtocol> upstreamProtocol, Func<ModuleClient, Task> initialize, Func<TransportType, Task<Client.ModuleClient>> deviceClientCreator)
{
Expand Down Expand Up @@ -98,19 +89,15 @@ static async Task<IModuleClient> Create(Option<UpstreamProtocol> upstreamProtoco
return result.Value;
});

static async Task<Client.ModuleClient> CreateAndOpenDeviceClient(TransportType transport, ConnectionStatusChangesHandler statusChangedHandler)
{
Events.AttemptingConnectionWithTransport(transport);
Client.ModuleClient deviceClient = await Client.ModuleClient.CreateFromEnvironmentAsync(transport);
await OpenAsync(statusChangedHandler, deviceClient);
Events.ConnectedWithTransport(transport);
return deviceClient;
}

static async Task<Client.ModuleClient> CreateAndOpenDeviceClient(TransportType transport, string connectionString, ConnectionStatusChangesHandler statusChangedHandler)
static async Task<Client.ModuleClient> CreateAndOpenDeviceClient(TransportType transport,
Option<string> connectionString,
ConnectionStatusChangesHandler statusChangedHandler,
Option<string> productInfo)
{
Events.AttemptingConnectionWithTransport(transport);
Client.ModuleClient deviceClient = Client.ModuleClient.CreateFromConnectionString(connectionString, transport);
Client.ModuleClient deviceClient = await connectionString.Map(cs => Task.FromResult(Client.ModuleClient.CreateFromConnectionString(cs, transport)))
.GetOrElse(() => Client.ModuleClient.CreateFromEnvironmentAsync(transport));
productInfo.ForEach(p => deviceClient.ProductInfo = p);
await OpenAsync(statusChangedHandler, deviceClient);
Events.ConnectedWithTransport(transport);
return deviceClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ public class ModuleClientProvider : IModuleClientProvider
{
readonly string edgeAgentConnectionString;
readonly Option<UpstreamProtocol> upstreamProtocol;
readonly Option<string> productInfo;

public ModuleClientProvider(string edgeAgentConnectionString, Option<UpstreamProtocol> upstreamProtocol)
public ModuleClientProvider(string edgeAgentConnectionString, Option<UpstreamProtocol> upstreamProtocol, Option<string> productInfo)
{
this.edgeAgentConnectionString = Preconditions.CheckNonWhiteSpace(edgeAgentConnectionString, nameof(edgeAgentConnectionString));
this.upstreamProtocol = upstreamProtocol;
this.productInfo = productInfo;
}

public Task<IModuleClient> Create(
ConnectionStatusChangesHandler statusChangedHandler,
Func<IModuleClient, Task> initialize) =>
ModuleClient.Create(this.edgeAgentConnectionString, this.upstreamProtocol, statusChangedHandler, initialize);
ModuleClient.Create(Option.Some(this.edgeAgentConnectionString), this.upstreamProtocol, statusChangedHandler, initialize, this.productInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static async Task<int> MainAsync(IConfiguration configuration)
VersionInfo versionInfo = VersionInfo.Get(VersionInfoFileName);
if (versionInfo != VersionInfo.Empty)
{
logger.LogInformation($"Version - {versionInfo}");
logger.LogInformation($"Version - {versionInfo.ToString(true)}");
}
LogLogo(logger);

Expand Down Expand Up @@ -99,22 +99,22 @@ public static async Task<int> MainAsync(IConfiguration configuration)
var builder = new ContainerBuilder();
builder.RegisterModule(new AgentModule(maxRestartCount, intensiveCareTime, coolOffTimeUnitInSeconds, usePersistentStorage, storagePath));
builder.RegisterModule(new LoggingModule(dockerLoggingDriver, dockerLoggingOptions));

Option<string> productInfo = versionInfo != VersionInfo.Empty ? Option.Some(versionInfo.ToString()) : Option.None<string>();
Option<UpstreamProtocol> upstreamProtocol = configuration.GetValue<string>(Constants.UpstreamProtocolKey).ToUpstreamProtocol();
switch (mode.ToLowerInvariant())
{
case Constants.DockerMode:
var dockerUri = new Uri(configuration.GetValue<string>("DockerUri"));
string deviceConnectionString = configuration.GetValue<string>("DeviceConnectionString");
builder.RegisterModule(new DockerModule(deviceConnectionString, edgeDeviceHostName, dockerUri, dockerAuthConfig, upstreamProtocol));
builder.RegisterModule(new DockerModule(deviceConnectionString, edgeDeviceHostName, dockerUri, dockerAuthConfig, upstreamProtocol, productInfo));
break;

case Constants.IotedgedMode:
string managementUri = configuration.GetValue<string>(Constants.EdgeletManagementUriVariableName);
string workloadUri = configuration.GetValue<string>(Constants.EdgeletWorkloadUriVariableName);
string iothubHostname = configuration.GetValue<string>(Constants.IotHubHostnameVariableName);
string deviceId = configuration.GetValue<string>(Constants.DeviceIdVariableName);
builder.RegisterModule(new EdgeletModule(iothubHostname, edgeDeviceHostName, deviceId, new Uri(managementUri), new Uri(workloadUri), dockerAuthConfig, upstreamProtocol));
builder.RegisterModule(new EdgeletModule(iothubHostname, edgeDeviceHostName, deviceId, new Uri(managementUri), new Uri(workloadUri), dockerAuthConfig, upstreamProtocol, productInfo));
break;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public class DockerModule : Module
readonly Uri dockerHostname;
readonly IEnumerable<AuthConfig> dockerAuthConfig;
readonly Option<UpstreamProtocol> upstreamProtocol;
readonly Option<string> productInfo;

public DockerModule(string edgeDeviceConnectionString, string gatewayHostName, Uri dockerHostname, IEnumerable<AuthConfig> dockerAuthConfig, Option<UpstreamProtocol> upstreamProtocol)
public DockerModule(string edgeDeviceConnectionString, string gatewayHostName, Uri dockerHostname,
IEnumerable<AuthConfig> dockerAuthConfig, Option<UpstreamProtocol> upstreamProtocol, Option<string> productInfo)
{
this.edgeDeviceConnectionString = Preconditions.CheckNonWhiteSpace(edgeDeviceConnectionString, nameof(edgeDeviceConnectionString));
this.gatewayHostName = Preconditions.CheckNonWhiteSpace(gatewayHostName, nameof(gatewayHostName));
Expand All @@ -35,13 +37,14 @@ public DockerModule(string edgeDeviceConnectionString, string gatewayHostName, U
this.dockerHostname = Preconditions.CheckNotNull(dockerHostname, nameof(dockerHostname));
this.dockerAuthConfig = Preconditions.CheckNotNull(dockerAuthConfig, nameof(dockerAuthConfig));
this.upstreamProtocol = Preconditions.CheckNotNull(upstreamProtocol, nameof(upstreamProtocol));
this.productInfo = productInfo;
}

protected override void Load(ContainerBuilder builder)
{
// IDeviceClientProvider
string edgeAgentConnectionString = $"{this.edgeDeviceConnectionString};{Constants.ModuleIdKey}={Constants.EdgeAgentModuleIdentityName}";
builder.Register(c => new ModuleClientProvider(edgeAgentConnectionString, this.upstreamProtocol))
builder.Register(c => new ModuleClientProvider(edgeAgentConnectionString, this.upstreamProtocol, this.productInfo))
.As<IModuleClientProvider>()
.SingleInstance();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public class EdgeletModule : Module
readonly Uri workloadUri;
readonly IEnumerable<AuthConfig> dockerAuthConfig;
readonly Option<UpstreamProtocol> upstreamProtocol;
readonly Option<string> productInfo;

public EdgeletModule(string iotHubHostname, string gatewayHostName, string deviceId, Uri managementUri,
Uri workloadUri, IEnumerable<AuthConfig> dockerAuthConfig, Option<UpstreamProtocol> upstreamProtocol)
Uri workloadUri, IEnumerable<AuthConfig> dockerAuthConfig, Option<UpstreamProtocol> upstreamProtocol, Option<string> productInfo)
{
this.iotHubHostName = Preconditions.CheckNonWhiteSpace(iotHubHostname, nameof(iotHubHostname));
this.gatewayHostName = Preconditions.CheckNonWhiteSpace(gatewayHostName, nameof(gatewayHostName));
Expand All @@ -42,12 +43,13 @@ public EdgeletModule(string iotHubHostname, string gatewayHostName, string devic
this.workloadUri = Preconditions.CheckNotNull(workloadUri, nameof(workloadUri));
this.dockerAuthConfig = Preconditions.CheckNotNull(dockerAuthConfig, nameof(dockerAuthConfig));
this.upstreamProtocol = Preconditions.CheckNotNull(upstreamProtocol, nameof(upstreamProtocol));
this.productInfo = productInfo;
}

protected override void Load(ContainerBuilder builder)
{
// IModuleClientProvider
builder.Register(c => new EnvironmentModuleClientProvider(this.upstreamProtocol))
builder.Register(c => new EnvironmentModuleClientProvider(this.upstreamProtocol, this.productInfo))
.As<IModuleClientProvider>()
.SingleInstance();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task EdgeAgentConnectionBasicTest()
await SetAgentDesiredProperties(registryManager, edgeDeviceId);

string edgeAgentConnectionString = $"HostName={iotHubConnectionStringBuilder.HostName};DeviceId={edgeDeviceId};ModuleId=$edgeAgent;SharedAccessKey={edgeDevice.Authentication.SymmetricKey.PrimaryKey}";
IModuleClientProvider moduleClientProvider = new ModuleClientProvider(edgeAgentConnectionString, Option.None<UpstreamProtocol>());
IModuleClientProvider moduleClientProvider = new ModuleClientProvider(edgeAgentConnectionString, Option.None<UpstreamProtocol>(), Option.None<string>());

var moduleDeserializerTypes = new Dictionary<string, Type>
{
Expand Down Expand Up @@ -317,7 +317,7 @@ public async Task EdgeAgentConnectionConfigurationTest()
await Task.Delay(TimeSpan.FromMinutes(7));

string edgeAgentConnectionString = $"HostName={iotHubConnectionStringBuilder.HostName};DeviceId={edgeDeviceId};ModuleId=$edgeAgent;SharedAccessKey={edgeDevice.Authentication.SymmetricKey.PrimaryKey}";
IModuleClientProvider moduleClientProvider = new ModuleClientProvider(edgeAgentConnectionString, Option.None<UpstreamProtocol>());
IModuleClientProvider moduleClientProvider = new ModuleClientProvider(edgeAgentConnectionString, Option.None<UpstreamProtocol>(), Option.None<string>());

var moduleDeserializerTypes = new Dictionary<string, Type>
{
Expand Down Expand Up @@ -1130,7 +1130,7 @@ public async Task EdgeAgentConnectionStatusTest()
await SetAgentDesiredProperties(registryManager, edgeDeviceId);

string edgeAgentConnectionString = $"HostName={iotHubConnectionStringBuilder.HostName};DeviceId={edgeDeviceId};ModuleId=$edgeAgent;SharedAccessKey={edgeDevice.Authentication.SymmetricKey.PrimaryKey}";
IModuleClientProvider moduleClientProvider = new ModuleClientProvider(edgeAgentConnectionString, Option.None<UpstreamProtocol>());
IModuleClientProvider moduleClientProvider = new ModuleClientProvider(edgeAgentConnectionString, Option.None<UpstreamProtocol>(), Option.None<string>());

var moduleDeserializerTypes = new Dictionary<string, Type>
{
Expand Down Expand Up @@ -1219,7 +1219,7 @@ public async Task EdgeAgentPingMethodTest()
await SetAgentDesiredProperties(registryManager, edgeDeviceId);

string edgeAgentConnectionString = $"HostName={iotHubConnectionStringBuilder.HostName};DeviceId={edgeDeviceId};ModuleId=$edgeAgent;SharedAccessKey={edgeDevice.Authentication.SymmetricKey.PrimaryKey}";
IModuleClientProvider moduleClientProvider = new ModuleClientProvider(edgeAgentConnectionString, Option.None<UpstreamProtocol>());
IModuleClientProvider moduleClientProvider = new ModuleClientProvider(edgeAgentConnectionString, Option.None<UpstreamProtocol>(), Option.None<string>());

var moduleDeserializerTypes = new Dictionary<string, Type>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static async Task<int> MainAsync(IConfigurationRoot configuration)
VersionInfo versionInfo = VersionInfo.Get(Constants.VersionInfoFileName);
if (versionInfo != VersionInfo.Empty)
{
logger.LogInformation($"Version - {versionInfo}");
logger.LogInformation($"Version - {versionInfo.ToString(true)}");
}
LogLogo(logger);

Expand Down
11 changes: 3 additions & 8 deletions edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Service/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ IContainer BuildContainer(IServiceCollection services)
eventListener.EnableEvents(CommonEventSource.Log, EventLevel.Informational);
});

string productInfo = VersionInfo.Get(Constants.VersionInfoFileName).ToString();;

// Register modules
builder.RegisterModule(
new CommonModule(
this.GetProductInfo(),
productInfo,
this.iotHubHostname,
this.edgeDeviceId));
builder.RegisterModule(
Expand Down Expand Up @@ -193,13 +195,6 @@ public void Configure(IApplicationBuilder app)
return (isEnabled, usePersistentStorage, storeAndForwardConfiguration, storagePath);
}

string GetProductInfo()
{
string name = "Microsoft.Azure.Devices.Edge.Hub";
string version = FileVersionInfo.GetVersionInfo(typeof(Startup).Assembly.Location).ProductVersion;
return $"{name}/{version}";
}

string GetStoragePath()
{
string baseStoragePath = this.Configuration.GetValue<string>("storageFolder");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static VersionInfo Get(string fileName)
[JsonProperty(PropertyName = "commit")]
public string Commit { get; }

public override string ToString()
public string ToString(bool includeCommitId)
{
if (string.IsNullOrWhiteSpace(this.Version))
{
Expand All @@ -59,14 +59,16 @@ public override string ToString()
sb.Append($".{this.Build}");
}

if (!string.IsNullOrWhiteSpace(this.Commit))
if (includeCommitId && !string.IsNullOrWhiteSpace(this.Commit))
{
sb.Append($" ({this.Commit})");
}

return sb.ToString();
}

public override string ToString() => this.ToString(false);

public override bool Equals(object obj) => this.Equals(obj as VersionInfo);

public bool Equals(VersionInfo other) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public void GetVersionInfoTest()
Assert.Equal("1.0", versionInfo.Version);
Assert.Equal("7238638", versionInfo.Build);
Assert.Equal("fc9f2dafaf93cac936ef756bb37efeaa58688914", versionInfo.Commit);
Assert.Equal("1.0.7238638 (fc9f2dafaf93cac936ef756bb37efeaa58688914)", versionInfo.ToString());
Assert.Equal("1.0.7238638 (fc9f2dafaf93cac936ef756bb37efeaa58688914)", versionInfo.ToString(true));
Assert.Equal("1.0.7238638", versionInfo.ToString());
}

[Fact]
Expand Down

0 comments on commit 1f7c27b

Please sign in to comment.