<commit_before>// Copyright 2021 OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
	"context"
	"time"

	"go.opentelemetry.io/collector/model/pdata"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/codes"
	"go.opentelemetry.io/otel/sdk/instrumentation"
	sdkresource "go.opentelemetry.io/otel/sdk/resource"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
	apitrace "go.opentelemetry.io/otel/trace"
)

func pdataResourceSpansToOTSpanData(rs pdata.ResourceSpans) []sdktrace.ReadOnlySpan {
	resource := rs.Resource()
	var sds []sdktrace.ReadOnlySpan
	ilss := rs.ScopeSpans()
	for i := 0; i < ilss.Len(); i++ {
		ils := ilss.At(i)
		spans := ils.Spans()
		for j := 0; j < spans.Len(); j++ {
			sd := pdataSpanToOTSpanData(spans.At(j), resource, ils.Scope())
			sds = append(sds, sd)
		}
	}

	return sds
}

func pdataSpanToOTSpanData(
	span pdata.Span,
	resource pdata.Resource,
	il pdata.InstrumentationScope,
) spanSnapshot {
	sc := apitrace.SpanContextConfig{
		TraceID: span.TraceID().Bytes(),
		SpanID:  span.SpanID().Bytes(),
	}
	parentSc := apitrace.SpanContextConfig{
		TraceID: span.TraceID().Bytes(),
		SpanID:  span.ParentSpanID().Bytes(),
	}
	startTime := time.Unix(0, int64(span.StartTimestamp()))
	endTime := time.Unix(0, int64(span.EndTimestamp()))
	// TODO: Decide if ignoring the error is fine.
	r, _ := sdkresource.New(
		context.Background(),
		sdkresource.WithAttributes(pdataAttributesToOTAttributes(pdata.NewMap(), resource)...),
	)

	status := span.Status()
	return spanSnapshot{
		spanContext:          apitrace.NewSpanContext(sc),
		parent:               apitrace.NewSpanContext(parentSc),
		spanKind:             pdataSpanKindToOTSpanKind(span.Kind()),
		startTime:            startTime,
		endTime:              endTime,
		name:                 span.Name(),
		attributes:           pdataAttributesToOTAttributes(span.Attributes(), resource),
		links:                pdataLinksToOTLinks(span.Links()),
		events:               pdataEventsToOTMessageEvents(span.Events()),
		droppedAttributes:    int(span.DroppedAttributesCount()),
		droppedMessageEvents: int(span.DroppedEventsCount()),
		droppedLinks:         int(span.DroppedLinksCount()),
		resource:             r,
		instrumentationLibrary: instrumentation.Library{
			Name:    il.Name(),
			Version: il.Version(),
		},
		status: sdktrace.Status{
			Code:        pdataStatusCodeToOTCode(status.Code()),
			Description: status.Message(),
		},
	}
}

func pdataSpanKindToOTSpanKind(k pdata.SpanKind) apitrace.SpanKind {
	switch k {
	case pdata.SpanKindUnspecified:
		return apitrace.SpanKindInternal<commit_msg>address remaining nits from #347 (#351)<commit_after>// Copyright 2021 OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
	"context"
	"time"

	"go.opentelemetry.io/collector/model/pdata"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/codes"
	"go.opentelemetry.io/otel/sdk/instrumentation"
	sdkresource "go.opentelemetry.io/otel/sdk/resource"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
	apitrace "go.opentelemetry.io/otel/trace"
)

func pdataResourceSpansToOTSpanData(rs pdata.ResourceSpans) []sdktrace.ReadOnlySpan {
	resource := rs.Resource()
	var sds []sdktrace.ReadOnlySpan
	ss := rs.ScopeSpans()
	for i := 0; i < ss.Len(); i++ {
		s := ss.At(i)
		spans := s.Spans()
		for j := 0; j < spans.Len(); j++ {
			sd := pdataSpanToOTSpanData(spans.At(j), resource, s.Scope())
			sds = append(sds, sd)
		}
	}

	return sds
}

func pdataSpanToOTSpanData(
	span pdata.Span,
	resource pdata.Resource,
	is pdata.InstrumentationScope,
) spanSnapshot {
	sc := apitrace.SpanContextConfig{
		TraceID: span.TraceID().Bytes(),
		SpanID:  span.SpanID().Bytes(),
	}
	parentSc := apitrace.SpanContextConfig{
		TraceID: span.TraceID().Bytes(),
		SpanID:  span.ParentSpanID().Bytes(),
	}
	startTime := time.Unix(0, int64(span.StartTimestamp()))
	endTime := time.Unix(0, int64(span.EndTimestamp()))
	// TODO: Decide if ignoring the error is fine.
	r, _ := sdkresource.New(
		context.Background(),
		sdkresource.WithAttributes(pdataAttributesToOTAttributes(pdata.NewMap(), resource)...),
	)

	status := span.Status()
	return spanSnapshot{
		spanContext:          apitrace.NewSpanContext(sc),
		parent:               apitrace.NewSpanContext(parentSc),
		spanKind:             pdataSpanKindToOTSpanKind(span.Kind()),
		startTime:            startTime,
		endTime:              endTime,
		name:                 span.Name(),
		attributes:           pdataAttributesToOTAttributes(span.Attributes(), resource),
		links:                pdataLinksToOTLinks(span.Links()),
		events:               pdataEventsToOTMessageEvents(span.Events()),
		droppedAttributes:    int(span.DroppedAttributesCount()),
		droppedMessageEvents: int(span.DroppedEventsCount()),
		droppedLinks:         int(span.DroppedLinksCount()),
		resource:             r,
		instrumentationLibrary: instrumentation.Library{
			Name:    is.Name(),
			Version: is.Version(),
		},
		status: sdktrace.Status{
			Code:        pdataStatusCodeToOTCode(status.Code()),
			Description: status.Message(),
		},
	}
}

func pdataSpanKindToOTSpanKind(k pdata.SpanKind) apitrace.SpanKind {
	switch k {
	case pdata.SpanKindUnspecified:
		return apitrace.SpanKindInternal
{'fraction_non_alphanumeric': 0.12590952230306865, 'fraction_numerical': 0.0060107560898449855, 'mean_word_length': 3.690652818991098, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4601153', 'n_tokens_mistral': 2152, 'n_tokens_neox': 2043, 'n_words': 458}