-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
batchpersignal.go
115 lines (97 loc) · 4.2 KB
/
batchpersignal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright The 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:https://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 batchpersignal // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchpersignal"
import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/ptrace"
)
// SplitTraces returns one ptrace.Traces for each trace in the given ptrace.Traces input. Each of the resulting ptrace.Traces contains exactly one trace.
func SplitTraces(batch ptrace.Traces) []ptrace.Traces {
// for each span in the resource spans, we group them into batches of rs/ils/traceID.
// if the same traceID exists in different ils, they land in different batches.
var result []ptrace.Traces
for i := 0; i < batch.ResourceSpans().Len(); i++ {
rs := batch.ResourceSpans().At(i)
for j := 0; j < rs.ScopeSpans().Len(); j++ {
// the batches for this ILS
batches := map[pcommon.TraceID]ptrace.ResourceSpans{}
ils := rs.ScopeSpans().At(j)
for k := 0; k < ils.Spans().Len(); k++ {
span := ils.Spans().At(k)
key := span.TraceID()
// for the first traceID in the ILS, initialize the map entry
// and add the singleTraceBatch to the result list
if _, ok := batches[key]; !ok {
trace := ptrace.NewTraces()
newRS := trace.ResourceSpans().AppendEmpty()
// currently, the ResourceSpans implementation has only a Resource and an ILS. We'll copy the Resource
// and set our own ILS
rs.Resource().CopyTo(newRS.Resource())
newRS.SetSchemaUrl(rs.SchemaUrl())
newILS := newRS.ScopeSpans().AppendEmpty()
// currently, the ILS implementation has only an InstrumentationLibrary and spans. We'll copy the library
// and set our own spans
ils.Scope().CopyTo(newILS.Scope())
newILS.SetSchemaUrl(ils.SchemaUrl())
batches[key] = newRS
result = append(result, trace)
}
// there is only one instrumentation library per batch
tgt := batches[key].ScopeSpans().At(0).Spans().AppendEmpty()
span.CopyTo(tgt)
}
}
}
return result
}
// SplitLogs returns one plog.Logs for each trace in the given plog.Logs input. Each of the resulting plog.Logs contains exactly one log.
func SplitLogs(batch plog.Logs) []plog.Logs {
// for each log in the resource logs, we group them into batches of rl/sl/traceID.
// if the same traceID exists in different sl, they land in different batches.
var result []plog.Logs
for i := 0; i < batch.ResourceLogs().Len(); i++ {
rs := batch.ResourceLogs().At(i)
for j := 0; j < rs.ScopeLogs().Len(); j++ {
// the batches for this ILL
batches := map[pcommon.TraceID]plog.ResourceLogs{}
sl := rs.ScopeLogs().At(j)
for k := 0; k < sl.LogRecords().Len(); k++ {
log := sl.LogRecords().At(k)
key := log.TraceID()
// for the first traceID in the ILL, initialize the map entry
// and add the singleTraceBatch to the result list
if _, ok := batches[key]; !ok {
logs := plog.NewLogs()
newRL := logs.ResourceLogs().AppendEmpty()
// currently, the ResourceLogs implementation has only a Resource and an ILL. We'll copy the Resource
// and set our own ILL
rs.Resource().CopyTo(newRL.Resource())
newRL.SetSchemaUrl(rs.SchemaUrl())
newILL := newRL.ScopeLogs().AppendEmpty()
// currently, the ILL implementation has only an InstrumentationLibrary and logs. We'll copy the library
// and set our own logs
sl.Scope().CopyTo(newILL.Scope())
newILL.SetSchemaUrl(sl.SchemaUrl())
batches[key] = newRL
result = append(result, logs)
}
// there is only one instrumentation library per batch
tgt := batches[key].ScopeLogs().At(0).LogRecords().AppendEmpty()
log.CopyTo(tgt)
}
}
}
return result
}