示例#1
0
        // https://github.com/DataDog/datadog-agent/blob/eac2327c5574da7f225f9ef0f89eaeb05ed10382/pkg/trace/traceutil/normalize.go#L39-L55
        internal static string NormalizeName(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                Log.Debug("Fixing malformed trace. Name is empty (reason:span_name_empty), setting span.name={name}.", name);
                return(DefaultSpanName);
            }

            if (TraceUtil.TruncateUTF8(ref name, MaxNameLen))
            {
                Log.Debug <int>("Fixing malformed trace. Name is too long (reason:span_name_truncate), truncating span.name to length={maxServiceLen}.", MaxNameLen);
            }

            name = TraceUtil.NormalizeMetricName(name, MaxNameLen);
            if (name is null)
            {
                name = DefaultSpanName;
            }

            return(name);
        }
示例#2
0
        public Span Process(Span span)
        {
            // https://github.com/DataDog/datadog-agent/blob/eac2327c5574da7f225f9ef0f89eaeb05ed10382/pkg/trace/agent/normalizer.go#L51-L63
            span.ServiceName = NormalizeService(span.ServiceName);

            // https://github.com/DataDog/datadog-agent/blob/eac2327c5574da7f225f9ef0f89eaeb05ed10382/pkg/trace/agent/normalizer.go#L76-L87
            span.OperationName = NormalizeName(span.OperationName);

            // https://github.com/DataDog/datadog-agent/blob/eac2327c5574da7f225f9ef0f89eaeb05ed10382/pkg/trace/agent/normalizer.go#L89-L93
            if (string.IsNullOrEmpty(span.ResourceName))
            {
                Log.Debug("Fixing malformed trace. Resource is empty (reason:resource_empty), setting span.resource={name}: {span}", span.OperationName, span);
                span.ResourceName = span.OperationName;
            }

            // https://github.com/DataDog/datadog-agent/blob/eac2327c5574da7f225f9ef0f89eaeb05ed10382/pkg/trace/agent/normalizer.go#L95-L99
            // The validation for ParentID == TraceID == SpanID is not required
            // This is done by the agent for zipkin support.
            // This scenario is very unlikely to happen in an official tracer implementation.

            // https://github.com/DataDog/datadog-agent/blob/eac2327c5574da7f225f9ef0f89eaeb05ed10382/pkg/trace/agent/normalizer.go#L108-L112
            if (span.Duration < TimeSpan.Zero)
            {
                Log.Debug("Fixing malformed trace. Duration is invalid (reason:invalid_duration), setting span.duration=0: {span}", span.OperationName, span);
                span.SetDuration(TimeSpan.Zero);
            }

            // https://github.com/DataDog/datadog-agent/blob/eac2327c5574da7f225f9ef0f89eaeb05ed10382/pkg/trace/agent/normalizer.go#L113-L117
            if (span.Duration.ToNanoseconds() > long.MaxValue - span.StartTime.ToUnixTimeNanoseconds())
            {
                Log.Debug("Fixing malformed trace. Duration is too large and causes overflow (reason:invalid_duration), setting span.duration=0: {span}", span.OperationName, span);
                span.SetDuration(TimeSpan.Zero);
            }

            // https://github.com/DataDog/datadog-agent/blob/eac2327c5574da7f225f9ef0f89eaeb05ed10382/pkg/trace/agent/normalizer.go#L118-L126
            if (span.StartTime < Year2000Time)
            {
                Log.Debug("Fixing malformed trace. Start date is invalid (reason:invalid_start_date), setting span.start=time.now(): {span}", span);
                var now   = span.Context.TraceContext.UtcNow;
                var start = now - span.Duration;
                if (start.ToUnixTimeNanoseconds() < 0)
                {
                    start = now;
                }

                span.SetStartTime(start);
            }

            // https://github.com/DataDog/datadog-agent/blob/eac2327c5574da7f225f9ef0f89eaeb05ed10382/pkg/trace/agent/normalizer.go#L128-L132
            var type = span.Type;

            if (TraceUtil.TruncateUTF8(ref type, MaxTypeLen))
            {
                span.Type = type;
                Log.Debug("Fixing malformed trace. Type is too long (reason:type_truncate), truncating span.type to length={maxServiceLen}: {span}", MaxTypeLen, span);
            }

            // https://github.com/DataDog/datadog-agent/blob/eac2327c5574da7f225f9ef0f89eaeb05ed10382/pkg/trace/agent/normalizer.go#L133-L135
            if (span.Tags is CommonTags commonTags)
            {
                commonTags.Environment = TraceUtil.NormalizeTag(commonTags.Environment);
            }
            else
            {
                string env = span.GetTag("env");
                if (!string.IsNullOrEmpty(env))
                {
                    span.SetTag("env", TraceUtil.NormalizeTag(env));
                }
            }

            // https://github.com/DataDog/datadog-agent/blob/eac2327c5574da7f225f9ef0f89eaeb05ed10382/pkg/trace/agent/normalizer.go#L136-L142
            if (span.Tags is IHasStatusCode statusCodeTags)
            {
                if (!TraceUtil.IsValidStatusCode(statusCodeTags.HttpStatusCode))
                {
                    Log.Debug("Fixing malformed trace. HTTP status code is invalid (reason:invalid_http_status_code), dropping invalid http.status_code={invalidStatusCode}: {span}", statusCodeTags.HttpStatusCode, span);
                    statusCodeTags.HttpStatusCode = string.Empty;
                }
            }
            else
            {
                string httpStatusCode = span.GetTag(Tags.HttpStatusCode);
                if (!string.IsNullOrEmpty(httpStatusCode) && !TraceUtil.IsValidStatusCode(httpStatusCode))
                {
                    Log.Debug("Fixing malformed trace. HTTP status code is invalid (reason:invalid_http_status_code), dropping invalid http.status_code={invalidStatusCode}: {span}", httpStatusCode, span);
                    span.SetTag(Tags.HttpStatusCode, null);
                }
            }

            return(span);
        }