Example #1
0
        internal static IEnumerable <OtlpTrace.ResourceSpans> ToOtlpResourceSpans(this IEnumerable <SpanData> spanDataList)
        {
            var resourceToLibraryAndSpans = GroupByResourceAndLibrary(spanDataList);
            var resourceSpansList         = new List <OtlpTrace.ResourceSpans>(resourceToLibraryAndSpans.Count);

            foreach (var resource in resourceToLibraryAndSpans)
            {
                // TODO: this is a temporary workaround since library is still on the resource, not on its own field.
                var libName = resource.Key.Attributes.FirstOrDefault(
                    kvp => kvp.Key == Resource.LibraryNameKey);
                var libVersion = resource.Key.Attributes.FirstOrDefault(
                    kvp => kvp.Key == Resource.LibraryVersionKey);

                var libraryList = new List <OtlpTrace.InstrumentationLibrarySpans>(resource.Value.Count);
                foreach (var library in resource.Value)
                {
                    var otlpLibrarySpans = new OtlpTrace.InstrumentationLibrarySpans
                    {
                        InstrumentationLibrary = new OtlpCommon.InstrumentationLibrary
                        {
                            Name    = libName.Value?.ToString() ?? string.Empty,
                            Version = libVersion.Value?.ToString() ?? string.Empty,
                        },
                    };

                    otlpLibrarySpans.Spans.AddRange(library.Value);
                    libraryList.Add(otlpLibrarySpans);
                }

                var otlpResources = new OtlpResource.Resource();
                otlpResources.Attributes.AddRange(
                    resource.Key.Attributes.Select(ToOtlpAttribute));

                var otlpResourceSpans = new OtlpTrace.ResourceSpans
                {
                    Resource = otlpResources,
                };
                otlpResourceSpans.InstrumentationLibrarySpans.AddRange(libraryList);

                resourceSpansList.Add(otlpResourceSpans);
            }

            return(resourceSpansList);
        }
        internal static IEnumerable <OtlpTrace.ResourceSpans> ToOtlpResourceSpans(this IEnumerable <Activity> activityBatch)
        {
            var resourceToLibraryAndSpans = GroupByResourceAndLibrary(activityBatch);
            var resourceSpansList         = new List <OtlpTrace.ResourceSpans>(resourceToLibraryAndSpans.Count);

            foreach (var resource in resourceToLibraryAndSpans)
            {
                var libraryList = new List <OtlpTrace.InstrumentationLibrarySpans>(resource.Value.Count);
                foreach (var activitySourceEntry in resource.Value)
                {
                    var activitySource   = activitySourceEntry.Key;
                    var otlpLibrarySpans = new OtlpTrace.InstrumentationLibrarySpans
                    {
                        InstrumentationLibrary = new OtlpCommon.InstrumentationLibrary
                        {
                            Name    = activitySource.Name,                    // Name is enforced to not be null, but it can be empty.
                            Version = activitySource.Version ?? string.Empty, // NRE throw by proto
                        },
                    };

                    otlpLibrarySpans.Spans.AddRange(activitySourceEntry.Value);
                    libraryList.Add(otlpLibrarySpans);
                }

                var otlpResources = new OtlpResource.Resource();
                otlpResources.Attributes.AddRange(
                    resource.Key.Attributes.Select(ToOtlpAttribute).Where(a => a != null));

                var otlpResourceSpans = new OtlpTrace.ResourceSpans
                {
                    Resource = otlpResources,
                };
                otlpResourceSpans.InstrumentationLibrarySpans.AddRange(libraryList);

                resourceSpansList.Add(otlpResourceSpans);
            }

            return(resourceSpansList);
        }