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
38 changes: 36 additions & 2 deletions src/WebJobs.Script/Config/ScriptJobHostOptions.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Azure.WebJobs.Script.Diagnostics.OpenTelemetry;

namespace Microsoft.Azure.WebJobs.Script
{
public class ScriptJobHostOptions
public class ScriptJobHostOptions : IOptionsFormatter
{
private static readonly JsonSerializerOptions _serializerOptions = new()
{
Converters = { new ScriptJobHostOptionsConverter() },
WriteIndented = true,
};

private string _rootScriptPath;
private ImmutableArray<string> _directorySnapshot;

Expand Down Expand Up @@ -145,5 +154,30 @@ public string RootScriptPath
/// Gets or sets a value indicating the timeout duration for the function metadata provider.
/// </summary>
public TimeSpan MetadataProviderTimeout { get; set; } = TimeSpan.Zero;

public string Format()
{
return JsonSerializer.Serialize(this, _serializerOptions);
}

private class ScriptJobHostOptionsConverter : JsonConverter<ScriptJobHostOptions>
{
public override ScriptJobHostOptions Read(
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}

public override void Write(
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need custom JsonConverter?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've considered adding the JsonSourceGenerationOptions attribute to use source generation to create a SerializationContext as we do in other Options classes. However, since we didn't want to log all the properties but a few, almost all the properties would have ended with a JsonIgnore attribute and didn't like that approach.

Utf8JsonWriter writer, ScriptJobHostOptions value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteBoolean(nameof(value.FileWatchingEnabled), value.FileWatchingEnabled);
writer.WriteString(nameof(value.FileLoggingMode), value.FileLoggingMode.ToString());
writer.WriteString(nameof(value.FunctionTimeout), value.FunctionTimeout?.ToString());
writer.WriteString(nameof(value.TelemetryMode), value.TelemetryMode.ToString());
writer.WriteEndObject();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// 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.Text.Json;
using AwesomeAssertions;
using Microsoft.Azure.WebJobs.Script.Diagnostics.OpenTelemetry;
using Xunit;

namespace Microsoft.Azure.WebJobs.Script.Tests.Configuration
{
public class ScriptJobHostOptionsTests
{
[Theory]
[InlineData(FileLoggingMode.Always, 10, true, "Always", "00:10:00", "ApplicationInsights")]
[InlineData(FileLoggingMode.Never, 5, false, "Never", "00:05:00", "OpenTelemetry")]
[InlineData(FileLoggingMode.DebugOnly, 0.5, true, "DebugOnly", "00:00:30", "ApplicationInsights")]
public void Format_SerializesOptionsToJson(
FileLoggingMode loggingMode,
double timeoutMinutes,
bool fileWatchingEnabled,
string expectedLoggingMode,
string expectedTimeout,
string expectedTelemetryMode)
{
var options = new ScriptJobHostOptions
{
FileLoggingMode = loggingMode,
FunctionTimeout = TimeSpan.FromMinutes(timeoutMinutes),
FileWatchingEnabled = fileWatchingEnabled,
TelemetryMode = Enum.Parse<TelemetryMode>(expectedTelemetryMode)
};

string json = options.Format();
var root = JsonDocument.Parse(json).RootElement;

root.TryGetProperty("FileWatchingEnabled", out var fileWatchingProperty).Should().BeTrue();
fileWatchingProperty.GetBoolean().Should().Be(fileWatchingEnabled);

root.TryGetProperty("FileLoggingMode", out var fileLoggingProperty).Should().BeTrue();
fileLoggingProperty.GetString().Should().Be(expectedLoggingMode);

root.TryGetProperty("FunctionTimeout", out var timeoutProperty).Should().BeTrue();
timeoutProperty.GetString().Should().Be(expectedTimeout);

root.TryGetProperty("TelemetryMode", out var telemetryModeProperty).Should().BeTrue();
telemetryModeProperty.GetString().Should().Be(expectedTelemetryMode);
}

[Fact]
public void Format_WithNullFunctionTimeout_SerializesAsNull()
{
var options = new ScriptJobHostOptions
{
FileLoggingMode = FileLoggingMode.Never,
FileWatchingEnabled = true,
FunctionTimeout = null
};

string json = options.Format();
var root = JsonDocument.Parse(json).RootElement;

root.TryGetProperty("FunctionTimeout", out var timeoutProperty).Should().BeTrue();
timeoutProperty.ValueKind.Should().Be(JsonValueKind.Null);
}

[Fact]
public void Format_ReturnsValidIndentedJson()
{
var options = new ScriptJobHostOptions
{
FileLoggingMode = FileLoggingMode.Always,
FileWatchingEnabled = true,
FunctionTimeout = TimeSpan.FromMinutes(5)
};

string json = options.Format();

// Should be valid JSON
var exception = Record.Exception(() => JsonDocument.Parse(json));
exception.Should().BeNull();

// Should be indented (contains newlines)
json.Should().Contain(Environment.NewLine);
}
}
}