Skip to content

Commit

Permalink
Support ActivitySource in OPC UA client to enable tracing (.NET6) (#2251
Browse files Browse the repository at this point in the history
)

- Add traceable client session with factory to support ActivitySource on .NET 6
- Only traces the public API calls, inner calls are not yet supported.
- Uses the name: Opc.Ua.Client-TraceableSession-ActivitySource
  • Loading branch information
bhnaphade authored Aug 4, 2023
1 parent e6f3004 commit cfb3ac4
Show file tree
Hide file tree
Showing 11 changed files with 2,274 additions and 22 deletions.
44 changes: 37 additions & 7 deletions Libraries/Opc.Ua.Client/DefaultSessionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -39,9 +40,21 @@ namespace Opc.Ua.Client
/// </summary>
public class DefaultSessionFactory : ISessionFactory
{
/// <summary>
/// The default instance of the factory.
/// </summary>
public static readonly DefaultSessionFactory Instance = new DefaultSessionFactory();

/// <summary>
/// Force use of the default instance.
/// </summary>
protected DefaultSessionFactory()
{
}

#region Public Methods
/// <inheritdoc/>
public async Task<ISession> CreateAsync(
public async virtual Task<ISession> CreateAsync(
ApplicationConfiguration configuration,
ConfiguredEndpoint endpoint,
bool updateBeforeConnect,
Expand All @@ -55,7 +68,7 @@ public async Task<ISession> CreateAsync(
}

/// <inheritdoc/>
public async Task<ISession> CreateAsync(
public async virtual Task<ISession> CreateAsync(
ApplicationConfiguration configuration,
ConfiguredEndpoint endpoint,
bool updateBeforeConnect,
Expand All @@ -71,7 +84,7 @@ public async Task<ISession> CreateAsync(
}

/// <inheritdoc/>
public async Task<ISession> CreateAsync(
public async virtual Task<ISession> CreateAsync(
ApplicationConfiguration configuration,
ITransportWaitingConnection connection,
ConfiguredEndpoint endpoint,
Expand All @@ -89,7 +102,7 @@ public async Task<ISession> CreateAsync(
}

/// <inheritdoc/>
public async Task<ISession> CreateAsync(
public async virtual Task<ISession> CreateAsync(
ApplicationConfiguration configuration,
ReverseConnectManager reverseConnectManager,
ConfiguredEndpoint endpoint,
Expand All @@ -102,7 +115,6 @@ public async Task<ISession> CreateAsync(
CancellationToken ct = default
)
{

if (reverseConnectManager == null)
{
return await CreateAsync(configuration, endpoint, updateBeforeConnect,
Expand Down Expand Up @@ -141,7 +153,25 @@ await endpoint.UpdateFromServerAsync(
}

/// <inheritdoc/>
public Task<ISession> RecreateAsync(ISession sessionTemplate)
public virtual ISession Create(
ApplicationConfiguration configuration,
ITransportChannel channel,
ConfiguredEndpoint endpoint,
X509Certificate2 clientCertificate,
EndpointDescriptionCollection availableEndpoints = null,
StringCollection discoveryProfileUris = null)
{
return Session.Create(configuration, channel, endpoint, clientCertificate, availableEndpoints, discoveryProfileUris);
}

/// <inheritdoc/>
public virtual Task<ITransportChannel> CreateChannelAsync(ApplicationConfiguration configuration, ITransportWaitingConnection connection, ConfiguredEndpoint endpoint, bool updateBeforeConnect, bool checkDomain)
{
return Session.CreateChannelAsync(configuration, connection, endpoint, updateBeforeConnect, checkDomain);
}

/// <inheritdoc/>
public virtual Task<ISession> RecreateAsync(ISession sessionTemplate)
{
if (!(sessionTemplate is Session template))
{
Expand All @@ -152,7 +182,7 @@ public Task<ISession> RecreateAsync(ISession sessionTemplate)
}

/// <inheritdoc/>
public Task<ISession> RecreateAsync(ISession sessionTemplate, ITransportWaitingConnection connection)
public virtual Task<ISession> RecreateAsync(ISession sessionTemplate, ITransportWaitingConnection connection)
{
if (!(sessionTemplate is Session template))
{
Expand Down
34 changes: 34 additions & 0 deletions Libraries/Opc.Ua.Client/ISessionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* ======================================================================*/

using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -131,6 +132,39 @@ Task<ISession> CreateAsync(
IList<string> preferredLocales,
CancellationToken ct = default);

/// <summary>
/// Creates a secure channel to the specified endpoint.
/// </summary>
/// <param name="configuration">The application configuration.</param>
/// <param name="connection">The client endpoint for the reverse connect.</param>
/// <param name="endpoint">A configured endpoint to connect to.</param>
/// <param name="updateBeforeConnect">Update configuration based on server prior connect.</param>
/// <param name="checkDomain">Check that the certificate specifies a valid domain (computer) name.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task<ITransportChannel> CreateChannelAsync(
ApplicationConfiguration configuration,
ITransportWaitingConnection connection,
ConfiguredEndpoint endpoint,
bool updateBeforeConnect,
bool checkDomain);

/// <summary>
/// Creates a new session with a server using the specified channel by invoking the CreateSession service.
/// </summary>
/// <param name="configuration">The configuration for the client application.</param>
/// <param name="channel">The channel for the server.</param>
/// <param name="endpoint">The endpoint for the server.</param>
/// <param name="clientCertificate">The certificate to use for the client.</param>
/// <param name="availableEndpoints">The list of available endpoints returned by server in GetEndpoints() response.</param>
/// <param name="discoveryProfileUris">The value of profileUris used in GetEndpoints() request.</param>
ISession Create(
ApplicationConfiguration configuration,
ITransportChannel channel,
ConfiguredEndpoint endpoint,
X509Certificate2 clientCertificate,
EndpointDescriptionCollection availableEndpoints = null,
StringCollection discoveryProfileUris = null);

/// <summary>
/// Recreates a session based on a specified template.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Opc.Ua.Client/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ private void Initialize(
/// </summary>
private void Initialize()
{
m_sessionFactory = new DefaultSessionFactory();
m_sessionFactory = DefaultSessionFactory.Instance;
m_sessionTimeout = 0;
m_namespaceUris = new NamespaceTable();
m_serverUris = new StringTable();
Expand Down
Loading

0 comments on commit cfb3ac4

Please sign in to comment.