Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
9 changes: 7 additions & 2 deletions src/DotNetWorker.Core/Context/DefaultTraceContext.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
// 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.Collections.Generic;

namespace Microsoft.Azure.Functions.Worker
{
internal sealed class DefaultTraceContext : TraceContext
{
public DefaultTraceContext(string traceParent, string traceState)
public DefaultTraceContext(string traceParent, string traceState, IReadOnlyDictionary<string, string> attributes)
{
TraceParent = traceParent;
TraceState = traceState;
Attributes = attributes;
}

public override string TraceParent { get; }

public override string TraceState { get; }

public override IReadOnlyDictionary<string, string> Attributes { get; }
}
}
9 changes: 8 additions & 1 deletion src/DotNetWorker.Core/Context/TraceContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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.Collections.Generic;

namespace Microsoft.Azure.Functions.Worker
{
/// <summary>
Expand All @@ -17,5 +19,10 @@ public abstract class TraceContext
/// Gets the state data.
/// </summary>
public abstract string TraceState { get; }

/// <summary>
/// Gets the attributes associated with the trace.
/// </summary>
public abstract IReadOnlyDictionary<string, string> Attributes { get; }
}
}
110 changes: 0 additions & 110 deletions src/DotNetWorker.Core/Diagnostics/ActivityExtensions.cs

This file was deleted.

66 changes: 0 additions & 66 deletions src/DotNetWorker.Core/Diagnostics/FunctionActivitySourceFactory.cs

This file was deleted.

61 changes: 0 additions & 61 deletions src/DotNetWorker.Core/Diagnostics/FunctionInvocationScope.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// 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.

namespace Microsoft.Azure.Functions.Worker.Diagnostics
{
internal enum OpenTelemetrySchemaVersion
{
v1_17_0
V1_17_0,
V1_37_0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Diagnostics;

namespace Microsoft.Azure.Functions.Worker.Diagnostics;

/// <summary>
/// Provides methods for telemetry data collection related to function invocations.
/// </summary>
/// <remarks>This interface defines methods to retrieve telemetry attributes and manage the activity lifecycle for
/// function invocations, enabling detailed monitoring and diagnostics.</remarks>
internal interface IFunctionTelemetryProvider
{
/// <summary>
/// Returns the attributes to be applied to the Activity/Scope for this invocation.
/// </summary>
IEnumerable<KeyValuePair<string, object>> GetTelemetryAttributes(FunctionContext ctx);

/// <summary>
/// Starts the Activity for this invocation.
/// </summary>
Activity? StartActivityForInvocation(FunctionContext ctx);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Diagnostics;

namespace Microsoft.Azure.Functions.Worker.Diagnostics;

internal abstract class TelemetryProviderBase : IFunctionTelemetryProvider
{
private static readonly ActivitySource _source
= new(TraceConstants.FunctionsActivitySource, TraceConstants.FunctionsActivitySourceVersion);

protected abstract OpenTelemetrySchemaVersion SchemaVersion { get; }

protected abstract ActivityKind Kind { get; }

public Activity? StartActivityForInvocation(FunctionContext context)
{
if (!_source.HasListeners())
{
return null;
}

ActivityContext.TryParse(
context.TraceContext.TraceParent,
context.TraceContext.TraceState,
out var parent);

// If there is no parent, we still want to create a new root activity.
return _source.StartActivity(
TraceConstants.FunctionsInvokeActivityName,
Kind,
parent,
tags: GetTelemetryAttributes(context)!);
}

public IEnumerable<KeyValuePair<string, object>> GetTelemetryAttributes(FunctionContext context)
{
// Live-logs session
if (context.TraceContext.Attributes.TryGetValue(TraceConstants.AzFuncLiveLogsSessionIdKey, out var liveId)
&& !string.IsNullOrWhiteSpace(liveId))
{
yield return new(TraceConstants.AzFuncLiveLogsSessionIdKey, liveId);
}

// Version-specific tags
foreach (var kv in GetVersionSpecificAttributes(context))
{
yield return kv;
}
}

protected abstract IEnumerable<KeyValuePair<string, object>> GetVersionSpecificAttributes(FunctionContext context);
}
Loading