-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: nic <[email protected]>
- Loading branch information
Showing
6 changed files
with
212 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Agoda.Builds.Metrics | ||
{ | ||
public class BuildTimeData | ||
{ | ||
[JsonPropertyName("id")] | ||
public Guid Id { get; set; } | ||
|
||
[JsonPropertyName("metricsVersion")] | ||
public string MetricsVersion { get; set; } | ||
|
||
[JsonPropertyName("userName")] | ||
public string UserName { get; set; } | ||
|
||
[JsonPropertyName("cpuCount")] | ||
public int CpuCount { get; set; } | ||
|
||
[JsonPropertyName("hostname")] | ||
public string Hostname { get; set; } | ||
|
||
[JsonPropertyName("platform")] | ||
public PlatformID Platform { get; set; } | ||
|
||
[JsonPropertyName("os")] | ||
public string Os { get; set; } | ||
|
||
[JsonPropertyName("timeTaken")] | ||
public string TimeTaken { get; set; } | ||
|
||
[JsonPropertyName("branch")] | ||
public string Branch { get; set; } | ||
|
||
[JsonPropertyName("type")] | ||
public string Type { get; set; } | ||
|
||
[JsonPropertyName("projectName")] | ||
public string ProjectName { get; set; } | ||
|
||
[JsonPropertyName("repository")] | ||
public string Repository { get; set; } | ||
|
||
[JsonPropertyName("repositoryName")] | ||
public string RepositoryName { get; set; } | ||
|
||
[JsonPropertyName("date")] | ||
public DateTime Date { get; set; } | ||
|
||
public BuildTimeData( | ||
string metricsVersion, | ||
string type, | ||
string projectName, | ||
string timeTaken, | ||
GitContext gitContext | ||
) | ||
{ | ||
Id = Guid.NewGuid(); | ||
Type = type; | ||
MetricsVersion = metricsVersion; | ||
UserName = Environment.UserName; | ||
CpuCount = Environment.ProcessorCount; | ||
Hostname = Environment.MachineName; | ||
Platform = Environment.OSVersion.Platform; | ||
Os = Environment.OSVersion.VersionString; | ||
ProjectName = projectName; | ||
TimeTaken = timeTaken; | ||
Repository = gitContext.RepositoryUrl; | ||
RepositoryName = gitContext.RepositoryName; | ||
Branch = gitContext.BranchName; | ||
Date = DateTime.UtcNow; | ||
} | ||
} | ||
} |
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,33 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace Agoda.Builds.Metrics | ||
{ | ||
internal static class BuildTimePublisher | ||
{ | ||
public static void Publish(string apiEndpoint, BuildTimeData result) | ||
{ | ||
using (var httpClient = new HttpClient()) | ||
{ | ||
httpClient.Timeout = TimeSpan.FromSeconds(10); | ||
|
||
var content = new StringContent(JsonSerializer.Serialize(result), Encoding.UTF8, "application/json"); | ||
var response = httpClient.PostAsync(GetApiEndpoint(apiEndpoint), content).Result; | ||
|
||
response.EnsureSuccessStatusCode(); | ||
} | ||
} | ||
|
||
static string GetApiEndpoint(string apiEndpoint) | ||
{ | ||
if (string.IsNullOrEmpty(apiEndpoint)) | ||
{ | ||
apiEndpoint = Environment.GetEnvironmentVariable("BUILD_METRICS_ES_ENDPOINT"); | ||
} | ||
|
||
return string.IsNullOrEmpty(apiEndpoint) ? "http://compilation-metrics/dotnet" : apiEndpoint; | ||
} | ||
} | ||
} |
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,9 @@ | ||
namespace Agoda.Builds.Metrics | ||
{ | ||
public class GitContext | ||
{ | ||
public string RepositoryUrl { get; set; } | ||
public string RepositoryName { get; set; } | ||
public string BranchName { get; set; } | ||
} | ||
} |
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,52 @@ | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace Agoda.Builds.Metrics | ||
{ | ||
public static class GitContextReader | ||
{ | ||
public static GitContext GetGitContext() | ||
{ | ||
string url = RunCommand("config --get remote.origin.url"); | ||
string branch = RunCommand("rev-parse --abbrev-ref HEAD"); | ||
|
||
return new GitContext | ||
{ | ||
RepositoryUrl = url, | ||
RepositoryName = GetRepositoryNameFromUrl(url), | ||
BranchName = branch | ||
}; | ||
} | ||
|
||
static string RunCommand(string args) | ||
{ | ||
var process = new Process | ||
{ | ||
StartInfo = new ProcessStartInfo | ||
( | ||
fileName: Environment.OSVersion.Platform == PlatformID.Win32NT ? "git.exe" : "git" | ||
) | ||
{ | ||
UseShellExecute = false, | ||
WorkingDirectory = Environment.CurrentDirectory, | ||
RedirectStandardInput = true, | ||
RedirectStandardOutput = true, | ||
Arguments = args | ||
} | ||
}; | ||
|
||
process.Start(); | ||
|
||
return process.StandardOutput.ReadLine(); | ||
} | ||
|
||
static string GetRepositoryNameFromUrl(string url) | ||
{ | ||
var repositoryName = url.Substring(url.LastIndexOf('/') + 1); | ||
|
||
return repositoryName.EndsWith(".git") | ||
? repositoryName.Substring(0, repositoryName.LastIndexOf('.')) | ||
: repositoryName; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,101 +1,62 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Text; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
using System; | ||
|
||
using System.Text.Json; | ||
namespace Agoda.Builds.Metrics | ||
{ | ||
public class MeasureBuildTime : Task | ||
{ | ||
/// <summary> | ||
/// Set by the 'CaptureBuildTime' build event. | ||
/// </summary> | ||
public string ProjectName { get; set; } | ||
|
||
/// <summary> | ||
/// Set by the 'CaptureBuildTime' build event. | ||
/// </summary> | ||
public string StartDateTime { get; set; } | ||
|
||
/// <summary> | ||
/// Set by the 'CaptureBuildTime' build event. | ||
/// </summary> | ||
public string EndDateTime { get; set; } | ||
|
||
/// <summary> | ||
/// Seems not to be set from anywhere. | ||
/// </summary> | ||
public string ApiEndPoint { get; set; } | ||
public string ProjectName { get; set; } | ||
|
||
/// <summary> | ||
/// Set by the Task.Execute method. | ||
/// Used by the 'CaptureBuildTime' build event. | ||
/// </summary> | ||
[Output] | ||
public string DebugOutput { get; set; } | ||
public string BuildTimeMilliseconds { get; set; } | ||
|
||
public override bool Execute() | ||
{ | ||
DebugOutput = (DateTime.Parse(EndDateTime) - DateTime.Parse(StartDateTime)).TotalMilliseconds.ToString(); | ||
BuildTimeMilliseconds = DateTime.Parse(EndDateTime).Subtract(DateTime.Parse(StartDateTime)).TotalMilliseconds.ToString(); | ||
|
||
try | ||
{ | ||
var gitUrl = GetGitDetails("config --get remote.origin.url"); | ||
var data = new | ||
{ | ||
id = Guid.NewGuid(), | ||
metricsVersion = typeof(MeasureBuildTime).Assembly.GetName().Version.ToString(), | ||
userName = Environment.UserName, | ||
cpuCount = Environment.ProcessorCount, | ||
hostname = Environment.MachineName, | ||
platform = Environment.OSVersion.Platform, | ||
os = Environment.OSVersion.VersionString, | ||
timeTaken = DebugOutput, | ||
branch = GetGitDetails("rev-parse --abbrev-ref HEAD"), | ||
type = ".Net", | ||
projectName = ProjectName, | ||
repository = gitUrl, | ||
repositoryName = extractRepositoryName(gitUrl), | ||
date = DateTime.UtcNow | ||
}; | ||
var gitContext = GitContextReader.GetGitContext(); | ||
|
||
using (var httpClient = new HttpClient()) | ||
{ | ||
httpClient.Timeout = TimeSpan.FromMinutes(1); | ||
PopulateBuildMetricESDetails(); | ||
var content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json"); | ||
var responses = httpClient.PostAsync(ApiEndPoint, content).Result; | ||
if (!responses.IsSuccessStatusCode) | ||
{ | ||
Log.LogMessage($"Unable to publish metrics - {responses.ReasonPhrase}"); | ||
} | ||
} | ||
var result = new BuildTimeData( | ||
metricsVersion: typeof(MeasureBuildTime).Assembly.GetName().Version.ToString(), | ||
type: ".Net", | ||
projectName: ProjectName, | ||
timeTaken: BuildTimeMilliseconds, | ||
gitContext: gitContext | ||
); | ||
|
||
BuildTimePublisher.Publish(ApiEndPoint, result); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.LogMessage($"Unexpected issue while generating metrics - {ex.Message}"); | ||
Log.LogMessage("An error occured while capturing the build time: " + ex); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static string extractRepositoryName(string gitUrl) | ||
{ | ||
var repositoryName = gitUrl.Substring(gitUrl.LastIndexOf('/') + 1); | ||
return repositoryName.EndsWith(".git") ? repositoryName.Substring(0, repositoryName.LastIndexOf('.')) : repositoryName; | ||
} | ||
|
||
private void PopulateBuildMetricESDetails() | ||
{ | ||
if (string.IsNullOrEmpty(ApiEndPoint)) | ||
{ | ||
ApiEndPoint = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("BUILD_METRICS_ES_ENDPOINT")) ? Environment.GetEnvironmentVariable("BUILD_METRICS_ES_ENDPOINT") : "http://compilation-metrics/dotnet"; | ||
} | ||
} | ||
|
||
private static string GetGitDetails(string arg) | ||
{ | ||
string executableName = "git"; | ||
if (Environment.OSVersion.Platform == PlatformID.Win32NT) | ||
executableName += ".exe"; | ||
ProcessStartInfo startInfo = new ProcessStartInfo(executableName); | ||
|
||
startInfo.UseShellExecute = false; | ||
startInfo.WorkingDirectory = Environment.CurrentDirectory; | ||
startInfo.RedirectStandardInput = true; | ||
startInfo.RedirectStandardOutput = true; | ||
startInfo.Arguments = arg; | ||
|
||
Process process = new Process(); | ||
process.StartInfo = startInfo; | ||
process.Start(); | ||
|
||
var gitBranch = process.StandardOutput.ReadLine(); | ||
return gitBranch; | ||
} | ||
} | ||
} |
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