Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for logjson #224

Merged
merged 16 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 43 additions & 42 deletions Consul/Agent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// <copyright file="Agent.cs" company="PlayFab Inc">
// Copyright 2015 PlayFab Inc.
// Copyright 2020 G-Research Limited
Expand Down Expand Up @@ -45,10 +45,7 @@ public class TTLStatus : IEquatable<TTLStatus>
public static TTLStatus Critical { get; } = new TTLStatus() { Status = "critical", LegacyStatus = "fail" };

[Obsolete("Use TTLStatus.Critical instead. This status will be an error in 0.7.0+", true)]
public static TTLStatus Fail
{
get { return Critical; }
}
public static TTLStatus Fail => Critical;

public bool Equals(TTLStatus other)
{
Expand Down Expand Up @@ -443,7 +440,7 @@ internal Agent(ConsulClient c)
/// Self is used to query the agent we are speaking to for information about itself
/// </summary>
/// <returns>A somewhat dynamic object representing the various data elements in Self</returns>
public Task<QueryResult<Dictionary<string, Dictionary<string, dynamic>>>> Self(CancellationToken ct = default(CancellationToken))
public Task<QueryResult<Dictionary<string, Dictionary<string, dynamic>>>> Self(CancellationToken ct = default)
{
return _client.Get<Dictionary<string, Dictionary<string, dynamic>>>("/v1/agent/self").Execute(ct);
}
Expand All @@ -452,18 +449,12 @@ internal Agent(ConsulClient c)
/// NodeName is used to get the node name of the agent
/// </summary>
[Obsolete("This property will be removed in a future version. Replace uses of it with a call to 'await GetNodeName()'")]
public string NodeName
{
get
{
return GetNodeName().ConfigureAwait(false).GetAwaiter().GetResult();
}
}
public string NodeName => GetNodeName().ConfigureAwait(false).GetAwaiter().GetResult();

/// <summary>
/// GetNodeName is used to get the node name of the agent. The value is cached per instance of ConsulClient after the first use.
/// </summary>
public async Task<string> GetNodeName(CancellationToken ct = default(CancellationToken))
public async Task<string> GetNodeName(CancellationToken ct = default)
{
if (_nodeName == null)
{
Expand All @@ -483,7 +474,7 @@ public string NodeName
/// Checks returns the locally registered checks
/// </summary>
/// <returns>A map of the registered check names and check data</returns>
public Task<QueryResult<Dictionary<string, AgentCheck>>> Checks(CancellationToken ct = default(CancellationToken))
public Task<QueryResult<Dictionary<string, AgentCheck>>> Checks(CancellationToken ct = default)
{
return Checks(null, ct);
}
Expand All @@ -493,7 +484,7 @@ public string NodeName
/// </summary>
/// <param name="filter">Specifies the expression used to filter the queries results prior to returning the data</param>
/// <returns>A map of the registered check names and check data</returns>
public Task<QueryResult<Dictionary<string, AgentCheck>>> Checks(Filter filter, CancellationToken ct = default(CancellationToken))
public Task<QueryResult<Dictionary<string, AgentCheck>>> Checks(Filter filter, CancellationToken ct = default)
{
return _client.Get<Dictionary<string, AgentCheck>>("/v1/agent/checks", filter: filter).Execute(ct);
}
Expand All @@ -502,7 +493,7 @@ public string NodeName
/// Services returns the locally registered services
/// </summary>
/// <returns>A map of the registered services and service data</returns>
public async Task<QueryResult<Dictionary<string, AgentService>>> Services(CancellationToken ct = default(CancellationToken))
public async Task<QueryResult<Dictionary<string, AgentService>>> Services(CancellationToken ct = default)
{
return await Services(null, ct).ConfigureAwait(false);
}
Expand All @@ -512,7 +503,7 @@ public string NodeName
/// </summary>
/// <param name="filter">Specifies the expression used to filter the queries results prior to returning the data</param>
/// <returns>A map of the registered services and service data</returns>
public async Task<QueryResult<Dictionary<string, AgentService>>> Services(Filter filter, CancellationToken ct = default(CancellationToken))
public async Task<QueryResult<Dictionary<string, AgentService>>> Services(Filter filter, CancellationToken ct = default)
{
return await _client.Get<Dictionary<string, AgentService>>("/v1/agent/services", null, filter).Execute(ct).ConfigureAwait(false);
}
Expand All @@ -521,7 +512,7 @@ public string NodeName
/// Members returns the known gossip members. The WAN flag can be used to query a server for WAN members.
/// </summary>
/// <returns>An array of gossip peers</returns>
public Task<QueryResult<AgentMember[]>> Members(bool wan, CancellationToken ct = default(CancellationToken))
public Task<QueryResult<AgentMember[]>> Members(bool wan, CancellationToken ct = default)
{
var req = _client.Get<AgentMember[]>("/v1/agent/members");
if (wan)
Expand All @@ -536,7 +527,7 @@ public string NodeName
/// </summary>
/// <param name="service">A service registration object</param>
/// <returns>An empty write result</returns>
public Task<WriteResult> ServiceRegister(AgentServiceRegistration service, CancellationToken ct = default(CancellationToken))
public Task<WriteResult> ServiceRegister(AgentServiceRegistration service, CancellationToken ct = default)
{
return ServiceRegister(service, replaceExistingChecks: false, ct);
}
Expand All @@ -547,7 +538,7 @@ public string NodeName
/// <param name="service">A service registration object</param>
/// <param name="replaceExistingChecks">Missing health checks from the request will be deleted from the agent.</param>
/// <returns>An empty write result</returns>
public Task<WriteResult> ServiceRegister(AgentServiceRegistration service, bool replaceExistingChecks, CancellationToken ct = default(CancellationToken))
public Task<WriteResult> ServiceRegister(AgentServiceRegistration service, bool replaceExistingChecks, CancellationToken ct = default)
{
var req = _client.Put("/v1/agent/service/register", service, null);
if (replaceExistingChecks)
Expand All @@ -562,7 +553,7 @@ public string NodeName
/// </summary>
/// <param name="serviceID">The service ID</param>
/// <returns>An empty write result</returns>
public Task<WriteResult> ServiceDeregister(string serviceID, CancellationToken ct = default(CancellationToken))
public Task<WriteResult> ServiceDeregister(string serviceID, CancellationToken ct = default)
{
return _client.PutNothing(string.Format("/v1/agent/service/deregister/{0}", serviceID)).Execute(ct);
}
Expand All @@ -572,7 +563,7 @@ public string NodeName
/// </summary>
/// <param name="checkID">The check ID</param>
/// <param name="note">An optional, arbitrary string to write to the check status</param>
public Task PassTTL(string checkID, string note, CancellationToken ct = default(CancellationToken))
public Task PassTTL(string checkID, string note, CancellationToken ct = default)
{
return LegacyUpdateTTL(checkID, note, TTLStatus.Pass, ct);
}
Expand All @@ -582,7 +573,7 @@ public string NodeName
/// </summary>
/// <param name="checkID">The check ID</param>
/// <param name="note">An optional, arbitrary string to write to the check status</param>
public Task WarnTTL(string checkID, string note, CancellationToken ct = default(CancellationToken))
public Task WarnTTL(string checkID, string note, CancellationToken ct = default)
{
return LegacyUpdateTTL(checkID, note, TTLStatus.Warn, ct);
}
Expand All @@ -592,7 +583,7 @@ public string NodeName
/// </summary>
/// <param name="checkID">The check ID</param>
/// <param name="note">An optional, arbitrary string to write to the check status</param>
public Task FailTTL(string checkID, string note, CancellationToken ct = default(CancellationToken))
public Task FailTTL(string checkID, string note, CancellationToken ct = default)
{
return LegacyUpdateTTL(checkID, note, TTLStatus.Critical, ct);
}
Expand All @@ -604,7 +595,7 @@ public string NodeName
/// <param name="output">An optional, arbitrary string to write to the check status</param>
/// <param name="status">The state to set the check to</param>
/// <returns>An empty write result</returns>
public Task<WriteResult> UpdateTTL(string checkID, string output, TTLStatus status, CancellationToken ct = default(CancellationToken))
public Task<WriteResult> UpdateTTL(string checkID, string output, TTLStatus status, CancellationToken ct = default)
{
var u = new CheckUpdate
{
Expand All @@ -621,7 +612,7 @@ public string NodeName
/// <param name="note">An optional, arbitrary string to note on the check status</param>
/// <param name="status">The state to set the check to</param>
/// <returns>An empty write result</returns>
private Task<WriteResult> LegacyUpdateTTL(string checkID, string note, TTLStatus status, CancellationToken ct = default(CancellationToken))
private Task<WriteResult> LegacyUpdateTTL(string checkID, string note, TTLStatus status, CancellationToken ct = default)
{
var request = _client.PutNothing(string.Format("/v1/agent/check/{0}/{1}", status.LegacyStatus, checkID));
if (!string.IsNullOrEmpty(note))
Expand All @@ -636,7 +627,7 @@ public string NodeName
/// </summary>
/// <param name="check">A check registration object</param>
/// <returns>An empty write result</returns>
public Task<WriteResult> CheckRegister(AgentCheckRegistration check, CancellationToken ct = default(CancellationToken))
public Task<WriteResult> CheckRegister(AgentCheckRegistration check, CancellationToken ct = default)
{
return _client.Put("/v1/agent/check/register", check, null).Execute(ct);
}
Expand All @@ -646,7 +637,7 @@ public string NodeName
/// </summary>
/// <param name="checkID">The check ID to deregister</param>
/// <returns>An empty write result</returns>
public Task<WriteResult> CheckDeregister(string checkID, CancellationToken ct = default(CancellationToken))
public Task<WriteResult> CheckDeregister(string checkID, CancellationToken ct = default)
{
return _client.PutNothing(string.Format("/v1/agent/check/deregister/{0}", checkID)).Execute(ct);
}
Expand All @@ -657,7 +648,7 @@ public string NodeName
/// <param name="addr">The address to join to</param>
/// <param name="wan">Join the WAN pool</param>
/// <returns>An empty write result</returns>
public Task<WriteResult> Join(string addr, bool wan, CancellationToken ct = default(CancellationToken))
public Task<WriteResult> Join(string addr, bool wan, CancellationToken ct = default)
{
var req = _client.PutNothing(string.Format("/v1/agent/join/{0}", addr));
if (wan)
Expand All @@ -672,7 +663,7 @@ public string NodeName
/// </summary>
/// <param name="node">The node name to remove</param>
/// <returns>An empty write result</returns>
public Task<WriteResult> ForceLeave(string node, CancellationToken ct = default(CancellationToken))
public Task<WriteResult> ForceLeave(string node, CancellationToken ct = default)
{
return _client.PutNothing(string.Format("/v1/agent/force-leave/{0}", node)).Execute(ct);
}
Expand All @@ -682,7 +673,7 @@ public string NodeName
/// Leave is used to have the agent gracefully leave the cluster and shutdown
/// </summary>
/// <returns>An empty write result</returns>
public Task<WriteResult> Leave(string node, CancellationToken ct = default(CancellationToken))
public Task<WriteResult> Leave(string node, CancellationToken ct = default)
{
return _client.PutNothing("/v1/agent/leave").Execute(ct);
}
Expand All @@ -691,7 +682,7 @@ public string NodeName
/// Reload triggers a configuration reload for the agent we are connected to.
/// </summary>
/// <returns>An empty write result</returns>
public Task<WriteResult> Reload(string node, CancellationToken ct = default(CancellationToken))
public Task<WriteResult> Reload(string node, CancellationToken ct = default)
{
return _client.PutNothing("/v1/agent/reload").Execute(ct);
}
Expand All @@ -702,7 +693,7 @@ public string NodeName
/// <param name="serviceID">The service ID</param>
/// <param name="reason">An optional reason</param>
/// <returns>An empty write result</returns>
public Task<WriteResult> EnableServiceMaintenance(string serviceID, string reason, CancellationToken ct = default(CancellationToken))
public Task<WriteResult> EnableServiceMaintenance(string serviceID, string reason, CancellationToken ct = default)
{
var req = _client.PutNothing(string.Format("/v1/agent/service/maintenance/{0}", serviceID));
req.Params["enable"] = "true";
Expand All @@ -715,7 +706,7 @@ public string NodeName
/// </summary>
/// <param name="serviceID">The service ID</param>
/// <returns>An empty write result</returns>
public Task<WriteResult> DisableServiceMaintenance(string serviceID, CancellationToken ct = default(CancellationToken))
public Task<WriteResult> DisableServiceMaintenance(string serviceID, CancellationToken ct = default)
{
var req = _client.PutNothing(string.Format("/v1/agent/service/maintenance/{0}", serviceID));
req.Params["enable"] = "false";
Expand All @@ -727,7 +718,7 @@ public string NodeName
/// </summary>
/// <param name="reason">An optional reason</param>
/// <returns>An empty write result</returns>
public Task<WriteResult> EnableNodeMaintenance(string reason, CancellationToken ct = default(CancellationToken))
public Task<WriteResult> EnableNodeMaintenance(string reason, CancellationToken ct = default)
{
var req = _client.PutNothing("/v1/agent/maintenance");
req.Params["enable"] = "true";
Expand All @@ -739,7 +730,7 @@ public string NodeName
/// DisableNodeMaintenance toggles node maintenance mode off for the agent we are connected to
/// </summary>
/// <returns>An empty write result</returns>
public Task<WriteResult> DisableNodeMaintenance(CancellationToken ct = default(CancellationToken))
public Task<WriteResult> DisableNodeMaintenance(CancellationToken ct = default)
{
var req = _client.PutNothing("/v1/agent/maintenance");
req.Params["enable"] = "false";
Expand All @@ -751,14 +742,26 @@ public string NodeName
/// Providing a CancellationToken can be used to close the connection and stop the
/// log stream, otherwise the log stream will time out based on the HTTP Client's timeout value.
/// </summary>
public async Task<LogStream> Monitor(LogLevel level = default(LogLevel), CancellationToken ct = default(CancellationToken))
public async Task<LogStream> Monitor(LogLevel level = default, bool logJSON = false, CancellationToken ct = default)
winstxnhdw marked this conversation as resolved.
Show resolved Hide resolved
{
var req = _client.Get<Stream>("/v1/agent/monitor");
req.Params["loglevel"] = level.ToString().ToLowerInvariant();

if (logJSON)
{
req.Params["logjson"] = "true";
}

var res = await req.ExecuteStreaming(ct).ConfigureAwait(false);
return new LogStream(res.Response);
}

// MonitorJSON is like Monitor except it returns logs in JSON format.
public async Task<LogStream> MonitorJSON(LogLevel level = default, CancellationToken ct = default)
winstxnhdw marked this conversation as resolved.
Show resolved Hide resolved
{
return await Monitor(level, true, ct).ConfigureAwait(false);
}

/// <summary>
/// Log streamer
/// </summary>
Expand All @@ -776,6 +779,7 @@ public void Dispose()
{
_streamreader.Dispose();
_stream.Dispose();
GC.SuppressFinalize(this);
winstxnhdw marked this conversation as resolved.
Show resolved Hide resolved
}

public IEnumerator<Task<string>> GetEnumerator()
Expand All @@ -801,9 +805,6 @@ public partial class ConsulClient : IConsulClient
/// <summary>
/// Agent returns a handle to the agent endpoints
/// </summary>
public IAgentEndpoint Agent
{
get { return _agent.Value; }
}
public IAgentEndpoint Agent => _agent.Value;
}
}
Loading