/// <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="setupAction">Action to set up options. Cannot be null.</param> /// <remarks> /// If <see cref="RetryOptions.ExceptionHandling"/> is set to <see cref="ExceptionHandling.Propagate"/> /// and the <see cref="RequestDelegate"/> executed by this middleware throws ad exception and this /// diagnostics library also throws an exception trying to report it and <see cref="AggregateException"/> /// with both exceptions will be thrown. Otherwise only the exception from the <see cref="RequestDelegate"/> /// will be thrown. /// </remarks> 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 traceFallbackPredicate = serviceOptions.TraceFallbackPredicate ?? TraceDecisionPredicate.Default; var projectId = Project.GetAndCheckProjectId(serviceOptions.ProjectId); var consumer = ManagedTracer.CreateConsumer(client, options); var tracerFactory = ManagedTracer.CreateTracerFactory(projectId, consumer, options); services.AddScoped(CreateTraceHeaderContext); services.AddSingleton(tracerFactory); // Only add the HttpContextAccessor if it's not already added. // This is needed to get the `TraceHeaderContext`. See `CreateTraceHeaderContext`. services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton(ManagedTracer.CreateDelegatingTracer(() => ContextTracerManager.GetCurrentTracer())); services.AddSingleton(new TraceHeaderPropagatingHandler(() => ContextTracerManager.GetCurrentTracer())); return(services.AddSingleton(traceFallbackPredicate)); }
/// <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)); }
public TraceTest() { _projectId = Utils.GetProjectIdFromEnvironment(); _testId = Utils.GetTestId(); _startTime = Timestamp.FromDateTime(DateTime.UtcNow); _client = TraceServiceClient.Create(); }
/// <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="setupAction">Action to set up options. Cannot be null.</param> /// <remarks> /// If <see cref="RetryOptions.ExceptionHandling"/> is set to <see cref="ExceptionHandling.Propagate"/> /// and the <see cref="RequestDelegate"/> executed by this middleware throws ad exception and this /// diagnostics library also throws an exception trying to report it and <see cref="AggregateException"/> /// with both exceptions will be thrown. Otherwise only the exception from the <see cref="RequestDelegate"/> /// will be thrown. /// </remarks> public static void 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 traceFallbackPredicate = serviceOptions.TraceFallbackPredicate ?? TraceDecisionPredicate.Default; var projectId = Project.GetAndCheckProjectId(serviceOptions.ProjectId); var consumer = ManagedTracer.CreateConsumer(client, options); var tracerFactory = ManagedTracer.CreateTracerFactory(projectId, consumer, options); services.AddScoped(CreateTraceHeaderContext); services.AddSingleton <Func <TraceHeaderContext, IManagedTracer> >(tracerFactory); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton(CreateManagedTracer); services.AddSingleton(CreateTraceHeaderPropagatingHandler); services.AddSingleton(traceFallbackPredicate); }
/// <summary> /// Adds the needed services for Google Cloud Tracing. Used with <see cref="UseGoogleTrace"/>. /// </summary> /// <param name="services">The service collection. Must not be null.</param> /// <param name="setupAction">Action to set up options. Must not be null.</param> /// <remarks> /// If <see cref="RetryOptions.ExceptionHandling"/> is set to <see cref="ExceptionHandling.Propagate"/> /// and the <see cref="RequestDelegate"/> executed by this middleware throws ad exception and this /// diagnostics library also throws an exception trying to report it and <see cref="AggregateException"/> /// with both exceptions will be thrown. Otherwise only the exception from the <see cref="RequestDelegate"/> /// will be thrown. /// </remarks> 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 traceFallbackPredicate = serviceOptions.TraceFallbackPredicate ?? TraceDecisionPredicate.Default; var projectId = Project.GetAndCheckProjectId(serviceOptions.ProjectId); var consumer = ManagedTracer.CreateConsumer(client, options); var tracerFactory = ManagedTracer.CreateTracerFactory(projectId, consumer, options); services.AddScoped(CreateTraceHeaderContext); services.AddSingleton(tracerFactory); services.AddHttpContextAccessor(); 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 DelagatingHandler with no InnerHandler set. This is the recommended way. // It should be registered as follows. services.AddTransient(sp => new UnchainedTraceHeaderPropagatingHandler(ContextTracerManager.GetCurrentTracer)); // This is to be used for explicitly creating an HttpClient instance. Valid for all platforms. services.AddSingleton(new TraceHeaderPropagatingHandler(ContextTracerManager.GetCurrentTracer)); return(services.AddSingleton(traceFallbackPredicate)); }
/// <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="setupAction">Action to set up options. Cannot be null.</param> public static void 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 traceFallbackPredicate = serviceOptions.TraceFallbackPredicate ?? TraceDecisionPredicate.Default; var projectId = CommonUtils.GetAndCheckProjectId(serviceOptions.ProjectId); var consumer = ConsumerFactory <TraceProto> .GetConsumer( new GrpcTraceConsumer(client), MessageSizer <TraceProto> .GetSize, options.BufferOptions, options.RetryOptions); var tracerFactory = new ManagedTracerFactory(projectId, consumer, RateLimitingTraceOptionsFactory.Create(options), TraceIdFactory.Create()); services.AddScoped(CreateTraceHeaderContext); services.AddSingleton <IManagedTracerFactory>(tracerFactory); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton(CreateManagedTracer); services.AddSingleton(CreateTraceHeaderPropagatingHandler); services.AddSingleton(traceFallbackPredicate); }
/// <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)); }
/// <summary>Snippet for CreateSpan</summary> public void CreateSpanRequestObject() { // Snippet: CreateSpan(Span, CallSettings) // Create client TraceServiceClient traceServiceClient = TraceServiceClient.Create(); // Initialize request argument(s) Span request = new Span { SpanName = SpanName.FromProjectTraceSpan("[PROJECT]", "[TRACE]", "[SPAN]"), SpanId = "", ParentSpanId = "", DisplayName = new TruncatableString(), StartTime = new Timestamp(), EndTime = new Timestamp(), Attributes = new Span.Types.Attributes(), StackTrace = new StackTrace(), TimeEvents = new Span.Types.TimeEvents(), Links = new Span.Types.Links(), Status = new Status(), SameProcessAsParentSpan = false, ChildSpanCount = 0, }; // Make the request Span response = traceServiceClient.CreateSpan(request); // End snippet }
public static int Main(string[] args) { // Read projectId from args if (args.Length != 1) { Console.WriteLine("Usage: Project ID must be passed as first argument."); Console.WriteLine(); return(1); } string projectId = args[0]; // Create client TraceServiceClient client = TraceServiceClient.Create(); // Initialize request argument(s) ProjectName name = new ProjectName(projectId); IEnumerable <Span> spans = new List <Span>(); // Call API method client.BatchWriteSpans(name, spans); // Success Console.WriteLine("Smoke test passed OK"); return(0); }
/// <summary> /// Creates a <see cref="SimpleManagedTracer"/> with a <see cref="GrpcTraceConsumer"/>. /// </summary> private IManagedTracer CreateTracer() { string traceId = _traceIdFactory.NextId(); var consumer = new GrpcTraceConsumer(TraceServiceClient.Create()); return(SimpleManagedTracer.Create(consumer, _projectId, traceId, null)); }
public async Task ListTracesAsync() { // Snippet: ListTracesAsync(string,string,int?,CallSettings) // Create client TraceServiceClient traceServiceClient = TraceServiceClient.Create(); // Initialize request argument(s) string projectId = ""; // Make the request IPagedAsyncEnumerable <ListTracesResponse, Trace> response = traceServiceClient.ListTracesAsync(projectId); // Iterate over all response items, lazily performing RPCs as required await response.ForEachAsync((Trace item) => { // Do something with each item Console.WriteLine(item); }); // Or iterate over fixed-sized pages, lazily performing RPCs as required int pageSize = 10; IAsyncEnumerable <FixedSizePage <Trace> > fixedSizePages = response.AsPages().WithFixedSize(pageSize); await fixedSizePages.ForEachAsync((FixedSizePage <Trace> page) => { // Do something with each page of items Console.WriteLine("A page of results:"); foreach (Trace item in page) { Console.WriteLine(item); } }); // End snippet }
public static int Main(string[] args) { // Read projectId from args if (args.Length != 1) { Console.WriteLine("Usage: Project ID must be passed as first argument."); Console.WriteLine(); return(1); } string projectId = args[0]; // Create client TraceServiceClient client = TraceServiceClient.Create(); // Initialize request argument(s) string projectId2 = projectId; // Call API method PagedEnumerable <ListTracesResponse, Trace> pagedResponse = client.ListTraces(projectId2); // Show the result foreach (var item in pagedResponse) { Console.WriteLine(item); } // Success Console.WriteLine("Smoke test passed OK"); return(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>()); }
public SimpleManagedTracerTest() { _testId = IdGenerator.FromDateTime(); _grpcConsumer = new GrpcTraceConsumer(TraceServiceClient.Create()); _startTime = DateTimeOffset.UtcNow; }
public TraceHeaderPropagatingHandlerTest() { _testId = IdGenerator.FromDateTime(); _consumer = new GrpcTraceConsumer(TraceServiceClient.Create()); _tracer = SimpleManagedTracer.Create(_consumer, TestEnvironment.GetTestProjectId(), _traceIdFactory.NextId(), null); _startTime = DateTimeOffset.UtcNow; }
/// <summary>Snippet for GetTrace</summary> public void GetTrace() { // Snippet: GetTrace(string, string, CallSettings) // Create client TraceServiceClient traceServiceClient = TraceServiceClient.Create(); // Initialize request argument(s) string projectId = ""; string traceId = ""; // Make the request Trace response = traceServiceClient.GetTrace(projectId, traceId); // End snippet }
public async Task ExportAsync(IEnumerable <ISpanData> spanDataList) { TraceServiceClient traceWriter = TraceServiceClient.Create(settings: traceServiceSettings); var batchSpansRequest = new BatchWriteSpansRequest { ProjectName = googleCloudProjectId, Spans = { spanDataList.Select(s => s.ToSpan(googleCloudProjectId.ProjectId)) }, }; await traceWriter.BatchWriteSpansAsync(batchSpansRequest); }
/// <summary>Snippet for ListTraces</summary> public void ListTracesRequestObject() { // Snippet: ListTraces(ListTracesRequest, CallSettings) // Create client TraceServiceClient traceServiceClient = TraceServiceClient.Create(); // Initialize request argument(s) ListTracesRequest request = new ListTracesRequest { ProjectId = "", View = ListTracesRequest.Types.ViewType.Unspecified, StartTime = new Timestamp(), EndTime = new Timestamp(), Filter = "", OrderBy = "", }; // Make the request PagedEnumerable <ListTracesResponse, Trace> response = traceServiceClient.ListTraces(request); // Iterate over all response items, lazily performing RPCs as required foreach (Trace item in response) { // Do something with each item Console.WriteLine(item); } // Or iterate over pages (of server-defined size), performing one RPC per page foreach (ListTracesResponse page in response.AsRawResponses()) { // Do something with each page of items Console.WriteLine("A page of results:"); foreach (Trace item in page) { // Do something with each item Console.WriteLine(item); } } // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required int pageSize = 10; Page <Trace> singlePage = response.ReadPage(pageSize); // Do something with the page of items Console.WriteLine($"A page of {pageSize} results (unless it's the final page):"); foreach (Trace item in singlePage) { // Do something with each item Console.WriteLine(item); } // Store the pageToken, for when the next page is required. string nextPageToken = singlePage.NextPageToken; // End snippet }
/// <summary>Snippet for BatchWriteSpans</summary> public void BatchWriteSpansResourceNames() { // Snippet: BatchWriteSpans(ProjectName, IEnumerable<Span>, CallSettings) // Create client TraceServiceClient traceServiceClient = TraceServiceClient.Create(); // Initialize request argument(s) ProjectName name = ProjectName.FromProject("[PROJECT]"); IEnumerable <Span> spans = new Span[] { new Span(), }; // Make the request traceServiceClient.BatchWriteSpans(name, spans); // End snippet }
/// <summary>Snippet for BatchWriteSpans</summary> public void BatchWriteSpans() { // Snippet: BatchWriteSpans(string, IEnumerable<Span>, CallSettings) // Create client TraceServiceClient traceServiceClient = TraceServiceClient.Create(); // Initialize request argument(s) string name = "projects/[PROJECT]"; IEnumerable <Span> spans = new Span[] { new Span(), }; // Make the request traceServiceClient.BatchWriteSpans(name, spans); // End snippet }
/// <summary>Snippet for BatchWriteSpans</summary> public void BatchWriteSpans() { // Snippet: BatchWriteSpans(ProjectName,IEnumerable<apis::Span>,CallSettings) // Create client TraceServiceClient traceServiceClient = TraceServiceClient.Create(); // Initialize request argument(s) ProjectName name = new ProjectName("[PROJECT]"); IEnumerable <apis::Span> spans = new List <apis::Span>(); // Make the request traceServiceClient.BatchWriteSpans(name, spans); // End snippet }
/// <summary>Snippet for PatchTraces</summary> public void PatchTraces() { // Snippet: PatchTraces(string, Traces, CallSettings) // Create client TraceServiceClient traceServiceClient = TraceServiceClient.Create(); // Initialize request argument(s) string projectId = ""; Traces traces = new Traces(); // Make the request traceServiceClient.PatchTraces(projectId, traces); // End snippet }
private CloudTrace(string projectId, TraceOptions options = null, TraceServiceClient client = null, TraceDecisionPredicate traceFallbackPredicate = null) { GaxPreconditions.CheckNotNull(projectId, nameof(projectId)); // Create the default values if not set. client = client ?? TraceServiceClient.Create(); options = options ?? TraceOptions.Create(); _traceFallbackPredicate = traceFallbackPredicate ?? TraceDecisionPredicate.Default; _consumer = ManagedTracer.CreateConsumer(client, options); _tracerFactory = ManagedTracer.CreateTracerFactory(projectId, _consumer, options); }
private CloudTrace(string projectId, TraceConfiguration config = null, TraceServiceClient client = null) { GaxPreconditions.CheckNotNull(projectId, nameof(projectId)); // Create the default values if not set. client = client ?? TraceServiceClient.Create(); config = config ?? TraceConfiguration.Create(); _consumer = ConsumerFactory <TraceProto> .GetConsumer( new GrpcTraceConsumer(client), MessageSizer <TraceProto> .GetSize, config.BufferOptions); _tracerFactory = new ManagedTracerFactory(projectId, _consumer, RateLimitingTraceOptionsFactory.Create(config), TraceIdFactory.Create()); }
public async Task PatchTracesAsync() { // Snippet: PatchTracesAsync(string,Traces,CallSettings) // Additional: PatchTracesAsync(string,Traces,CancellationToken) // Create client TraceServiceClient traceServiceClient = TraceServiceClient.Create(); // Initialize request argument(s) string projectId = ""; Traces traces = new Traces(); // Make the request await traceServiceClient.PatchTracesAsync(projectId, traces); // End snippet }
public async Task GetTraceAsync() { // Snippet: GetTraceAsync(string,string,CallSettings) // Additional: GetTraceAsync(string,string,CancellationToken) // Create client TraceServiceClient traceServiceClient = TraceServiceClient.Create(); // Initialize request argument(s) string projectId = ""; string traceId = ""; // Make the request Trace response = await traceServiceClient.GetTraceAsync(projectId, traceId); // End snippet }
public override async Task <ExportResult> ExportAsync(IEnumerable <Trace.Span> spanDataList, CancellationToken cancellationToken) { var traceWriter = TraceServiceClient.Create(settings: this.traceServiceSettings); var batchSpansRequest = new BatchWriteSpansRequest { ProjectName = this.googleCloudProjectId, Spans = { spanDataList.Select(s => s.ToSpan(this.googleCloudProjectId.ProjectId)) }, }; await traceWriter.BatchWriteSpansAsync(batchSpansRequest, cancellationToken).ConfigureAwait(false); // TODO failures return(ExportResult.Success); }
/// <summary>Snippet for GetTrace</summary> public void GetTraceRequestObject() { // Snippet: GetTrace(GetTraceRequest, CallSettings) // Create client TraceServiceClient traceServiceClient = TraceServiceClient.Create(); // Initialize request argument(s) GetTraceRequest request = new GetTraceRequest { ProjectId = "", TraceId = "", }; // Make the request Trace response = traceServiceClient.GetTrace(request); // End snippet }
/// <summary>Snippet for PatchTraces</summary> public void PatchTracesRequestObject() { // Snippet: PatchTraces(PatchTracesRequest, CallSettings) // Create client TraceServiceClient traceServiceClient = TraceServiceClient.Create(); // Initialize request argument(s) PatchTracesRequest request = new PatchTracesRequest { ProjectId = "", Traces = new Traces(), }; // Make the request traceServiceClient.PatchTraces(request); // End snippet }
/// <summary>Snippet for BatchWriteSpans</summary> public void BatchWriteSpansRequestObject() { // Snippet: BatchWriteSpans(BatchWriteSpansRequest, CallSettings) // Create client TraceServiceClient traceServiceClient = TraceServiceClient.Create(); // Initialize request argument(s) BatchWriteSpansRequest request = new BatchWriteSpansRequest { ProjectName = ProjectName.FromProject("[PROJECT]"), Spans = { new Span(), }, }; // Make the request traceServiceClient.BatchWriteSpans(request); // End snippet }