Skip to content

Commit

Permalink
Merge pull request #22 from digma-ai/target_netstandard
Browse files Browse the repository at this point in the history
change framework target to netstandard
  • Loading branch information
shaykeren authored Oct 15, 2023
2 parents c465afd + 508849f commit cedb8bd
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection;

namespace OpenTelemetry.Instrumentation.Digma.Diagnostic;

public static class HttpDiagnosticObserverExtensions
{
public static IServiceCollection UseDigmaHttpDiagnosticObserver(this IServiceCollection serviceCollection)
Expand All @@ -30,16 +30,16 @@ public void OnNext(KeyValuePair<string, object?> pair)
if (pair.Key != "Microsoft.AspNetCore.Routing.EndpointMatched")
return;

var context = (HttpContext) pair.Value;
var endpoint = context?.GetEndpoint();
var descriptor = endpoint?.Metadata.GetMetadata<ControllerActionDescriptor>();
if (descriptor == null)
return;
var httpContext = (HttpContext) pair.Value;
if(httpContext == null)return;
var endpointFeature = httpContext.Features?.Get<IEndpointFeature>();
var descriptor = endpointFeature?.Endpoint.Metadata.GetMetadata<ControllerActionDescriptor>();
if (descriptor == null) return;
SpanUtils.AddCommonTags(descriptor.ControllerTypeInfo, descriptor.MethodInfo, Activity.Current);
}

public bool CanHandle(string diagnosticListener)
{
return diagnosticListener == "Microsoft.AspNetCore";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace OpenTelemetry.Instrumentation.Digma.Diagnostic;

public interface IDigmaDiagnosticObserver:IObserver<KeyValuePair<string, object?>>
public interface IDigmaDiagnosticObserver : IObserver<KeyValuePair<string, object?>>
{
bool CanHandle(string diagnosticListener);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@
// limitations under the License.
// </copyright>

namespace OpenTelemetry.Instrumentation.Digma
namespace OpenTelemetry.Instrumentation.Digma;
public class DigmaConfigurationOptions
{
public class DigmaConfigurationOptions
{
private const string DEFAULT_COMMIT_ENV_VAR = "DEPLOYMENT_COMMIT_ID";
private const string DEFAULT_DEPLOYMENT_ENV_ENV_VAR = "DEPLOYMENT_ENV";
private const string DEFAULT_DIGMA_ENV_ENV_VAR = "DIGMA_ENV";
private const string DEFAULT_COMMIT_ENV_VAR = "DEPLOYMENT_COMMIT_ID";
private const string DEFAULT_DEPLOYMENT_ENV_ENV_VAR = "DEPLOYMENT_ENV";
private const string DEFAULT_DIGMA_ENV_ENV_VAR = "DIGMA_ENV";

public string? NamespaceRoot { get; set; } = null;
public string? Environment { get; set; } = null;
public string CommitIdEnvVariable { get; set; } = DEFAULT_COMMIT_ENV_VAR;
[Obsolete($"Please use {nameof(DigmaEnvironmentEnvVariable)}")]
public string EnvironmentEnvVariable { get; set; } = DEFAULT_DEPLOYMENT_ENV_ENV_VAR;
public string DigmaEnvironmentEnvVariable { get; set; } = DEFAULT_DIGMA_ENV_ENV_VAR;
public string? CommitId { get; set; } = null;
public string? SpanMappingPattern { get; set; } = "";
public string? SpanMappingReplacement { get; set; } = "";
}
}
public string? NamespaceRoot { get; set; } = null;
public string? Environment { get; set; } = null;
public string CommitIdEnvVariable { get; set; } = DEFAULT_COMMIT_ENV_VAR;
[Obsolete("Please use DigmaEnvironmentEnvVariable")]
public string EnvironmentEnvVariable { get; set; } = DEFAULT_DEPLOYMENT_ENV_ENV_VAR;
public string DigmaEnvironmentEnvVariable { get; set; } = DEFAULT_DIGMA_ENV_ENV_VAR;
public string? CommitId { get; set; } = null;
public string? SpanMappingPattern { get; set; } = "";
public string? SpanMappingReplacement { get; set; } = "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace OpenTelemetry.Instrumentation.Digma;

public static class DigmaInstrumentationHelperExtensions
{
private static readonly HashSet<string> IgnoreNamespaces = new() { "Microsoft", "System" };
private static readonly HashSet<string> IgnoreNamespaces = new HashSet<string>() {"Microsoft", "System"};

public static ResourceBuilder AddDigmaAttributes(this ResourceBuilder builder,
Action<DigmaConfigurationOptions>? configure = null)
Expand Down Expand Up @@ -63,10 +63,12 @@ public static ResourceBuilder AddDigmaAttributes(this ResourceBuilder builder,
{
options.Environment = Environment.GetEnvironmentVariable(options.DigmaEnvironmentEnvVariable) ?? "";
}

if (string.IsNullOrWhiteSpace(options.Environment))
{
options.Environment = Environment.GetEnvironmentVariable(options.EnvironmentEnvVariable) ?? "";
}

if (string.IsNullOrWhiteSpace(options.Environment))
{
options.Environment = hostName + "[local]";
Expand All @@ -84,4 +86,4 @@ public static ResourceBuilder AddDigmaAttributes(this ResourceBuilder builder,
});
return builder;
}
}
}
8 changes: 4 additions & 4 deletions src/OpenTelemetry.Instrumentation.Digma/EndpointMonitoring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public Task StopAsync(CancellationToken cancellationToken)
_observer.Dispose();
return Task.CompletedTask;
}


private class DiagnosticSubscriber : IObserver<DiagnosticListener>, IDisposable
{
private readonly IEnumerable<IDigmaDiagnosticObserver> _diagnosticObservers;
private readonly List<IDisposable> _subscriptions;

public DiagnosticSubscriber(IEnumerable<IDigmaDiagnosticObserver> diagnosticObservers)
{
_diagnosticObservers = diagnosticObservers;
Expand All @@ -53,7 +54,7 @@ public void OnError(Exception error)
public void OnNext(DiagnosticListener value)
{
var diagnosticObserver = _diagnosticObservers.FirstOrDefault(o => o.CanHandle(value.Name));
if(diagnosticObserver is null) return;
if (diagnosticObserver == null) return;
var subscription = value.Subscribe(diagnosticObserver);
_subscriptions.Add(subscription);
}
Expand All @@ -71,4 +72,3 @@ public void Dispose()
}



Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ private static void EnsureAttributeSyntax(string[] attributes, string[][] attrib
" The correct syntax is \"key:value\"");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ namespace OpenTelemetry.Instrumentation.Digma.Helpers.Attributes;
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class NoActivityAttribute : Attribute
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ public TraceActivityAttribute(string? name = null, bool recordExceptions = true)
Name = name;
RecordExceptions = recordExceptions;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Reflection;

namespace OpenTelemetry.Instrumentation.Digma.Helpers;

public interface IActivityNamingSchema
{
public string GetSpanName(Type classType, MethodInfo method);
Expand Down Expand Up @@ -29,4 +28,4 @@ public string GetSpanName(Type classType, MethodInfo method)
{
return $"{classType.Name}.{method.Name}";
}
}
}
28 changes: 15 additions & 13 deletions src/OpenTelemetry.Instrumentation.Digma/Helpers/TracingDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using OpenTelemetry.Trace;

namespace OpenTelemetry.Instrumentation.Digma.Helpers;

public class TraceDecorator<TDecorated> : DispatchProxy where TDecorated : class
{
private ActivitySource _activity;
Expand All @@ -23,23 +22,25 @@ public static TDecorated Create(TDecorated decorated, IActivityNamingSchema? act
bool decorateAllMethods = true)
{
object proxy = Create<TDecorated, TraceDecorator<TDecorated>>()!;
((TraceDecorator<TDecorated>)proxy!).SetParameters(decorated, activityNamingSchema, decorateAllMethods);
((TraceDecorator<TDecorated>) proxy!).SetParameters(decorated, activityNamingSchema, decorateAllMethods);

return (TDecorated)proxy;
return (TDecorated) proxy;
}

private void SetParameters(TDecorated decorated, IActivityNamingSchema? spanNamingSchema, bool decorateAllMethods)
private void SetParameters(TDecorated decorated, IActivityNamingSchema? spanNamingSchema,
bool decorateAllMethods)
{
_decorated = decorated;
_activity = new(_decorated!.GetType().FullName!);
_activity = new ActivitySource(_decorated!.GetType().FullName!);
_decorateAllMethods = decorateAllMethods;
if (spanNamingSchema != null)
{
_namingSchema = spanNamingSchema;
}
}

private object? InvokeDecoratedExecution(Activity? activity, MethodInfo? targetMethod, object?[]? args, bool? recordException)
private object? InvokeDecoratedExecution(Activity? activity, MethodInfo? targetMethod, object?[]? args,
bool? recordException)
{
object? result;
try
Expand All @@ -48,13 +49,13 @@ private void SetParameters(TDecorated decorated, IActivityNamingSchema? spanNami
}
catch (Exception e)
{
if(recordException ?? true)
if (recordException ?? true)
activity?.RecordException(e);

activity?.Dispose();
throw;
}

if (result is Task resultTask)
{
resultTask.ContinueWith(task =>
Expand All @@ -66,7 +67,7 @@ private void SetParameters(TDecorated decorated, IActivityNamingSchema? spanNami

activity?.Dispose();
}, TaskContinuationOptions.AttachedToParent | TaskContinuationOptions.ExecuteSynchronously);

return result;
}

Expand All @@ -82,7 +83,7 @@ private void SetParameters(TDecorated decorated, IActivityNamingSchema? spanNami
if (noActivityAttribute == null && (_decorateAllMethods || activityAttribute != null))
{
var classType = _decorated!.GetType();

var defaultSpanName = _namingSchema.GetSpanName(classType, targetMethod);
var activity = _activity.StartActivity(activityAttribute?.Name ?? defaultSpanName);

Expand All @@ -97,7 +98,8 @@ private void SetParameters(TDecorated decorated, IActivityNamingSchema? spanNami

private void InjectAttributes(MethodInfo targetMethod, Activity? activity)
{
var methodActivityAttributes = targetMethod.GetCustomAttribute<ActivitiesAttributesAttribute>(inherit: false);
var methodActivityAttributes =
targetMethod.GetCustomAttribute<ActivitiesAttributesAttribute>(inherit: false);
var classActivityAttributes =
_decorated.GetType().GetCustomAttribute<ActivitiesAttributesAttribute>(inherit: false);

Expand All @@ -117,4 +119,4 @@ private void InjectAttributes(MethodInfo targetMethod, Activity? activity)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
<Version>0.6.12</Version>
<Authors>Digma Team</Authors>
<Company>Digma</Company>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>


<RootNamespace>OpenTelemetry.Instrumentation.Digma</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="OpenTelemetry" Version="1.3.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.4"/>
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/OpenTelemetry.Instrumentation.Digma/SpanUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Reflection;

namespace OpenTelemetry.Instrumentation.Digma;

public static class SpanUtils
{
public static void AddCommonTags(Type classType, MethodInfo methodInfo, Activity? activity)
Expand Down

0 comments on commit cedb8bd

Please sign in to comment.