internal ActivitySourceAdapter(Sampler sampler, ActivityProcessor activityProcessor, Resource resource)
        {
            if (sampler == null)
            {
                throw new ArgumentNullException(nameof(sampler));
            }

            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            this.sampler = sampler;
            if (this.sampler is AlwaysOnSampler)
            {
                this.getRequestedDataAction = this.RunGetRequestedDataAlwaysOnSampler;
            }
            else if (this.sampler is AlwaysOffSampler)
            {
                this.getRequestedDataAction = this.RunGetRequestedDataAlwaysOffSampler;
            }
            else
            {
                this.getRequestedDataAction = this.RunGetRequestedDataOtherSampler;
            }

            this.activityProcessor = activityProcessor;
            this.resource          = resource;
        }
Beispiel #2
0
        internal TracerProviderSdk AddProcessor(ActivityProcessor processor)
        {
            if (processor == null)
            {
                throw new ArgumentNullException(nameof(processor));
            }

            if (this.processor == null)
            {
                this.processor = processor;
            }
            else if (this.processor is CompositeActivityProcessor compositeProcessor)
            {
                compositeProcessor.AddProcessor(processor);
            }
            else
            {
                this.processor = new CompositeActivityProcessor(new[]
                {
                    this.processor,
                    processor,
                });
            }

            this.adapter?.UpdateProcessor(this.processor);

            return(this);
        }
        /// <summary>
        /// Adds processor to the provider.
        /// </summary>
        /// <param name="processor">Activity processor to add.</param>
        /// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
        public TracerProviderBuilder AddProcessor(ActivityProcessor processor)
        {
            if (processor == null)
            {
                throw new ArgumentNullException(nameof(processor));
            }

            this.processors.Add(processor);

            return(this);
        }
        public CompositeActivityProcessor AddProcessor(ActivityProcessor processor)
        {
            if (processor == null)
            {
                throw new ArgumentNullException(nameof(processor));
            }

            var node = new DoublyLinkedListNode <ActivityProcessor>(processor)
            {
                Previous = this.tail,
            };

            this.tail.Next = node;
            this.tail      = node;

            return(this);
        }
Beispiel #5
0
        internal ActivityProcessor Build()
        {
            this.Processors = new List <ActivityProcessor>();

            ActivityProcessor exportingProcessor = null;

            // build or create default exporting processor
            if (this.lastProcessorFactory != null)
            {
                exportingProcessor = this.lastProcessorFactory.Invoke(this.Exporter);
                this.Processors.Add(exportingProcessor);
            }
            else if (this.Exporter != null)
            {
                // TODO: Make this BatchingActivityProcessor once its available.
                exportingProcessor = new SimpleActivityProcessor(this.Exporter);
                this.Processors.Add(exportingProcessor);
            }

            if (this.processorChain == null)
            {
                // if there is no chain, return exporting processor.
                if (exportingProcessor == null)
                {
                    exportingProcessor = new NoopActivityProcessor();
                    this.Processors.Add(exportingProcessor);
                }

                return(exportingProcessor);
            }

            var next = exportingProcessor;

            // build chain from the end to the beginning
            for (int i = this.processorChain.Count - 1; i >= 0; i--)
            {
                next = this.processorChain[i].Invoke(next);
                this.Processors.Add(next);
            }

            // return the last processor in the chain - it will be called first
            return(this.Processors[this.Processors.Count - 1]);
        }
Beispiel #6
0
        public static TracerProvider AddProcessor(this TracerProvider provider, ActivityProcessor processor)
        {
            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            if (processor == null)
            {
                throw new ArgumentNullException(nameof(processor));
            }

            var trait = provider as TracerProviderSdk;

            if (trait == null)
            {
                throw new ArgumentException($"{nameof(provider)} is not an instance of TracerProviderSdk");
            }

            return(trait.AddProcessor(processor));
        }
 internal void UpdateProcessor(ActivityProcessor processor)
 {
     this.activityProcessor = processor;
 }
Beispiel #8
0
 internal ActivitySourceAdapter(Sampler sampler, ActivityProcessor activityProcessor, Resource resource)
 {
     this.sampler           = sampler;
     this.activityProcessor = activityProcessor;
     this.resource          = resource;
 }
Beispiel #9
0
        /// <summary>
        /// Creates TracerProvider with the configuration provided.
        /// This sets up listeners for all configured ActivitySources and
        /// sends activities to the pipeline of Sampler, Processor and Exporter.
        /// </summary>
        /// <param name="configureTracerProviderBuilder">Action to configure TracerProviderBuilder.</param>
        /// <returns>TracerProvider instance, which must be disposed upon shutdown.</returns>
        public static TracerProvider CreateTracerProvider(Action <TracerProviderBuilder> configureTracerProviderBuilder)
        {
            var tracerProviderBuilder = new TracerProviderBuilder();

            configureTracerProviderBuilder?.Invoke(tracerProviderBuilder);

            var     tracerProviderSdk = new TracerProviderSdk();
            Sampler sampler           = tracerProviderBuilder.Sampler ?? new AlwaysOnSampler();

            ActivityProcessor activityProcessor;

            if (tracerProviderBuilder.ProcessingPipelines == null || !tracerProviderBuilder.ProcessingPipelines.Any())
            {
                // if there are no pipelines are configured, use noop processor
                activityProcessor = new NoopActivityProcessor();
            }
            else if (tracerProviderBuilder.ProcessingPipelines.Count == 1)
            {
                // if there is only one pipeline - use it's outer processor as a
                // single processor on the tracerSdk.
                var processorFactory = tracerProviderBuilder.ProcessingPipelines[0];
                activityProcessor = processorFactory.Build();
            }
            else
            {
                // if there are more pipelines, use processor that will broadcast to all pipelines
                var processors = new ActivityProcessor[tracerProviderBuilder.ProcessingPipelines.Count];

                for (int i = 0; i < tracerProviderBuilder.ProcessingPipelines.Count; i++)
                {
                    processors[i] = tracerProviderBuilder.ProcessingPipelines[i].Build();
                }

                activityProcessor = new BroadcastActivityProcessor(processors);
            }

            tracerProviderSdk.Resource = tracerProviderBuilder.Resource;

            var activitySource = new ActivitySourceAdapter(sampler, activityProcessor, tracerProviderSdk.Resource);

            if (tracerProviderBuilder.InstrumentationFactories != null)
            {
                foreach (var instrumentation in tracerProviderBuilder.InstrumentationFactories)
                {
                    tracerProviderSdk.Instrumentations.Add(instrumentation.Factory(activitySource));
                }
            }

            // This is what subscribes to Activities.
            // Think of this as the replacement for DiagnosticListener.AllListeners.Subscribe(onNext => diagnosticListener.Subscribe(..));
            tracerProviderSdk.ActivityListener = new ActivityListener
            {
                // Callback when Activity is started.
                ActivityStarted = (activity) =>
                {
                    if (activity.IsAllDataRequested)
                    {
                        activity.SetResource(tracerProviderSdk.Resource);
                    }

                    activityProcessor.OnStart(activity);
                },

                // Callback when Activity is stopped.
                ActivityStopped = activityProcessor.OnEnd,

                // Function which takes ActivitySource and returns true/false to indicate if it should be subscribed to
                // or not
                ShouldListenTo = (activitySource) => tracerProviderBuilder.ActivitySourceNames?.Contains(activitySource.Name.ToUpperInvariant()) ?? false,

                // The following parameter is not used now.
                GetRequestedDataUsingParentId = (ref ActivityCreationOptions <string> options) => ActivityDataRequest.AllData,

                // This delegate informs ActivitySource about sampling decision when the parent context is an ActivityContext.
                GetRequestedDataUsingContext = (ref ActivityCreationOptions <ActivityContext> options) => ComputeActivityDataRequest(options, sampler),
            };

            ActivitySource.AddActivityListener(tracerProviderSdk.ActivityListener);
            tracerProviderSdk.ActivityProcessor = activityProcessor;
            return(tracerProviderSdk);
        }
Beispiel #10
0
 internal ActivitySourceAdapter(ActivitySampler activitySampler, ActivityProcessor activityProcessor)
 {
     this.activitySampler   = activitySampler;
     this.activityProcessor = activityProcessor;
 }