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

Initial implementation for workflow log tracing #1176

Merged
merged 8 commits into from
Nov 14, 2023
88 changes: 88 additions & 0 deletions src/Dapr.Workflow/WorkflowLoggingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// ------------------------------------------------------------------------
// 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;

/// <summary>
/// Defines runtime options for workflows.
/// </summary>
internal sealed class WorkflowLoggingService : IHostedService
Copy link
Contributor

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.

{
private readonly ILogger<WorkflowLoggingService> logger;
private static HashSet<string>? registeredWorkflows;
RyanLettieri marked this conversation as resolved.
Show resolved Hide resolved
private static HashSet<string>? registeredActivities;
private LogLevel logLevel = LogLevel.Debug;

public WorkflowLoggingService(ILogger<WorkflowLoggingService> logger)
{
var value = Environment.GetEnvironmentVariable("DAPR_LOG_LEVEL");
RyanLettieri marked this conversation as resolved.
Show resolved Hide resolved
RyanLettieri marked this conversation as resolved.
Show resolved Hide resolved
logLevel = string.IsNullOrEmpty(value) ? LogLevel.Debug : (LogLevel)Enum.Parse(typeof(LogLevel), value);
RyanLettieri marked this conversation as resolved.
Show resolved Hide resolved
this.logger = logger;
registeredActivities = new HashSet<string>();
registeredWorkflows = new HashSet<string>();
}
public Task StartAsync(CancellationToken cancellationToken)
{
this.logger.Log(logLevel, "WorkflowLoggingService started");

if (registeredWorkflows != null)
{
this.logger.Log(logLevel, "List of registered workflows");
foreach (string item in registeredWorkflows)
{
this.logger.Log(logLevel, item);
}
}

if (registeredActivities != null)
{
this.logger.Log(logLevel, "List of registered activities:");
foreach (string item in registeredActivities)
{
this.logger.Log(logLevel, item);
}
}
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
this.logger.Log(logLevel, "WorkflowLoggingService stopped");

return Task.CompletedTask;
}

public static void LogWorkflowName(string workflowName)
{
if (registeredWorkflows != null)
{
registeredWorkflows.Add(workflowName);
}
}

public static void LogActivityName(string activityName)
{
if (registeredActivities != null)
{
registeredActivities.Add(activityName);
}
}
}
}
4 changes: 4 additions & 0 deletions src/Dapr.Workflow/WorkflowRuntimeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void RegisterWorkflow<TInput, TOutput>(string name, Func<WorkflowContext,
WorkflowContext workflowContext = new DaprWorkflowContext(innerContext);
return implementation(workflowContext, input);
});
WorkflowLoggingService.LogWorkflowName(name);
});
}

Expand All @@ -73,6 +74,7 @@ public void RegisterWorkflow<TInput, TOutput>(string name, Func<WorkflowContext,
TWorkflow workflow = Activator.CreateInstance<TWorkflow>();
return new OrchestratorWrapper(workflow);
});
WorkflowLoggingService.LogWorkflowName(name);
});
}

Expand All @@ -91,6 +93,7 @@ public void RegisterActivity<TInput, TOutput>(string name, Func<WorkflowActivity
WorkflowActivityContext activityContext = new(innerContext);
return implementation(activityContext, input);
});
WorkflowLoggingService.LogActivityName(name);
});
}

Expand All @@ -111,6 +114,7 @@ public void RegisterActivity<TActivity>() where TActivity : class, IWorkflowActi
TActivity activity = ActivatorUtilities.CreateInstance<TActivity>(serviceProvider);
return new ActivityWrapper(activity);
});
WorkflowLoggingService.LogActivityName(name);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static IServiceCollection AddDaprWorkflow(
#pragma warning disable CS0618 // Type or member is obsolete - keeping around temporarily - replaced by DaprWorkflowClient
serviceCollection.TryAddSingleton<WorkflowEngineClient>();
#pragma warning restore CS0618 // Type or member is obsolete

serviceCollection.AddHostedService<WorkflowLoggingService>();
serviceCollection.TryAddSingleton<DaprWorkflowClient>();
serviceCollection.AddDaprClient();
serviceCollection.AddDaprWorkflowClient();
Expand Down