Skip to content

Commit 13f453f

Browse files
committed
Call Clear always
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 2bfee7e commit 13f453f

File tree

6 files changed

+34
-25
lines changed

6 files changed

+34
-25
lines changed

translator/internaldata/oc_to_metrics.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func setDataPoints(ocMetric *ocmetrics.Metric, metric pdata.Metric) {
238238
}
239239
}
240240

241-
func setLabelsMap(ocLabelsKeys []*ocmetrics.LabelKey, ocLabelValues []*ocmetrics.LabelValue, labelsMap pdata.StringMap) {
241+
func fillLabelsMap(ocLabelsKeys []*ocmetrics.LabelKey, ocLabelValues []*ocmetrics.LabelValue, labelsMap pdata.StringMap) {
242242
if len(ocLabelsKeys) == 0 || len(ocLabelValues) == 0 {
243243
return
244244
}
@@ -250,6 +250,7 @@ func setLabelsMap(ocLabelsKeys []*ocmetrics.LabelKey, ocLabelValues []*ocmetrics
250250
lablesCount = len(ocLabelValues)
251251
}
252252

253+
labelsMap.Clear()
253254
labelsMap.EnsureCapacity(lablesCount)
254255
for i := 0; i < lablesCount; i++ {
255256
if !ocLabelValues[i].GetHasValue() {
@@ -280,7 +281,7 @@ func fillIntDataPoint(ocMetric *ocmetrics.Metric, dps pdata.IntDataPointSlice) {
280281

281282
dp.SetStartTimestamp(startTimestamp)
282283
dp.SetTimestamp(pdata.TimestampFromTime(point.GetTimestamp().AsTime()))
283-
setLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap())
284+
fillLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap())
284285
dp.SetValue(point.GetInt64Value())
285286
}
286287
}
@@ -308,7 +309,7 @@ func fillDoubleDataPoint(ocMetric *ocmetrics.Metric, dps pdata.DoubleDataPointSl
308309

309310
dp.SetStartTimestamp(startTimestamp)
310311
dp.SetTimestamp(pdata.TimestampFromTime(point.GetTimestamp().AsTime()))
311-
setLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap())
312+
fillLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap())
312313
dp.SetValue(point.GetDoubleValue())
313314
}
314315
}
@@ -336,7 +337,7 @@ func fillDoubleHistogramDataPoint(ocMetric *ocmetrics.Metric, dps pdata.Histogra
336337

337338
dp.SetStartTimestamp(startTimestamp)
338339
dp.SetTimestamp(pdata.TimestampFromTime(point.GetTimestamp().AsTime()))
339-
setLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap())
340+
fillLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap())
340341
distributionValue := point.GetDistributionValue()
341342
dp.SetSum(distributionValue.GetSum())
342343
dp.SetCount(uint64(distributionValue.GetCount()))
@@ -368,7 +369,7 @@ func fillDoubleSummaryDataPoint(ocMetric *ocmetrics.Metric, dps pdata.SummaryDat
368369

369370
dp.SetStartTimestamp(startTimestamp)
370371
dp.SetTimestamp(pdata.TimestampFromTime(point.GetTimestamp().AsTime()))
371-
setLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap())
372+
fillLabelsMap(ocLabelsKeys, timeseries.LabelValues, dp.LabelsMap())
372373
summaryValue := point.GetSummaryValue()
373374
dp.SetSum(summaryValue.GetSum().GetValue())
374375
dp.SetCount(uint64(summaryValue.GetCount().GetValue()))
@@ -414,12 +415,13 @@ func exemplarToMetrics(ocExemplar *ocmetrics.DistributionValue_Exemplar, exempla
414415
if ocExemplar.GetTimestamp() != nil {
415416
exemplar.SetTimestamp(pdata.TimestampFromTime(ocExemplar.GetTimestamp().AsTime()))
416417
}
417-
exemplar.SetValue(ocExemplar.GetValue())
418-
attachments := exemplar.FilteredLabels()
419418
ocAttachments := ocExemplar.GetAttachments()
420-
attachments.EnsureCapacity(len(ocAttachments))
419+
exemplar.SetValue(ocExemplar.GetValue())
420+
filteredLabels := exemplar.FilteredLabels()
421+
filteredLabels.Clear()
422+
filteredLabels.EnsureCapacity(len(ocAttachments))
421423
for k, v := range ocAttachments {
422-
attachments.Upsert(k, v)
424+
filteredLabels.Upsert(k, v)
423425
}
424426
}
425427

translator/internaldata/oc_to_resource.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func ocNodeResourceToInternal(ocNode *occommon.Node, ocResource *ocresource.Reso
8080
}
8181

8282
attrs := dest.Attributes()
83+
attrs.Clear()
8384
attrs.EnsureCapacity(maxTotalAttrCount)
8485

8586
if ocNode != nil {

translator/internaldata/oc_to_traces.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,24 @@ func ocAttrsToDroppedAttributes(ocAttrs *octrace.Span_Attributes) uint32 {
226226

227227
// initAttributeMapFromOC initialize AttributeMap from OC attributes
228228
func initAttributeMapFromOC(ocAttrs *octrace.Span_Attributes, dest pdata.AttributeMap) {
229-
if ocAttrs == nil {
229+
if ocAttrs == nil || len(ocAttrs.AttributeMap) == 0 {
230230
return
231231
}
232232

233-
if len(ocAttrs.AttributeMap) > 0 {
234-
dest.EnsureCapacity(len(ocAttrs.AttributeMap))
235-
for key, ocAttr := range ocAttrs.AttributeMap {
236-
switch attribValue := ocAttr.Value.(type) {
237-
case *octrace.AttributeValue_StringValue:
238-
dest.UpsertString(key, attribValue.StringValue.GetValue())
239-
case *octrace.AttributeValue_IntValue:
240-
dest.UpsertInt(key, attribValue.IntValue)
241-
case *octrace.AttributeValue_BoolValue:
242-
dest.UpsertBool(key, attribValue.BoolValue)
243-
case *octrace.AttributeValue_DoubleValue:
244-
dest.UpsertDouble(key, attribValue.DoubleValue)
245-
default:
246-
dest.UpsertString(key, "<Unknown OpenCensus attribute value type>")
247-
}
233+
dest.Clear()
234+
dest.EnsureCapacity(len(ocAttrs.AttributeMap))
235+
for key, ocAttr := range ocAttrs.AttributeMap {
236+
switch attribValue := ocAttr.Value.(type) {
237+
case *octrace.AttributeValue_StringValue:
238+
dest.UpsertString(key, attribValue.StringValue.GetValue())
239+
case *octrace.AttributeValue_IntValue:
240+
dest.UpsertInt(key, attribValue.IntValue)
241+
case *octrace.AttributeValue_BoolValue:
242+
dest.UpsertBool(key, attribValue.BoolValue)
243+
case *octrace.AttributeValue_DoubleValue:
244+
dest.UpsertDouble(key, attribValue.DoubleValue)
245+
default:
246+
dest.UpsertString(key, "<Unknown OpenCensus attribute value type>")
248247
}
249248
}
250249
}

translator/trace/jaeger/jaegerproto_to_traces.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func jProcessToInternalResource(process *model.Process, dest pdata.Resource) {
102102
}
103103

104104
attrs := dest.Attributes()
105+
attrs.Clear()
105106
if serviceName != "" {
106107
attrs.EnsureCapacity(len(tags) + 1)
107108
attrs.UpsertString(conventions.AttributeServiceName, serviceName)
@@ -176,6 +177,7 @@ func jSpanToInternal(span *model.Span) (pdata.Span, instrumentationLibrary) {
176177
}
177178

178179
attrs := dest.Attributes()
180+
attrs.Clear()
179181
attrs.EnsureCapacity(len(span.Tags))
180182
jTagsToInternalAttributes(span.Tags, attrs)
181183
setInternalSpanStatus(attrs, dest.Status())
@@ -332,6 +334,7 @@ func jLogsToSpanEvents(logs []model.Log, dest pdata.SpanEventSlice) {
332334
}
333335

334336
attrs := event.Attributes()
337+
attrs.Clear()
335338
attrs.EnsureCapacity(len(log.Fields))
336339
jTagsToInternalAttributes(log.Fields, attrs)
337340
if name, ok := attrs.Get(tracetranslator.TagMessage); ok {

translator/trace/jaeger/jaegerthrift_to_traces.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func jThriftProcessToInternalResource(process *jaeger.Process, dest pdata.Resour
6464
}
6565

6666
attrs := dest.Attributes()
67+
attrs.Clear()
6768
if serviceName != "" {
6869
attrs.EnsureCapacity(len(tags) + 1)
6970
attrs.UpsertString(conventions.AttributeServiceName, serviceName)
@@ -110,6 +111,7 @@ func jThriftSpanToInternal(span *jaeger.Span, dest pdata.Span) {
110111
}
111112

112113
attrs := dest.Attributes()
114+
attrs.Clear()
113115
attrs.EnsureCapacity(len(span.Tags))
114116
jThriftTagsToInternalAttributes(span.Tags, attrs)
115117
setInternalSpanStatus(attrs, dest.Status())
@@ -163,6 +165,7 @@ func jThriftLogsToSpanEvents(logs []*jaeger.Log, dest pdata.SpanEventSlice) {
163165
}
164166

165167
attrs := event.Attributes()
168+
attrs.Clear()
166169
attrs.EnsureCapacity(len(log.Fields))
167170
jThriftTagsToInternalAttributes(log.Fields, attrs)
168171
if name, ok := attrs.Get(tracetranslator.TagMessage); ok {

translator/trace/zipkin/zipkinv2_to_traces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func zSpanToInternal(zspan *zipkinmodel.SpanModel, tags map[string]string, dest
143143
}
144144

145145
attrs := dest.Attributes()
146+
attrs.Clear()
146147
attrs.EnsureCapacity(len(tags))
147148
if err := zTagsToInternalAttrs(zspan, tags, attrs, parseStringTags); err != nil {
148149
return err

0 commit comments

Comments
 (0)