Skip to content

Commit c205903

Browse files
committed
Update versioning logic & move TelemetryInstrumentationKey to its own Constants file (#4469)
* Update versioning logic & move TelemetryInstrumentationKey * Use attribute approach for setting TelemetryInstrumentationKey
1 parent 512cdd4 commit c205903

File tree

5 files changed

+87
-9
lines changed

5 files changed

+87
-9
lines changed

eng/build/Telemetry.props

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<!-- Default value; overridden in CI via /p:TelemetryInstrumentationKey -->
5+
<TelemetryInstrumentationKey>00000000-0000-0000-0000-000000000000</TelemetryInstrumentationKey>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<AssemblyAttribute Include="Azure.Functions.Cli.Common.TelemetryInstrumentationKeyAttribute">
10+
<_Parameter1>$(TelemetryInstrumentationKey)</_Parameter1>
11+
</AssemblyAttribute>
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using System.Reflection;
5+
6+
namespace Azure.Functions.Cli.Common
7+
{
8+
internal static partial class Constants
9+
{
10+
public static readonly string TelemetryInstrumentationKey =
11+
typeof(Constants).Assembly.GetCustomAttribute<TelemetryInstrumentationKeyAttribute>().Value
12+
?? "00000000-0000-0000-0000-000000000000";
13+
}
14+
15+
[AttributeUsage(AttributeTargets.Assembly)]
16+
internal class TelemetryInstrumentationKeyAttribute(string value) : Attribute
17+
{
18+
public string Value => value;
19+
}
20+
}

src/Cli/func/Common/Constants.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using System.Collections.ObjectModel;
5-
using System.Reflection;
5+
using Azure.Functions.Cli.Extensions;
66
using Azure.Functions.Cli.Helpers;
77

88
namespace Azure.Functions.Cli.Common
99
{
10-
internal static class Constants
10+
internal static partial class Constants
1111
{
1212
public const string StorageConnectionStringTemplate = "DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}";
1313
public const string FunctionsStorageAccountNamePrefix = "AzureFunctions";
@@ -46,7 +46,6 @@ internal static class Constants
4646
public const string FunctionsCoreToolsEnvironment = "FUNCTIONS_CORETOOLS_ENVIRONMENT";
4747
public const string EnablePersistenceChannelDebugSetting = "FUNCTIONS_CORE_TOOLS_ENABLE_PERSISTENCE_CHANNEL_DEBUG_OUTPUT";
4848
public const string TelemetryOptOutVariable = "FUNCTIONS_CORE_TOOLS_TELEMETRY_OPTOUT";
49-
public const string TelemetryInstrumentationKey = "00000000-0000-0000-0000-000000000000";
5049
public const string ScmRunFromPackage = "SCM_RUN_FROM_PACKAGE";
5150
public const string ScmDoBuildDuringDeployment = "SCM_DO_BUILD_DURING_DEPLOYMENT";
5251
public const string WebsiteRunFromPackage = "WEBSITE_RUN_FROM_PACKAGE";
@@ -120,12 +119,11 @@ internal static class Constants
120119
"entityTrigger",
121120
];
122121

123-
#pragma warning disable SA1401 // Fields should be private
124-
public static string CliDetailedVersion = typeof(Constants).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? string.Empty;
125-
public static string CliUserAgent = $"functions-core-tools/{CliVersion}";
126-
#pragma warning restore SA1401 // Fields should be private
122+
public static string CliVersion => typeof(Constants).Assembly.GetCliVersion();
127123

128-
public static string CliVersion => typeof(Constants).GetTypeInfo().Assembly.GetName().Version.ToString(3);
124+
public static string CliDetailedVersion => typeof(Constants).Assembly.GetInformationalVersion();
125+
126+
public static string CliUserAgent => $"functions-core-tools/{CliVersion}";
129127

130128
public static ExtensionPackage ExtensionsMetadataGeneratorPackage => new ExtensionPackage { Name = "Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" };
131129

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using System.Reflection;
5+
6+
namespace Azure.Functions.Cli.Extensions
7+
{
8+
internal static class AssemblyExtensions
9+
{
10+
public static string GetInformationalVersion(this Assembly assembly)
11+
{
12+
var informationalVersion = assembly
13+
.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false)
14+
.OfType<AssemblyInformationalVersionAttribute>()
15+
.FirstOrDefault()?.InformationalVersion;
16+
17+
return informationalVersion ?? string.Empty;
18+
}
19+
20+
public static string GetCliVersion(this Assembly assembly)
21+
{
22+
// The package version is stamped into the assembly's AssemblyInformationalVersionAttribute at build time, followed by a '+'
23+
// and the commit hash, e.g.: "4.0.10000-dev+67bd99a8ce2ec3cf833f25c039f60222caf44573 (64-bit)"
24+
var version = assembly.GetInformationalVersion();
25+
26+
if (version is not null)
27+
{
28+
var plusIndex = version.IndexOf('+');
29+
30+
if (plusIndex > 0)
31+
{
32+
return version[..plusIndex];
33+
}
34+
35+
return version;
36+
}
37+
38+
// Fallback to the file version, which is based on the CI build number, and then fallback to the assembly version, which is
39+
// product stable version, e.g. 4.0.0.0
40+
version = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version
41+
?? assembly.GetCustomAttribute<AssemblyVersionAttribute>()?.Version;
42+
43+
return version;
44+
}
45+
}
46+
}

src/Cli/func/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Azure.Functions.Cli
1111
{
1212
internal class Program
1313
{
14-
private static readonly string[] _versionArgs = new[] { "version", "v" };
14+
private static readonly string[] _versionArgs = ["version", "v"];
1515
private static IContainer _container;
1616

1717
internal static void Main(string[] args)

0 commit comments

Comments
 (0)