-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SPDBT-3022: added dynamics instrumentation (#1417)
- Loading branch information
Showing
15 changed files
with
163 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
# directories | ||
**/bin/ | ||
**/obj/ | ||
**/out/ | ||
**/node_modules/ | ||
**/.angular/ | ||
**/bin | ||
**/obj | ||
**/out | ||
**/node_modules | ||
**/.angular | ||
**/.vs | ||
|
||
# files | ||
*[Dd]ockerfile* | ||
**/*.trx | ||
**/*.md | ||
**/*.ps1 | ||
**/*.cmd | ||
**/*.sh | ||
**/*.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,85 @@ | ||
namespace Spd.Utilities.Dynamics | ||
using Microsoft.OData.Client; | ||
using System.Diagnostics; | ||
using System.Net; | ||
|
||
namespace Spd.Utilities.Dynamics; | ||
|
||
public class DynamicsContext : Microsoft.Dynamics.CRM.System, IDisposable | ||
{ | ||
public class DynamicsContext : Microsoft.Dynamics.CRM.System | ||
public static readonly string ActivitySourceName = typeof(DynamicsContext).FullName!; | ||
private static ActivitySource source = new(ActivitySourceName); | ||
private Activity? executionActivity; | ||
|
||
private bool disposedValue; | ||
|
||
public DynamicsContext(Uri serviceRoot) : base(serviceRoot) | ||
{ | ||
public DynamicsContext(Uri serviceRoot) : base(serviceRoot) | ||
this.SendingRequest2 += Instrument; | ||
this.BuildingRequest += Instrument; | ||
this.ReceivingResponse += Instrument; | ||
} | ||
|
||
private void Instrument(object? sender, ReceivingResponseEventArgs e) | ||
{ | ||
if (executionActivity != null) | ||
{ | ||
var tags = new ActivityTagsCollection | ||
{ | ||
{ nameof(e.ResponseMessage.StatusCode), e.ResponseMessage.StatusCode } | ||
}; | ||
if (e.Descriptor is EntityDescriptor ed) | ||
{ | ||
tags.Add(nameof(EntityDescriptor.State), ed.State); | ||
tags.Add(nameof(EntityDescriptor.Entity), ed.Entity?.GetType().Name); | ||
} | ||
|
||
executionActivity.AddEvent(new ActivityEvent(nameof(ReceivingResponse), tags: tags)); | ||
executionActivity.SetStatus(e.ResponseMessage.StatusCode != (int)HttpStatusCode.OK ? ActivityStatusCode.Error : ActivityStatusCode.Ok); | ||
executionActivity.Stop(); | ||
} | ||
} | ||
|
||
private void Instrument(object? sender, BuildingRequestEventArgs e) | ||
{ | ||
executionActivity = source.StartActivity(e.Method); | ||
executionActivity?.SetTag(nameof(e.Method), e.Method); | ||
} | ||
|
||
private void Instrument(object? sender, SendingRequest2EventArgs e) | ||
{ | ||
if (executionActivity != null) | ||
{ | ||
var tags = new ActivityTagsCollection | ||
{ | ||
{ nameof(e.IsBatchPart), e.IsBatchPart }, | ||
{ nameof(e.IsBulkUpdate), e.IsBulkUpdate }, | ||
{ nameof(e.RequestMessage.Url), e.RequestMessage.Url.ToString() } | ||
}; | ||
if (e.Descriptor is EntityDescriptor ed) | ||
{ | ||
tags.Add(nameof(EntityDescriptor.State), ed.State); | ||
tags.Add(nameof(EntityDescriptor.Entity), ed.Entity?.GetType().Name); | ||
} | ||
executionActivity.AddEvent(new ActivityEvent(nameof(SendingRequest2), tags: tags)); | ||
} | ||
} | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!disposedValue) | ||
{ | ||
if (disposing) | ||
{ | ||
executionActivity?.Dispose(); | ||
} | ||
|
||
disposedValue = true; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Spd.Utilities.Hosting/IProvideInstrumentationSources.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Spd.Utilities.Hosting; | ||
public interface IProvideInstrumentationSources | ||
{ | ||
InstrumentationSources GetInstrumentationSources(); | ||
} | ||
|
||
public record InstrumentationSources | ||
{ | ||
public IEnumerable<string> TraceSources { get; set; } = []; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.