-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Diagnostics: Add CPU history (#1602)
* Diagnostics: Add CPU history to all diagnostics
- Loading branch information
Showing
13 changed files
with
568 additions
and
357 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
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
29 changes: 29 additions & 0 deletions
29
Microsoft.Azure.Cosmos/src/Diagnostics/CosmosSystemInfo.cs
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,29 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
namespace Microsoft.Azure.Cosmos.Diagnostics | ||
{ | ||
using System; | ||
using Microsoft.Azure.Documents.Rntbd; | ||
|
||
internal sealed class CosmosSystemInfo : CosmosDiagnosticsInternal | ||
{ | ||
public readonly CpuLoadHistory CpuLoadHistory; | ||
|
||
public CosmosSystemInfo( | ||
CpuLoadHistory cpuLoadHistory) | ||
{ | ||
this.CpuLoadHistory = cpuLoadHistory ?? throw new ArgumentNullException(nameof(cpuLoadHistory)); | ||
} | ||
|
||
public override void Accept(CosmosDiagnosticsInternalVisitor visitor) | ||
{ | ||
visitor.Visit(this); | ||
} | ||
|
||
public override TResult Accept<TResult>(CosmosDiagnosticsInternalVisitor<TResult> visitor) | ||
{ | ||
return visitor.Visit(this); | ||
} | ||
} | ||
} |
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,78 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Handlers | ||
{ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Cosmos.Diagnostics; | ||
using Microsoft.Azure.Documents.Rntbd; | ||
|
||
/// <summary> | ||
/// Handler which add process level info like CPU usage to the | ||
/// diagnostics. This is a best effort scenario. It will not | ||
/// add or attempt to add it if an exception occurs to avoid | ||
/// impacting users. | ||
/// </summary> | ||
internal class DiagnosticsHandler : RequestHandler | ||
{ | ||
public override Task<ResponseMessage> SendAsync( | ||
RequestMessage request, | ||
CancellationToken cancellationToken) | ||
{ | ||
DiagnosticsHandlerHelper.Instance.RecordCpuDiagnostics(request); | ||
return base.SendAsync(request, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// This is a helper class that creates a single static instance to avoid each | ||
/// client instance from creating a new CPU monitor. | ||
/// </summary> | ||
private class DiagnosticsHandlerHelper | ||
{ | ||
public static readonly DiagnosticsHandlerHelper Instance = new DiagnosticsHandlerHelper(); | ||
private readonly CpuMonitor cpuMonitor = null; | ||
private bool isCpuMonitorEnabled = false; | ||
|
||
private DiagnosticsHandlerHelper() | ||
{ | ||
// If the CPU monitor fails for some reason don't block the application | ||
try | ||
{ | ||
this.cpuMonitor = new CpuMonitor(); | ||
this.cpuMonitor.Start(); | ||
this.isCpuMonitorEnabled = true; | ||
} | ||
catch (Exception) | ||
{ | ||
this.isCpuMonitorEnabled = false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The diagnostics should never block a request, and is a best attempt | ||
/// If the CPU load history fails then don't try it in the future. | ||
/// </summary> | ||
public void RecordCpuDiagnostics(RequestMessage request) | ||
{ | ||
if (this.isCpuMonitorEnabled) | ||
{ | ||
try | ||
{ | ||
CpuLoadHistory cpuHistory = this.cpuMonitor.GetCpuLoad(); | ||
if (cpuHistory != null) | ||
{ | ||
request.DiagnosticsContext.AddDiagnosticsInternal(new CosmosSystemInfo(cpuHistory)); | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
this.isCpuMonitorEnabled = false; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.