Ejemplo n.º 1
0
        /// <summary>
        /// Configures Google Cloud Trace for dependency injection.
        /// </summary>
        public static IServiceCollection AddGoogleTrace(this IServiceCollection services, TraceServiceOptions options = null)
        {
            GaxPreconditions.CheckNotNull(services, nameof(services));

            var client       = options?.Client ?? TraceServiceClient.Create();
            var traceOptions = options?.Options ?? TraceOptions.Create();
            var projectId    = Project.GetAndCheckProjectId(options?.ProjectId);

            var consumer      = ManagedTracer.CreateConsumer(client, traceOptions);
            var tracerFactory = ManagedTracer.CreateFactory(projectId, consumer, traceOptions);

            services.AddSingleton(tracerFactory);
            services.AddSingleton(ManagedTracer.CreateDelegatingTracer(ContextTracerManager.GetCurrentTracer));

            // On .Net Standard 2.0 or higher, we can use the System.Net.Http.IHttpClientFactory defined in Microsoft.Extensions.Http,
            // for which we need a DelegatingHandler with no InnerHandler set.
            // we register factories for the UnchainedTraceHeaderPropagatingHandler.
#pragma warning disable CS0618 // Type or member is obsolete
            // This factory we register for backwards compatibility only.
            // We can remove it on our next major version as we are making UnchainedTraceHeaderPropagatingHandler
            // obsolete as well.
            services.AddTransient(UnchainedTraceHeaderPropagatingHandlerFactory);
#pragma warning restore CS0618 // Type or member is obsolete
            // This is the new factory, that takes trace custom labels into account.
            return(services.AddSingleton <OutgoingGoogleTraceHandlerFactory>());
        }
        /// <summary>
        /// Configures Google Cloud Trace for dependency injection.
        /// </summary>
        public static IServiceCollection AddGoogleTrace(
            this IServiceCollection services, Action <TraceServiceOptions> setupAction)
        {
            GaxPreconditions.CheckNotNull(services, nameof(services));
            GaxPreconditions.CheckNotNull(setupAction, nameof(setupAction));

            var serviceOptions = new TraceServiceOptions();

            setupAction(serviceOptions);

            var client    = serviceOptions.Client ?? TraceServiceClient.Create();
            var options   = serviceOptions.Options ?? TraceOptions.Create();
            var projectId = Project.GetAndCheckProjectId(serviceOptions.ProjectId);

            var consumer      = ManagedTracer.CreateConsumer(client, options);
            var tracerFactory = ManagedTracer.CreateFactory(projectId, consumer, options);

            services.AddSingleton(tracerFactory);
            services.AddSingleton(ManagedTracer.CreateDelegatingTracer(ContextTracerManager.GetCurrentTracer));

            // On .Net Standard 2.0 or higher, we can use the System.Net.Http.IHttpClientFactory defined in Microsoft.Extensions.Http,
            // for which we need a DelegatingHandler with no InnerHandler set. This is the recommended way.
            // It should be registered as follows.
            return(services.AddTransient(UnchainedTraceHeaderPropagatingHandlerFactory));
        }