Skip to content

Commit

Permalink
Adding some error telemetry (#3624)
Browse files Browse the repository at this point in the history
* WIP

* Removing the thrown exception

* Assigning default value on the property
  • Loading branch information
dhoehna authored Aug 23, 2024
1 parent a0f0407 commit a9f6a73
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Licensed under the MIT License.

using System;
using System.Configuration.Provider;
using System.Diagnostics.Tracing;
using System.Security.Cryptography.X509Certificates;
using DevHome.Common.TelemetryEvents.DeveloperId;
using DevHome.Telemetry;
using Microsoft.Diagnostics.Telemetry;
Expand All @@ -24,6 +26,10 @@ public class GetReposEvent : EventBase

public int NumberOfReposFound { get; }

public int HResult { get; }

public string ExceptionMessage { get; } = string.Empty;

/// <summary>
/// Initializes a new instance of the <see cref="GetReposEvent"/> class.
/// </summary>
Expand Down Expand Up @@ -52,6 +58,15 @@ public GetReposEvent(string stageName, string providerName, IDeveloperId develop
NumberOfReposFound = reposFound;
}

public GetReposEvent(Exception exception, string providerName, IDeveloperId developerId)
{
StageName = "Error";
ProviderName = providerName;
DeveloperId = developerId is null ? string.Empty : DeveloperIdHelper.GetHashedDeveloperId(providerName, developerId);
ExceptionMessage = exception.Message;
HResult = exception.HResult;
}

public override void ReplaceSensitiveStrings(Func<string, string> replaceSensitiveStrings)
{
// The only sensitive string is the developerID. GetHashedDeveloperId is used to hash the developerId.
Expand Down
12 changes: 11 additions & 1 deletion tools/SetupFlow/DevHome.SetupFlow/Models/RepositoryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,13 @@ public RepositorySearchInformation SearchForRepositories(IDeveloperId developerI
else
{
_log.Error(result.Result.ExtendedError, $"Could not get repositories. Message: {result.Result.DisplayMessage}");
TelemetryFactory.Get<ITelemetry>().Log("RepoTool_SearchForRepos_Event", LogLevel.Critical, new GetReposEvent(result.Result.ExtendedError, _repositoryProvider.DisplayName, developerId));
}
}
else
{
TelemetryFactory.Get<ITelemetry>().Log("RepoTool_SearchForRepos_Event", LogLevel.Critical, new GetReposEvent("UsingIRepositoryProvider", _repositoryProvider.DisplayName, developerId));

// Fallback in case this is called with IRepositoryProvider.
RepositoriesResult result = _repositoryProvider.GetRepositoriesAsync(developerId).AsTask().Result;
if (result.Result.Status == ProviderOperationStatus.Success)
Expand All @@ -258,6 +261,7 @@ public RepositorySearchInformation SearchForRepositories(IDeveloperId developerI
else
{
_log.Error(result.Result.ExtendedError, $"Could not get repositories. Message: {result.Result.DisplayMessage}");
TelemetryFactory.Get<ITelemetry>().Log("RepoTool_SearchForRepos_Event", LogLevel.Critical, new GetReposEvent(result.Result.ExtendedError, _repositoryProvider.DisplayName, developerId));
}
}
}
Expand All @@ -271,11 +275,13 @@ public RepositorySearchInformation SearchForRepositories(IDeveloperId developerI
else
{
_log.Information(aggregateException.ToString());
TelemetryFactory.Get<ITelemetry>().Log("RepoTool_SearchForRepos_Event", LogLevel.Critical, new GetReposEvent(aggregateException, _repositoryProvider.DisplayName, developerId));
}
}
catch (Exception ex)
{
_log.Error(ex, $"Could not get repositories. Message: {ex}");
TelemetryFactory.Get<ITelemetry>().Log("RepoTool_SearchForRepos_Event", LogLevel.Critical, new GetReposEvent(ex, _repositoryProvider.DisplayName, developerId));
}

_repositories[developerId] = repoSearchInformation.Repositories;
Expand All @@ -294,27 +300,31 @@ public RepositorySearchInformation GetAllRepositories(IDeveloperId developerId)
if (result.Result.Status == ProviderOperationStatus.Success)
{
repoSearchInformation.Repositories = result.Repositories;
throw new ArgumentNullException(nameof(developerId));
}
else
{
_log.Error(result.Result.ExtendedError, $"Could not get repositories. Message: {result.Result.DisplayMessage}");
TelemetryFactory.Get<ITelemetry>().Log("RepoTool_GetAllRepos_Event", LogLevel.Critical, new GetReposEvent(result.Result.ExtendedError, _repositoryProvider.DisplayName, developerId));
}
}
catch (AggregateException aggregateException)
{
// Because tasks can be canceled DevHome should emit different logs.
if (aggregateException.InnerException is OperationCanceledException)
{
_log.Information($"Get Repos operation was cancalled.");
_log.Information($"Get Repos operation was cancelled.");
}
else
{
_log.Error(aggregateException, aggregateException.Message);
TelemetryFactory.Get<ITelemetry>().Log("RepoTool_GetAllRepos_Event", LogLevel.Critical, new GetReposEvent(aggregateException, _repositoryProvider.DisplayName, developerId));
}
}
catch (Exception ex)
{
_log.Error(ex, $"Could not get repositories. Message: {ex}");
TelemetryFactory.Get<ITelemetry>().Log("RepoTool_GetAllRepos_Event", LogLevel.Critical, new GetReposEvent(ex, _repositoryProvider.DisplayName, developerId));
}

_repositories[developerId] = repoSearchInformation.Repositories;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ private Task<RepositorySearchInformation> StartSearchingForRepos(string reposito
TelemetryFactory.Get<ITelemetry>().Log("RepoTool_GetRepos_Event", LogLevel.Critical, new RepoToolEvent("GettingAllLoggedInAccounts"), _activityId);
var loggedInDeveloper = _providers.GetAllLoggedInAccounts(repositoryProvider).FirstOrDefault(x => x.LoginId == loginId);
TelemetryFactory.Get<ITelemetry>().Log("RepoTool_GetRepos_Event", LogLevel.Critical, new RepoToolEvent("GettingAllRepos"), _activityId);
TelemetryFactory.Get<ITelemetry>().Log("RepoTool_GetRepos_Event", LogLevel.Critical, new RepoToolEvent("SearchingAllRepos"), _activityId);
return _providers.SearchForRepos(repositoryProvider, loggedInDeveloper, _repoSearchInputs);
});
}
Expand Down

0 comments on commit a9f6a73

Please sign in to comment.