internal void AddSpanAndRemoveAmbient(Span span, SpanScope scope)
        {
            lock (gate)
            {
                this.spans.Add(ref span);

                if (ambientManageQueue != null && ambientManageQueue.Count != 0)
                {
                    ambientManageQueue.Remove(scope);
                }
            }
        }
        /// <summary>
        /// Replace returned child scope as ambient root.
        /// </summary>
        /// <param name="name">The span name.</param>
        /// <param name="resource">The resource you are tracing such as "/Home/Index", "/Article/Post".</param>
        /// <param name="service">The service name such as "webservice", "batch", "mysql", "redis".</param>
        /// <param name="type">The type of request such as "web", "db", "cache".</param>
        public SpanScope BeginSpanAndChangeAmbientScope(string name, string resource, string service, string type)
        {
            lock (gate)
            {
                if (ambientManageQueue == null)
                {
                    ambientManageQueue = new List <SpanScope>(4);
                }

                SpanScope scope;
                if (ambientManageQueue.Count == 0)
                {
                    scope = new SpanScope(name, resource, service, type, SpanId, this, true);
                }
                else
                {
                    var lastChild = ambientManageQueue[ambientManageQueue.Count - 1];
                    scope = lastChild.BeginSpan(name, resource, service, type, true);
                }

                ambientManageQueue.Add(scope);
                return(scope);
            }
        }