public void ConfigureServices(IServiceCollection services) { var traceConfig = TraceConfiguration.Create(GetSampleRate(), GetBufferOptions()); services.AddGoogleTrace(_projectId, traceConfig); services.AddMvc(); }
/// <summary> /// Adds the needed services for Google Cloud Tracing. Used with <see cref="UseGoogleTrace"/>. /// </summary> /// <param name="services">The service collection. Cannot be null.</param> /// <param name="projectId">Optional if running on Google App Engine or Google Compute Engine. /// The Google Cloud Platform project ID. If unspecified and running on GAE or GCE the project ID will be /// detected from the platform.</param> /// <param name="config">Optional trace configuration, if unset the default will be used.</param> /// <param name="client">Optional Trace client, if unset the default will be used.</param> /// <param name="traceFallbackPredicate">Optional function to trace requests. If the trace header is not set /// then this function will be called to determine if a given request should be traced. This will /// not override trace headers. If the function returns true the request will be traced, if false /// is returned the trace will not be traced and if null is returned it will not affect the /// trace decision.</param> public static void AddGoogleTrace( this IServiceCollection services, string projectId = null, TraceConfiguration config = null, TraceServiceClient client = null, Func <HttpRequest, bool?> traceFallbackPredicate = null) { GaxPreconditions.CheckNotNull(services, nameof(services)); client = client ?? TraceServiceClient.Create(); config = config ?? TraceConfiguration.Create(); projectId = CommonUtils.GetAndCheckProjectId(projectId); var consumer = ConsumerFactory <TraceProto> .GetConsumer( new GrpcTraceConsumer(client), MessageSizer <TraceProto> .GetSize, config.BufferOptions); var tracerFactory = new ManagedTracerFactory(projectId, consumer, RateLimitingTraceOptionsFactory.Create(config), TraceIdFactory.Create()); services.AddScoped(CreateTraceHeaderContext); services.AddSingleton <IManagedTracerFactory>(tracerFactory); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton(CreateManagedTracer); services.AddSingleton(CreateTraceHeaderPropagatingHandler); services.AddSingleton(new ShouldTraceRequest(traceFallbackPredicate)); }
// [START configure_services_trace] public override void Init() { // [START_EXCLUDE] string projectId = ConfigurationManager.AppSettings["projectId"]; // Confirm that projectId has been changed from placeholder value. if (projectId == ("YOUR-PROJECT-ID")) { throw new Exception("Update Web.config and replace " + "YOUR-PROJECT-ID with your project id, and recompile."); } // [END_EXCLUDE] base.Init(); TraceConfiguration traceConfig = TraceConfiguration .Create(bufferOptions: BufferOptions.NoBuffer()); CloudTrace.Initialize(this, projectId, traceConfig); }
/// <summary> /// Adds the needed services for Google Cloud Tracing. Used with <see cref="UseGoogleTrace"/>. /// </summary> /// <param name="services">The service collection. Cannot be null.</param> /// <param name="projectId">The Google Cloud Platform project ID. Cannot be null.</param> /// <param name="config">Optional trace configuration, if unset the default will be used.</param> /// <param name="clientTask">Optional task which produces the Trace client, if /// unset the default will be used.</param> public static void AddGoogleTrace( this IServiceCollection services, string projectId, TraceConfiguration config = null, Task <TraceServiceClient> clientTask = null) { GaxPreconditions.CheckNotNull(services, nameof(services)); GaxPreconditions.CheckNotNull(projectId, nameof(projectId)); clientTask = clientTask ?? TraceServiceClient.CreateAsync(); config = config ?? TraceConfiguration.Create(); IConsumer <TraceProto> consumer = ConsumerFactory <TraceProto> .GetConsumer( new GrpcTraceConsumer(clientTask), MessageSizer <TraceProto> .GetSize, config.BufferOptions); var tracerFactory = new ManagedTracerFactory(projectId, consumer, RateLimitingTraceOptionsFactory.Create(config), TraceIdFactory.Create()); services.AddSingleton <IManagedTracerFactory>(tracerFactory); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddScoped(CreateTraceHeaderContext); services.AddScoped(CreateManagedTracer); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // [START configure_services_logging] // [START configure_services_error_reporting] services.AddOptions(); services.Configure <StackdriverOptions>( Configuration.GetSection("Stackdriver")); // [END configure_services_logging] services.AddGoogleExceptionLogging( Configuration["Stackdriver:ProjectId"], Configuration["Stackdriver:ServiceName"], Configuration["Stackdriver:Version"]); // [END configure_services_error_reporting] // [START configure_services_trace] // Add trace service. TraceConfiguration traceConfig = TraceConfiguration.Create(bufferOptions: BufferOptions.NoBuffer()); services.AddGoogleTrace(Configuration["Stackdriver:ProjectId"], traceConfig); // [END configure_services_trace] // Add framework services. services.AddMvc(); }