-
Notifications
You must be signed in to change notification settings - Fork 335
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
Initial implementation for workflow log tracing #1176
Changes from 4 commits
b4f1e10
017656e
30962d7
5649d74
7b3f381
2f5f3f4
3917fb9
71064cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2022 The Dapr Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------ | ||
|
||
namespace Dapr.Workflow | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
/// <summary> | ||
/// Defines runtime options for workflows. | ||
/// </summary> | ||
internal sealed class WorkflowLoggingService : IHostedService | ||
{ | ||
private readonly ILogger<WorkflowLoggingService> logger; | ||
private static readonly HashSet<string> registeredWorkflows = new(); | ||
private static readonly HashSet<string> registeredActivities = new(); | ||
private LogLevel logLevel = LogLevel.Information; | ||
|
||
public WorkflowLoggingService(ILogger<WorkflowLoggingService> logger, IConfiguration configuration) | ||
{ | ||
logLevel = string.IsNullOrEmpty(configuration["DAPR_LOG_LEVEL"]) ? LogLevel.Information : ConvertLogLevel(configuration["DAPR_LOG_LEVEL"]); | ||
RyanLettieri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.logger = logger; | ||
|
||
} | ||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
this.logger.Log(LogLevel.Information, "WorkflowLoggingService started"); | ||
|
||
this.logger.Log(LogLevel.Information, "List of registered workflows"); | ||
foreach (string item in registeredWorkflows) | ||
{ | ||
this.logger.Log(LogLevel.Information, item); | ||
} | ||
|
||
this.logger.Log(LogLevel.Information, "List of registered activities:"); | ||
foreach (string item in registeredActivities) | ||
{ | ||
this.logger.Log(LogLevel.Information, item); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Indentation is weird here. |
||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
this.logger.Log(LogLevel.Information, "WorkflowLoggingService stopped"); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public static void LogWorkflowName(string workflowName) | ||
{ | ||
registeredWorkflows.Add(workflowName); | ||
} | ||
|
||
public static void LogActivityName(string activityName) | ||
{ | ||
registeredActivities.Add(activityName); | ||
} | ||
|
||
private LogLevel ConvertLogLevel(string ?daprLogLevel) | ||
{ | ||
LogLevel logLevel = LogLevel.Information; | ||
if (!string.IsNullOrEmpty(daprLogLevel)) | ||
{ | ||
Enum.TryParse(daprLogLevel, out logLevel); | ||
} | ||
return logLevel; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,25 +11,76 @@ | |
// limitations under the License. | ||
// ------------------------------------------------------------------------ | ||
using System; | ||
using System.IO; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Dapr.Client; | ||
using FluentAssertions; | ||
using Xunit; | ||
using System.Linq; | ||
using System.Diagnostics; | ||
|
||
namespace Dapr.E2E.Test | ||
{ | ||
[Obsolete] | ||
public partial class E2ETests | ||
{ | ||
[Fact] | ||
public async Task TestWorkflowLogging() | ||
{ | ||
// This test starts the daprclient and searches through the logfile to ensure the | ||
// workflow logger is correctly logging the registered workflow(s) and activity(s) | ||
|
||
Dictionary<string, bool> logStrings = new Dictionary<string, bool>(); | ||
logStrings["PlaceOrder"] = false; | ||
logStrings["ShipProduct"] = false; | ||
var logFilePath = "../../../../../test/Dapr.E2E.Test.App/log.txt"; | ||
var allLogsFound = false; | ||
var timeout = 30; // 30s | ||
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeout)); | ||
using var daprClient = new DaprClientBuilder().UseGrpcEndpoint(this.GrpcEndpoint).UseHttpEndpoint(this.HttpEndpoint).Build(); | ||
var health = await daprClient.CheckHealthAsync(); | ||
health.Should().Be(true, "DaprClient is not healthy"); | ||
|
||
var searchTask = Task.Run(() => | ||
{ | ||
using (StreamReader reader = new StreamReader(logFilePath)) | ||
{ | ||
string line; | ||
|
||
while ((line = reader.ReadLine()) != null) | ||
{ | ||
foreach (var entry in logStrings) | ||
{ | ||
if (line.Contains(entry.Key)) | ||
{ | ||
logStrings[entry.Key] = true; | ||
} | ||
} | ||
allLogsFound = logStrings.All(k => k.Value); | ||
if (allLogsFound) | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
}, cts.Token); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK, this You could use the |
||
|
||
if (!Task.WaitAll(new Task[] {searchTask}, timeout)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: maybe hold off testing the result until after the log file has been deleted (rather than two deletion paths). |
||
{ | ||
File.Delete(logFilePath); | ||
Assert.True(false, "Expected logs weren't found within timeout period"); | ||
} | ||
File.Delete(logFilePath); | ||
} | ||
[Fact] | ||
public async Task TestWorkflows() | ||
{ | ||
string instanceId = "testInstanceId"; | ||
string instanceId2 = "EventRaiseId"; | ||
string workflowComponent = "dapr"; | ||
string workflowName = "PlaceOrder"; | ||
var instanceId = "testInstanceId"; | ||
var instanceId2 = "EventRaiseId"; | ||
var workflowComponent = "dapr"; | ||
var workflowName = "PlaceOrder"; | ||
object input = "paperclips"; | ||
Dictionary<string, string> workflowOptions = new Dictionary<string, string>(); | ||
workflowOptions.Add("task_queue", "testQueue"); | ||
|
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.
I'm not sure if hosted service is the right way to go here. I was thinking more that this would be injected into the various workflow runtime mechanics, like the context.