| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.ComponentModel; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Reflection; |
| | 8 | | using System.Runtime.Versioning; |
| | 9 | |
|
| | 10 | | namespace Azure.Messaging.EventHubs.Core |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// The set of information describing the active version of the |
| | 14 | | /// client library. |
| | 15 | | /// </summary> |
| | 16 | | /// |
| | 17 | | internal sealed class ClientLibraryInformation |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// The current set of information. |
| | 21 | | /// </summary> |
| | 22 | | /// |
| 32 | 23 | | public static ClientLibraryInformation Current { get; } = new ClientLibraryInformation(); |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// The name of the client library product. |
| | 27 | | /// </summary> |
| | 28 | | /// |
| 20 | 29 | | public string Product { get; } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// The version of the client library. |
| | 33 | | /// </summary> |
| | 34 | | /// |
| 20 | 35 | | public string Version { get; } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// The version of the framework on which the client library was |
| | 39 | | /// built. |
| | 40 | | /// </summary> |
| | 41 | | /// |
| 20 | 42 | | public string Framework { get; } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// The name of the platform on which the client library is currently running. |
| | 46 | | /// </summary> |
| | 47 | | /// |
| 20 | 48 | | public string Platform { get; } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// The client library information, formatted in the standard form used by SDK |
| | 52 | | /// user agents when interacting with Azure services. |
| | 53 | | /// </summary> |
| | 54 | | /// |
| | 55 | | [Description("user-agent")] |
| 10 | 56 | | public string UserAgent => $"azsdk-net-{ Product }/{ Version } ({ Framework }; { Platform })"; |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Prevents a default instance of the <see cref="ClientLibraryInformation"/> class from being created. |
| | 60 | | /// </summary> |
| | 61 | | /// |
| 2 | 62 | | private ClientLibraryInformation() |
| | 63 | | { |
| 2 | 64 | | Assembly assembly = typeof(ClientLibraryInformation).Assembly; |
| | 65 | |
|
| 2 | 66 | | Product = $"{ nameof(Messaging) }.{ nameof(EventHubs) }"; |
| 2 | 67 | | Version = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version; |
| 2 | 68 | | Framework = assembly.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName; |
| | 69 | |
|
| | 70 | | #if FullNetFx |
| | 71 | | Platform = Environment.OSVersion.VersionString; |
| | 72 | | #else |
| 2 | 73 | | Platform = System.Runtime.InteropServices.RuntimeInformation.OSDescription; |
| | 74 | | #endif |
| 2 | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Enumerates the client library properties, normalizing the property names. |
| | 79 | | /// </summary> |
| | 80 | | /// |
| | 81 | | /// <returns>An enumerable set of the properties, with name and value.</returns> |
| | 82 | | /// |
| | 83 | | public IEnumerable<KeyValuePair<string, string>> EnumerateProperties() => |
| 6 | 84 | | typeof(ClientLibraryInformation) |
| 6 | 85 | | .GetProperties(BindingFlags.Instance | BindingFlags.Public) |
| 36 | 86 | | .Select(property => new KeyValuePair<string, string>(GetTelemetryName(property), (string)property.GetVal |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Gets the name of the property, as it should appear in telemetry |
| | 90 | | /// information. |
| | 91 | | /// </summary> |
| | 92 | | /// |
| | 93 | | /// <param name="property">The property to consider.</param> |
| | 94 | | /// |
| | 95 | | /// <returns>The name of the property for use as telemetry for the client library.</returns> |
| | 96 | | /// |
| | 97 | | private static string GetTelemetryName(PropertyInfo property) |
| | 98 | | { |
| 30 | 99 | | string name = property.GetCustomAttribute<DescriptionAttribute>(false)?.Description; |
| 30 | 100 | | return ((string.IsNullOrEmpty(name)) ? property.Name : name).ToLowerInvariant(); |
| | 101 | | } |
| | 102 | | } |
| | 103 | | } |