-
Notifications
You must be signed in to change notification settings - Fork 469
Streaming logs and diagnostic events #1723
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,56 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reactive.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.WebJobs.Script.Eventing; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.Diagnostics | ||
{ | ||
public sealed class StructuredLogWriter : IDisposable | ||
{ | ||
private readonly IDisposable _subscription; | ||
private readonly FileTraceWriter _traceWriter; | ||
private bool _disposedValue = false; | ||
|
||
public StructuredLogWriter(IScriptEventManager eventManager, string baseLogPath) | ||
{ | ||
string logPath = Path.Combine(baseLogPath, "structured"); | ||
_traceWriter = new FileTraceWriter(logPath, TraceLevel.Verbose, s => s); | ||
|
||
_subscription = eventManager.OfType<StructuredLogEntryEvent>() | ||
.Subscribe(OnLogEntry); | ||
} | ||
|
||
private void OnLogEntry(StructuredLogEntryEvent logEvent) | ||
{ | ||
string message = logEvent.LogEntry.ToJsonLineString(); | ||
_traceWriter.Trace(message, TraceLevel.Verbose, null); | ||
} | ||
|
||
private void Dispose(bool disposing) | ||
{ | ||
if (!_disposedValue) | ||
{ | ||
if (disposing) | ||
{ | ||
_subscription?.Dispose(); | ||
_traceWriter?.Dispose(); | ||
} | ||
|
||
_disposedValue = true; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/WebJobs.Script/Eventing/StructuredLogging/StructuredLogEntry.cs
This file contains hidden or 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 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.Eventing | ||
{ | ||
public sealed class StructuredLogEntry | ||
{ | ||
private readonly Dictionary<string, object> _properties = new Dictionary<string, object>(); | ||
|
||
public StructuredLogEntry(string name) | ||
: this(Guid.NewGuid(), name) | ||
{ | ||
} | ||
|
||
public StructuredLogEntry(Guid id, string name) | ||
{ | ||
Id = id; | ||
Name = name; | ||
_properties = new Dictionary<string, object>(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the event ID. This uniquely identifies this <see cref="StructuredLogEntry"/> instance. | ||
/// </summary | ||
public Guid Id { get; } | ||
|
||
/// <summary> | ||
/// Gets the event name. This identifies the type of event represented by the <see cref="StructuredLogEntry"/> instance. | ||
/// </summary> | ||
public string Name { get; } | ||
|
||
/// <summary> | ||
/// Adds a log entry property. | ||
/// </summary> | ||
/// <param name="name">The property name.</param> | ||
/// <param name="value">The property value.</param> | ||
public void AddProperty(string name, object value) | ||
{ | ||
if (string.Equals(nameof(Name), name, StringComparison.OrdinalIgnoreCase) || | ||
string.Equals(nameof(Id), name, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
throw new ArgumentException($"{name} is an invalid property name.", nameof(name)); | ||
} | ||
|
||
_properties.Add(name, value); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a JSON string representation of this object in a single line. | ||
/// </summary> | ||
/// <returns>A JSON string representation of this object in a single line.</returns> | ||
public string ToJsonLineString() | ||
{ | ||
var resultObject = new JObject | ||
{ | ||
["name"] = Name, | ||
["id"] = Id | ||
}; | ||
foreach (var item in _properties) | ||
{ | ||
resultObject.Add(item.Key, JToken.FromObject(item.Value)); | ||
} | ||
|
||
return resultObject.ToString(Formatting.None); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/WebJobs.Script/Eventing/StructuredLogging/StructuredLogEntryEvent.cs
This file contains hidden or 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,36 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.Eventing | ||
{ | ||
public sealed class StructuredLogEntryEvent : ScriptEvent | ||
{ | ||
private readonly Lazy<StructuredLogEntry> _logEntry; | ||
|
||
public StructuredLogEntryEvent(StructuredLogEntry logEntry) | ||
: this(logEntry, string.Empty) | ||
{ | ||
} | ||
|
||
public StructuredLogEntryEvent(StructuredLogEntry logEntry, string source) | ||
: this(() => logEntry, source) | ||
{ | ||
} | ||
|
||
public StructuredLogEntryEvent(Func<StructuredLogEntry> logEntryFactory) | ||
: this(logEntryFactory, string.Empty) | ||
{ | ||
} | ||
|
||
public StructuredLogEntryEvent(Func<StructuredLogEntry> logEntryFactory, string source) | ||
: base(nameof(StructuredLogEntryEvent), source) | ||
{ | ||
_logEntry = new Lazy<StructuredLogEntry>(logEntryFactory, LazyThreadSafetyMode.ExecutionAndPublication); | ||
} | ||
|
||
public StructuredLogEntry LogEntry => _logEntry.Value; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FileTraceWriter is a public type and we just added an optional param to the constructor.. do we care about this runtime breaking change? I know script packages are still not RTM so from a semantic versioning perspective we're OK, but just wanted to double check anyway...