Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#nullable enable
Aspire.Hosting.ApplicationModel.UvicornAppResource
Aspire.Hosting.ApplicationModel.UvicornAppResource.UvicornAppResource(string! name, string! workingDirectory) -> void
Aspire.Hosting.ApplicationModel.UvicornAppResource.UvicornAppResource(string! name, string! executablePath, string! workingDirectory) -> void
Aspire.Hosting.UvicornAppHostingExtension
static Aspire.Hosting.UvicornAppHostingExtension.AddUvicornApp(this Aspire.Hosting.IDistributedApplicationBuilder! builder, string! name, string! projectDirectory, string! appName, string![]? args = null) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.UvicornAppResource!>!
static Aspire.Hosting.UvicornAppHostingExtension.AddUvicornApp(this Aspire.Hosting.IDistributedApplicationBuilder! builder, string! name, string! projectDirectory, string! appName, params string![]! args) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.UvicornAppResource!>!
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#nullable enable
Aspire.Hosting.ApplicationModel.UvAppResource
Aspire.Hosting.ApplicationModel.UvAppResource.UvAppResource(string! name, string! workingDirectory) -> void
Aspire.Hosting.ApplicationModel.UvAppResource.UvAppResource(string! name, string! executablePath, string! workingDirectory) -> void
Aspire.Hosting.UvAppHostingExtension
static Aspire.Hosting.UvAppHostingExtension.AddUvApp(this Aspire.Hosting.IDistributedApplicationBuilder! builder, string! name, string! projectDirectory, string! scriptPath, params string![]! scriptArgs) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.UvAppResource!>!
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,23 @@ private static IResourceBuilder<UvAppResource> AddUvApp(this IDistributedApplica
: Path.Join(projectDirectory, virtualEnvironmentPath));

var instrumentationExecutable = virtualEnvironment.GetExecutable("opentelemetry-instrument");
// var pythonExecutable = virtualEnvironment.GetRequiredExecutable("python");
// var projectExecutable = instrumentationExecutable ?? pythonExecutable;
var projectExecutable = instrumentationExecutable ?? "uv";

string[] allArgs = args is { Length: > 0 }
? ["run", scriptPath, .. args]
: ["run", scriptPath];
var projectResource = new UvAppResource(name, projectExecutable, projectDirectory);

var projectResource = new UvAppResource(name, projectDirectory);

var resourceBuilder = builder.AddResource(projectResource)
.WithArgs(allArgs)
.WithArgs(context =>
var resourceBuilder = builder.AddResource(projectResource).WithArgs(context =>
{
// If the project is to be automatically instrumented, add the instrumentation executable arguments first.
if (!string.IsNullOrEmpty(instrumentationExecutable))
{
// If the project is to be automatically instrumented, add the instrumentation executable arguments first.
if (!string.IsNullOrEmpty(instrumentationExecutable))
{
AddOpenTelemetryArguments(context);
AddOpenTelemetryArguments(context);

// Add the uvicorn executable as the next argument so we can run the project.
context.Args.Add("uv");
}

// // Add the python executable as the next argument so we can run the project.
// context.Args.Add(pythonExecutable!);
}
});
AddProjectArguments(scriptPath, args, context);
});

if (!string.IsNullOrEmpty(instrumentationExecutable))
{
Expand All @@ -85,6 +80,17 @@ private static IResourceBuilder<UvAppResource> AddUvApp(this IDistributedApplica
return resourceBuilder;
}

private static void AddProjectArguments(string scriptPath, string[] scriptArgs, CommandLineArgsCallbackContext context)
{
context.Args.Add("run");
context.Args.Add(scriptPath);

foreach (var arg in scriptArgs)
{
context.Args.Add(arg);
}
}

private static void AddOpenTelemetryArguments(CommandLineArgsCallbackContext context)
{
context.Args.Add("--traces_exporter");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ namespace Aspire.Hosting.ApplicationModel;
/// Represents a Uv application.
/// </summary>
/// <param name="name">The name of the resource.</param>
/// <param name="executablePath">The path to the executable used to run the python app.</param>
/// <param name="workingDirectory">The working directory for uv.</param>
public class UvAppResource(string name, string workingDirectory)
: PythonAppResource(name, "uv", workingDirectory), IResourceWithServiceDiscovery
public class UvAppResource(string name, string executablePath, string workingDirectory)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sure is.

The uv extensions are under the umbrella of being experimental (there's an Experimental attribute on them) warning that the API is in flux.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, to solve this I had to make the executable a var instead of hardcoding "uv". Same for uvicorn. That's because when we want to run a python app with opentelemetry, the first command of the executable has to be open-telemetry.

: PythonAppResource(name, executablePath, workingDirectory), IResourceWithServiceDiscovery
{
internal const string HttpEndpointName = "http";
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static IResourceBuilder<UvicornAppResource> AddUvicornApp(
[ResourceName] string name,
string projectDirectory,
string appName,
string[]? args = null)
params string[] args)
{
ArgumentNullException.ThrowIfNull(builder);

Expand All @@ -36,7 +36,7 @@ private static IResourceBuilder<UvicornAppResource> AddUvicornApp(this IDistribu
string projectDirectory,
string appName,
string virtualEnvironmentPath,
string[]? args = null)
params string[] args)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(appName);
Expand All @@ -50,23 +50,23 @@ private static IResourceBuilder<UvicornAppResource> AddUvicornApp(this IDistribu
: Path.Join(projectDirectory, virtualEnvironmentPath));

var instrumentationExecutable = virtualEnvironment.GetExecutable("opentelemetry-instrument");
var projectExecutable = instrumentationExecutable ?? "uvicorn";

string[] allArgs = args is { Length: > 0 }
? [appName, .. args]
: [appName];
var projectResource = new UvicornAppResource(name, projectExecutable, projectDirectory);

var projectResource = new UvicornAppResource(name, projectDirectory);
var resourceBuilder = builder.AddResource(projectResource).WithArgs(context =>
{
// If the project is to be automatically instrumented, add the instrumentation executable arguments first.
if (!string.IsNullOrEmpty(instrumentationExecutable))
{
AddOpenTelemetryArguments(context);

// Add the uvicorn executable as the next argument so we can run the project.
context.Args.Add("uvicorn");
}

var resourceBuilder = builder.AddResource(projectResource)
.WithArgs(allArgs)
.WithArgs(context =>
{
// If the project is to be automatically instrumented, add the instrumentation executable arguments first.
if (!string.IsNullOrEmpty(instrumentationExecutable))
{
AddOpenTelemetryArguments(context);
}
});
AddProjectArguments(appName, args, context);
});

if (!string.IsNullOrEmpty(instrumentationExecutable))
{
Expand All @@ -80,6 +80,16 @@ private static IResourceBuilder<UvicornAppResource> AddUvicornApp(this IDistribu
return resourceBuilder;
}

private static void AddProjectArguments(string scriptPath, string[] scriptArgs, CommandLineArgsCallbackContext context)
{
context.Args.Add(scriptPath);

foreach (var arg in scriptArgs)
{
context.Args.Add(arg);
}
}

private static void AddOpenTelemetryArguments(CommandLineArgsCallbackContext context)
{
context.Args.Add("--traces_exporter");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Aspire.Hosting.ApplicationModel;
/// Represents a Uvicorn application.
/// </summary>
/// <param name="name">The name of the resource.</param>
/// <param name="executablePath">The path to the executable used to run the python app.</param>
/// <param name="workingDirectory">The working directory for uvicorn.</param>
public class UvicornAppResource(string name, string workingDirectory)
: PythonAppResource(name, "uvicorn", workingDirectory), IResourceWithServiceDiscovery;
public class UvicornAppResource(string name, string executablePath, string workingDirectory)
: PythonAppResource(name, executablePath, workingDirectory), IResourceWithServiceDiscovery;
Loading