-
Notifications
You must be signed in to change notification settings - Fork 0
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 #30 from RaaLabs/healthchecks
Healthchecks
- Loading branch information
Showing
12 changed files
with
87 additions
and
40 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,46 @@ | ||
using System; | ||
using System.Globalization; | ||
using RaaLabs.Edge.Connectors.OPCUA.Events; | ||
using RaaLabs.Edge.Modules.Diagnostics.Health; | ||
using RaaLabs.Edge.Modules.EventHandling; | ||
using Serilog; | ||
|
||
namespace RaaLabs.Edge.Connectors.OPCUA; | ||
|
||
public class HealthCheck : IConsumeEvent<OpcuaDatapointOutput>, IExposeHealthStatus | ||
{ | ||
readonly TimeSpan _maxTimeBetweenDatapoints; | ||
readonly TimeProvider _clock; | ||
readonly ILogger _logger; | ||
DateTimeOffset _lastTimestampReceived; | ||
|
||
public HealthCheck(TimeProvider clock, ILogger logger) | ||
{ | ||
_maxTimeBetweenDatapoints = TimeSpan.FromSeconds(double.Parse(Environment.GetEnvironmentVariable("HEALTHCHECK_MAX_SECONDS_BETWEEN_DATAPOINTS") ?? "120", CultureInfo.CurrentCulture)); | ||
_clock = clock; | ||
_logger = logger; | ||
_lastTimestampReceived = _clock.GetUtcNow(); | ||
} | ||
|
||
public void Handle(OpcuaDatapointOutput @event) | ||
{ | ||
_lastTimestampReceived = _clock.GetUtcNow(); | ||
_logger.Debug("Sent data, updating last timestamp received in healthcheck with {LastTimestampReceived}", _lastTimestampReceived); | ||
} | ||
|
||
public bool IsReady => true; | ||
public bool IsHealthy | ||
{ | ||
get | ||
{ | ||
var timeSinceLastDatapoint = _clock.GetUtcNow() - _lastTimestampReceived; | ||
if (timeSinceLastDatapoint > _maxTimeBetweenDatapoints) | ||
{ | ||
_logger.Warning("Time since data was sent is {Since}, returning unhealthy", timeSinceLastDatapoint); | ||
return false; | ||
} | ||
_logger.Debug("Time since data was sent is {Since}, returning healthy", timeSinceLastDatapoint); | ||
return true; | ||
} | ||
} | ||
} |
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