public Task DisposeAsync()
        {
            Cluster?.Dispose();
            _tracerProvider.Dispose();

            return(Task.CompletedTask);
        }
Пример #2
0
    private static void OnExit(object sender, EventArgs e)
    {
        if (Interlocked.Exchange(ref _isExiting, value: 1) != 0)
        {
            // OnExit() was already called before
            return;
        }

        try
        {
            _tracerProvider?.Dispose();
            _meterProvider?.Dispose();
            _sdkEventListener.Dispose();

            Logger.Information("OpenTelemetry Automatic Instrumentation exit.");
        }
        catch (Exception ex)
        {
            try
            {
                Logger.Error(ex, "An error occured while attempting to exit.");
            }
            catch
            {
                // If we encounter an error while logging there is nothing else we can do
                // with the exception.
            }
        }
    }
Пример #3
0
        public async Task IncomingRequestInstrumentationTest(
            bool instrument,
            bool filter               = false,
            bool includeVersion       = false,
            bool enrich               = false,
            bool enrichmentExcecption = false)
        {
            List <Activity> stoppedActivities = new List <Activity>();

            using ActivityListener activityListener = new ActivityListener
                  {
                      ShouldListenTo  = activitySource => true,
                      ActivityStopped = activity => stoppedActivities.Add(activity),
                  };

            ActivitySource.AddActivityListener(activityListener);

            TracerProvider tracerProvider = null;

            if (instrument)
            {
                tracerProvider = Sdk.CreateTracerProviderBuilder()
                                 .AddWcfInstrumentation(options =>
                {
                    if (enrich)
                    {
                        if (!enrichmentExcecption)
                        {
                            options.Enrich = (activity, eventName, message) =>
                            {
                                switch (eventName)
                                {
                                case WcfEnrichEventNames.AfterReceiveRequest:
                                    activity.SetTag(
                                        "server.afterreceiverequest",
                                        WcfEnrichEventNames.AfterReceiveRequest);
                                    break;

                                case WcfEnrichEventNames.BeforeSendReply:
                                    activity.SetTag(
                                        "server.beforesendreply",
                                        WcfEnrichEventNames.BeforeSendReply);
                                    break;
                                }
                            };
                        }
                        else
                        {
                            options.Enrich = (activity, eventName, message) =>
                            {
                                throw new Exception("Failure whilst enriching activity");
                            };
                        }
                    }

                    options.IncomingRequestFilter = (Message m) =>
                    {
                        return(!filter);
                    };
                    options.SetSoapMessageVersion = includeVersion;
                })
                                 .Build();
            }

            ServiceClient client = new ServiceClient(
                new NetTcpBinding(),
                new EndpointAddress(new Uri(this.serviceBaseUri, "/Service")));

            try
            {
                var response = await client.ExecuteAsync(
                    new ServiceRequest
                {
                    Payload = "Hello Open Telemetry!",
                }).ConfigureAwait(false);
            }
            finally
            {
                if (client.State == CommunicationState.Faulted)
                {
                    client.Abort();
                }
                else
                {
                    client.Close();
                }

                tracerProvider?.Shutdown();
                tracerProvider?.Dispose();

                WcfInstrumentationActivitySource.Options = null;
            }

            if (instrument && !filter)
            {
                Assert.NotEmpty(stoppedActivities);
                Assert.Single(stoppedActivities);

                Activity activity = stoppedActivities[0];
                Assert.Equal(WcfInstrumentationActivitySource.IncomingRequestActivityName, activity.OperationName);
                Assert.Equal(WcfInstrumentationConstants.WcfSystemValue, activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.RpcSystemTag).Value);
                Assert.Equal("http://opentelemetry.io/Service", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.RpcServiceTag).Value);
                Assert.Equal("Execute", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.RpcMethodTag).Value);
                Assert.Equal(this.serviceBaseUri.Host, activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.NetHostNameTag).Value);
                Assert.Equal(this.serviceBaseUri.Port, activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.NetHostPortTag).Value);
                Assert.Equal("net.tcp", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.WcfChannelSchemeTag).Value);
                Assert.Equal("/Service", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.WcfChannelPathTag).Value);
                Assert.Equal("http://opentelemetry.io/Service/ExecuteResponse", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.SoapReplyActionTag).Value);
                if (includeVersion)
                {
                    Assert.Equal("Soap12 (http://www.w3.org/2003/05/soap-envelope) Addressing10 (http://www.w3.org/2005/08/addressing)", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.SoapMessageVersionTag).Value);
                }

                if (enrich && !enrichmentExcecption)
                {
                    Assert.Equal(WcfEnrichEventNames.AfterReceiveRequest, activity.TagObjects.Single(t => t.Key == "server.afterreceiverequest").Value);
                    Assert.Equal(WcfEnrichEventNames.BeforeSendReply, activity.TagObjects.Single(t => t.Key == "server.beforesendreply").Value);
                }
            }
            else
            {
                Assert.Empty(stoppedActivities);
            }
        }
        public void IncomingRequestOneWayOperationInstrumentationTest()
        {
            List <Activity> stoppedActivities = new List <Activity>();

            using ActivityListener activityListener = new ActivityListener
                  {
                      ShouldListenTo  = activitySource => true,
                      ActivityStopped = activity => stoppedActivities.Add(activity),
                  };

            ActivitySource.AddActivityListener(activityListener);
            TracerProvider tracerProvider = Sdk.CreateTracerProviderBuilder()
                                            .AddWcfInstrumentation()
                                            .Build();

            ServiceClient client = new ServiceClient(
                new NetTcpBinding(),
                new EndpointAddress(new Uri(this.serviceBaseUri, "/Service")));

            try
            {
                client.ExecuteWithOneWay(new ServiceRequest());
                this.thrownExceptionsHandle.WaitOne(3000);
            }
            finally
            {
                if (client.State == CommunicationState.Faulted)
                {
                    client.Abort();
                }
                else
                {
                    client.Close();
                }

                tracerProvider?.Shutdown();
                tracerProvider?.Dispose();

                WcfInstrumentationActivitySource.Options = null;
            }

            // Assert
            Assert.Empty(this.thrownExceptions);

            Assert.NotEmpty(stoppedActivities);
            Assert.Single(stoppedActivities);

            Activity activity = stoppedActivities[0];

            Assert.Equal("http://opentelemetry.io/Service/ExecuteWithOneWay", activity.DisplayName);
            Assert.Equal("ExecuteWithOneWay", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.RpcMethodTag).Value);
            Assert.DoesNotContain(activity.TagObjects, t => t.Key == WcfInstrumentationConstants.SoapReplyActionTag);

            Assert.Equal(WcfInstrumentationActivitySource.IncomingRequestActivityName, activity.OperationName);
            Assert.Equal(WcfInstrumentationConstants.WcfSystemValue, activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.RpcSystemTag).Value);
            Assert.Equal("http://opentelemetry.io/Service", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.RpcServiceTag).Value);
            Assert.Equal(this.serviceBaseUri.Host, activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.NetHostNameTag).Value);
            Assert.Equal(this.serviceBaseUri.Port, activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.NetHostPortTag).Value);
            Assert.Equal("net.tcp", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.WcfChannelSchemeTag).Value);
            Assert.Equal("/Service", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.WcfChannelPathTag).Value);
        }
        public async Task OutgoingRequestInstrumentationTest(
            bool instrument,
            bool filter = false,
            bool suppressDownstreamInstrumentation = true,
            bool includeVersion = false)
        {
#if NETFRAMEWORK
            const string OutgoingHttpOperationName = "OpenTelemetry.HttpWebRequest.HttpRequestOut";
#else
            const string OutgoingHttpOperationName = "System.Net.Http.HttpRequestOut";
#endif
            List <Activity> stoppedActivities = new List <Activity>();

            var builder = Sdk.CreateTracerProviderBuilder()
                          .AddInMemoryExporter(stoppedActivities);

            if (instrument)
            {
                builder
                .AddWcfInstrumentation(options =>
                {
                    options.OutgoingRequestFilter = (Message m) =>
                    {
                        return(!filter);
                    };
                    options.SuppressDownstreamInstrumentation = suppressDownstreamInstrumentation;
                    options.SetSoapMessageVersion             = includeVersion;
                })
                .AddHttpClientInstrumentation();     // <- Added to test SuppressDownstreamInstrumentation.
            }

            TracerProvider tracerProvider = builder.Build();

            ServiceClient client = new ServiceClient(
                new BasicHttpBinding(BasicHttpSecurityMode.None),
                new EndpointAddress(new Uri(this.serviceBaseUri, "/Service")));
            try
            {
                client.Endpoint.EndpointBehaviors.Add(new TelemetryEndpointBehavior());

                var response = await client.ExecuteAsync(
                    new ServiceRequest
                {
                    Payload = "Hello Open Telemetry!",
                }).ConfigureAwait(false);
            }
            finally
            {
                if (client.State == CommunicationState.Faulted)
                {
                    client.Abort();
                }
                else
                {
                    client.Close();
                }

                tracerProvider.Shutdown();
                tracerProvider.Dispose();

                WcfInstrumentationActivitySource.Options = null;
            }

            if (instrument)
            {
                if (!suppressDownstreamInstrumentation)
                {
                    Assert.NotEmpty(stoppedActivities);
                    if (filter)
                    {
                        Assert.Single(stoppedActivities);
                        Assert.Equal(OutgoingHttpOperationName, stoppedActivities[0].OperationName);
                    }
                    else
                    {
                        Assert.Equal(2, stoppedActivities.Count);
                        Assert.Equal(OutgoingHttpOperationName, stoppedActivities[0].OperationName);
                        Assert.Equal(WcfInstrumentationActivitySource.OutgoingRequestActivityName, stoppedActivities[1].OperationName);
                    }
                }
                else
                {
                    if (!filter)
                    {
                        Assert.NotEmpty(stoppedActivities);
                        Assert.Single(stoppedActivities);

                        Activity activity = stoppedActivities[0];
                        Assert.Equal(WcfInstrumentationActivitySource.OutgoingRequestActivityName, activity.OperationName);
                        Assert.Equal(WcfInstrumentationConstants.WcfSystemValue, activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.RpcSystemTag).Value);
                        Assert.Equal("http://opentelemetry.io/Service", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.RpcServiceTag).Value);
                        Assert.Equal("Execute", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.RpcMethodTag).Value);
                        Assert.Equal(this.serviceBaseUri.Host, activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.NetPeerNameTag).Value);
                        Assert.Equal(this.serviceBaseUri.Port, activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.NetPeerPortTag).Value);
                        Assert.Equal("http", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.WcfChannelSchemeTag).Value);
                        Assert.Equal("/Service", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.WcfChannelPathTag).Value);
                        if (includeVersion)
                        {
                            Assert.Equal("Soap11 (http://schemas.xmlsoap.org/soap/envelope/) AddressingNone (http://schemas.microsoft.com/ws/2005/05/addressing/none)", activity.TagObjects.FirstOrDefault(t => t.Key == WcfInstrumentationConstants.SoapMessageVersionTag).Value);
                        }
                    }
                    else
                    {
                        Assert.Empty(stoppedActivities);
                    }
                }
            }
            else
            {
                Assert.Empty(stoppedActivities);
            }
        }