Exemplo n.º 1
0
        internal static LogEntry SetTraceAndSpanIfAny(
            this LogEntry entry, TraceTarget traceTarget,
            IServiceProvider serviceProvider, Action <IServiceProvider, LogEntry, TraceTarget> obsoleteTraceContextGetter)
        {
            GaxPreconditions.CheckNotNull(entry, nameof(entry));

            if (traceTarget is null)
            {
                return(entry);
            }

            // If there's currently a Google trace and span use that one.
            // This means that the Google Trace component of the diagnostics library
            // has been initialized.
            // Else attempt to use an external trace context.
            var context = ContextTracerManager.GetCurrentTraceContext() ?? serviceProvider.SafeGetTraceContext();

            if (context is ITraceContext && context.TraceId is string)
            {
                entry.Trace        = traceTarget.GetFullTraceName(context.TraceId);
                entry.TraceSampled = context.ShouldTrace ?? false;
                entry.SpanId       = SpanIdToHex(context.SpanId);
            }
            else
            {
                obsoleteTraceContextGetter?.Invoke(serviceProvider, entry, traceTarget);
            }

            return(entry);
Exemplo n.º 2
0
        private void SetTraceAndSpanIfAny(LogEntry entry)
        {
            if (_traceTarget is null)
            {
                return;
            }

            // If there's currently a Google trace and span use that one.
            // This means that the Google Trace component of the diagnostics library
            // has been initialized.
            // Else attempt to use an external trace context.
            if ((ContextTracerManager.GetCurrentTraceContext() ?? _serviceProvider?.GetService <ITraceContext>()) is ITraceContext context &&
                context.TraceId is string)
            {
                entry.Trace        = _traceTarget.GetFullTraceName(context.TraceId);
                entry.TraceSampled = context.ShouldTrace ?? false;
                entry.SpanId       = SpanIdToHex(context.SpanId);
            }