Example #1
0
            // End sample

            // Sample: UseTracerRunIn
            /// <summary>
            /// The <see cref="IManagedTracer"/> is populated by dependency injection.
            /// </summary>
            public void TraceHelloWorldRunIn(IManagedTracer tracer)
            {
                // Manually trace a specific Action or Func<T>.
                tracer.RunInSpan(
                    () => Console.Out.WriteLine("Hello, World!"),
                    nameof(TraceHelloWorldRunIn));
            }
Example #2
0
        public async Task TraceSingleAsync(Func <IHostBuilder> createHostBuilder)
        {
            IHost host = null;

            try
            {
                host = createHostBuilder().Build();
                await host.StartAsync();

                ITraceContext  traceContext  = new SimpleTraceContext(null, null, true);
                var            tracerFactory = host.Services.GetRequiredService <Func <ITraceContext, IManagedTracer> >();
                IManagedTracer tracer        = tracerFactory(traceContext);
                ContextTracerManager.SetCurrentTracer(tracer);

                using (tracer.StartSpan(_testId))
                {
                    IManagedTracer currentTracer = host.Services.GetRequiredService <IManagedTracer>();
                    currentTracer.RunInSpan(
                        () => Console.WriteLine("Using Cloud Trace from a non ASP.NET Core app"),
                        "testing_tracing");
                }

                var trace = TraceEntryPolling.Default.GetTrace(_testId, _startTime);
                TraceEntryVerifiers.AssertParentChildSpan(trace, _testId, "testing_tracing");
            }
            finally
            {
                if (host is object)
                {
                    await host.StopAsync();
                }
            }
        }
        // End sample

        // Sample: UseTracerRunIn
        /// <summary>
        /// Manually trace a specific Action or Func<T>.
        /// The <see cref="IManagedTracer"/> is populated by dependency injection
        /// thanks to the use of the <see cref="FromServicesAttribute"/> attribute.
        /// </summary>
        public void TraceHelloWorldRunIn(string id, [FromServices] IManagedTracer tracer)
        {
            tracer.RunInSpan(
                // The Action or Func<T> to be traced.
                () =>
            {
                // The code whose execution is to be included in the span goes here.
                ViewData["Message"] = "Hello World.";
            },
                // The name of the span.
                id);
        }
        public async Task TraceSingleAsync()
        {
            IHost host = null;

            try
            {
                host = TroubleshootingHostBuilder.CreateHostBuilder().Build();
                await host.StartAsync();

                // Sample: SingleContext
                ITraceContext  traceContext  = new SimpleTraceContext(null, null, true);
                var            tracerFactory = host.Services.GetRequiredService <Func <ITraceContext, IManagedTracer> >();
                IManagedTracer tracer        = tracerFactory(traceContext);
                ContextTracerManager.SetCurrentTracer(tracer);
                // End sample

                // Let's just start a span with the test ID so we can find it faster.
                // But we don't show this in sample code.
                using (tracer.StartSpan(_testId))
                {
                    // Sample: RunIn
                    IManagedTracer currentTracer = host.Services.GetRequiredService <IManagedTracer>();
                    currentTracer.RunInSpan(
                        () => Console.WriteLine("Using Cloud Trace from a non ASP.NET Core app"),
                        "testing_tracing");
                    // End sample
                }

                var trace = TraceEntryPolling.Default.GetTrace(_testId, _startTime);
                TraceEntryVerifiers.AssertParentChildSpan(trace, _testId, "testing_tracing");
            }
            finally
            {
                if (host is object)
                {
                    await host.StopAsync();
                }
            }
        }