-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from siemens/feat/CAToolTelemetry_Application…
…Insight CA Tool telemetry
- Loading branch information
Showing
56 changed files
with
614 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// SPDX-FileCopyrightText: 2025 Siemens AG | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace LCT.Common.Constants | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class TelemetryConstant | ||
{ | ||
public const string ToolName = "CATool"; | ||
public const string PackageIdentifier = "PackageIdentifierExecution"; | ||
public const string PackageCreator = "PackageCreatorExecution"; | ||
public const string ArtifactoryUploader = "ArtifactoryUploaderExecution"; | ||
public const string IdentifierKpiData = "IdentifierKpiDataTelemetry"; | ||
public const string CreatorKpiData = "CreatorKpiDataTelemetry"; | ||
public const string ArtifactoryUploaderKpiData = "UploaderKpiDataTelemetry"; | ||
public const string Type = "ApplicationInsights"; | ||
public const string StartLogMessage = "Telemetry tracking is now active for this execution. To turn off telemetry, use the command-line option --Telemetry:Enable false or adjust the settings in your appsettings file."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// SPDX-FileCopyrightText: 2025 Siemens AG | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
using LCT.Common.Constants; | ||
using LCT.Common.Interface; | ||
using LCT.Telemetry; | ||
using log4net; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace LCT.Common | ||
{ | ||
public class TelemetryHelper | ||
{ | ||
private readonly ILog Logger; | ||
LCT.Telemetry.Telemetry telemetry_; | ||
EnvironmentHelper environmentHelper; | ||
CommonAppSettings appSettings_; | ||
|
||
public TelemetryHelper(CommonAppSettings appSettings) | ||
{ | ||
environmentHelper = new EnvironmentHelper(); | ||
Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
appSettings_ = appSettings ?? new CommonAppSettings(); | ||
|
||
telemetry_ = new LCT.Telemetry.Telemetry(TelemetryConstant.Type, new Dictionary<string, string> | ||
{ | ||
{ "InstrumentationKey", appSettings.Telemetry.ApplicationInsightInstrumentKey } | ||
}); | ||
} | ||
|
||
public void StartTelemetry<T>(string catoolVersion, T kpiData,string telemetryFor) | ||
{ | ||
// Initialize telemetry with CATool version and instrumentation key only if Telemetry is enabled in appsettings | ||
Logger.Warn(TelemetryConstant.StartLogMessage); | ||
try | ||
{ | ||
InitializeAndTrackEvent(TelemetryConstant.ToolName, catoolVersion, telemetryFor | ||
, appSettings_); | ||
TrackKpiDataTelemetry(telemetryFor, kpiData); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.Error($"An error occurred: {ex.Message}"); | ||
TrackException(ex); | ||
environmentHelper.CallEnvironmentExit(-1); | ||
} | ||
finally | ||
{ | ||
telemetry_.Flush(); // Ensure telemetry is sent before application exits | ||
} | ||
} | ||
|
||
private void InitializeAndTrackEvent(string toolName, string toolVersion, string eventName, | ||
CommonAppSettings appSettings) | ||
{ | ||
telemetry_.Initialize(toolName, toolVersion); | ||
|
||
telemetry_.TrackCustomEvent(eventName, new Dictionary<string, string> | ||
{ | ||
{ "CA Tool Version", toolVersion }, | ||
{ "SW360 Project Name", appSettings.SW360.ProjectName }, | ||
{ "SW360 Project ID", appSettings.SW360.ProjectID }, | ||
{ "Project Type", appSettings.ProjectType }, | ||
{ "Hashed User ID", HashUtility.GetHashString(Environment.UserName) }, | ||
{ "Start Time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) } | ||
}); | ||
} | ||
private void TrackKpiDataTelemetry<T>(string eventName, T kpiData) | ||
{ | ||
var properties = typeof(T).GetProperties(); | ||
var telemetryData = properties.ToDictionary( | ||
prop => prop.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName ?? prop.Name, | ||
prop => prop.GetValue(kpiData)?.ToString() | ||
); | ||
|
||
telemetryData["Hashed User ID"] = HashUtility.GetHashString(Environment.UserName); | ||
telemetryData["Time stamp"] = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); | ||
|
||
telemetry_.TrackCustomEvent(eventName, telemetryData); | ||
} | ||
|
||
private void TrackException(Exception ex) | ||
{ | ||
var exceptionData = new Dictionary<string, string> | ||
{ | ||
{ "Error Time", DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) }, | ||
{ "Stack Trace", ex.StackTrace } | ||
}; | ||
|
||
telemetry_.TrackException(ex, exceptionData); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// SPDX-FileCopyrightText: 2025 Siemens AG | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
using Microsoft.ApplicationInsights; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
|
||
namespace LCT.Telemetry | ||
{ | ||
public class ApplicationInsightsTelemetryProvider : ITelemetryProvider | ||
{ | ||
private readonly TelemetryClient _telemetryClient; | ||
|
||
public ApplicationInsightsTelemetryProvider(Dictionary<string, string> configuration) | ||
{ | ||
var instrumentationKey = configuration.GetValueOrDefault("InstrumentationKey") | ||
?? Environment.GetEnvironmentVariable("TelemetryInstrumentationKey"); | ||
|
||
if (string.IsNullOrEmpty(instrumentationKey)) | ||
{ | ||
throw new InvalidOperationException("Application Insights Instrumentation Key is missing or invalid."); | ||
} | ||
|
||
var aiConfig = TelemetryConfiguration.CreateDefault(); | ||
aiConfig.InstrumentationKey = instrumentationKey; | ||
|
||
_telemetryClient = new TelemetryClient(aiConfig); | ||
} | ||
|
||
public void TrackEvent(string eventName, Dictionary<string, string> properties = null) | ||
{ | ||
_telemetryClient.TrackEvent(eventName, properties); | ||
} | ||
|
||
public void TrackException(Exception ex, Dictionary<string, string> properties = null) | ||
{ | ||
var exceptionTelemetry = new ExceptionTelemetry(ex); | ||
|
||
if (properties != null) | ||
{ | ||
foreach (var property in properties) | ||
{ | ||
exceptionTelemetry.Properties[property.Key] = property.Value; | ||
} | ||
} | ||
|
||
_telemetryClient.TrackException(exceptionTelemetry); | ||
} | ||
|
||
public void Flush() | ||
{ | ||
_telemetryClient.Flush(); | ||
Thread.Sleep(1000); // Allow some time for telemetry to be sent | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// SPDX-FileCopyrightText: 2025 Siemens AG | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace LCT.Telemetry | ||
{ | ||
public static class HashUtility | ||
{ | ||
public static string GetHashString(string input) | ||
{ | ||
if (string.IsNullOrEmpty(input)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
using (var hashAlgorithm = SHA256.Create()) | ||
{ | ||
byte[] hashBytes = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(input)); | ||
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// SPDX-FileCopyrightText: 2025 Siemens AG | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
namespace LCT.Telemetry | ||
{ | ||
public interface ITelemetryProvider | ||
{ | ||
void TrackEvent(string eventName, Dictionary<string, string> properties = null); | ||
void TrackException(Exception ex, Dictionary<string, string> properties = null); | ||
void Flush(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Version>7.0.2</Version> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> | ||
<OutputPath>..\..\out</OutputPath> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> | ||
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.