Skip to content

Commit 1386b93

Browse files
committed
internal/telemetry: convert attributes using the key type
This uses the strongly typed key to get the value rather than type switching on the value itself. Change-Id: I8f72d1d9cac0191b0565c14d8a1108459ee6df36 Reviewed-on: https://go-review.googlesource.com/c/tools/+/225379 Run-TryBot: Ian Cottrell <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 1249273 commit 1386b93

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

internal/telemetry/export/ocagent/ocagent.go

+36-32
Original file line numberDiff line numberDiff line change
@@ -234,43 +234,47 @@ func convertAttributes(it event.TagIterator) *wire.Attributes {
234234
attributes := make(map[string]wire.Attribute)
235235
for ; it.Valid(); it.Advance() {
236236
tag := it.Tag()
237-
attributes[tag.Key.Name()] = convertAttribute(tag.Value)
237+
attributes[tag.Key.Name()] = convertAttribute(tag)
238238
}
239239
return &wire.Attributes{AttributeMap: attributes}
240240
}
241241

242-
func convertAttribute(v interface{}) wire.Attribute {
243-
switch v := v.(type) {
244-
case int8:
245-
return wire.IntAttribute{IntValue: int64(v)}
246-
case int16:
247-
return wire.IntAttribute{IntValue: int64(v)}
248-
case int32:
249-
return wire.IntAttribute{IntValue: int64(v)}
250-
case int64:
251-
return wire.IntAttribute{IntValue: v}
252-
case int:
253-
return wire.IntAttribute{IntValue: int64(v)}
254-
case uint8:
255-
return wire.IntAttribute{IntValue: int64(v)}
256-
case uint16:
257-
return wire.IntAttribute{IntValue: int64(v)}
258-
case uint32:
259-
return wire.IntAttribute{IntValue: int64(v)}
260-
case uint64:
261-
return wire.IntAttribute{IntValue: int64(v)}
262-
case uint:
263-
return wire.IntAttribute{IntValue: int64(v)}
264-
case float32:
265-
return wire.DoubleAttribute{DoubleValue: float64(v)}
266-
case float64:
267-
return wire.DoubleAttribute{DoubleValue: v}
268-
case bool:
269-
return wire.BoolAttribute{BoolValue: v}
270-
case string:
271-
return wire.StringAttribute{StringValue: toTruncatableString(v)}
242+
func convertAttribute(tag event.Tag) wire.Attribute {
243+
switch key := tag.Key.(type) {
244+
case *event.IntKey:
245+
return wire.IntAttribute{IntValue: int64(key.From(tag))}
246+
case *event.Int8Key:
247+
return wire.IntAttribute{IntValue: int64(key.From(tag))}
248+
case *event.Int16Key:
249+
return wire.IntAttribute{IntValue: int64(key.From(tag))}
250+
case *event.Int32Key:
251+
return wire.IntAttribute{IntValue: int64(key.From(tag))}
252+
case *event.Int64Key:
253+
return wire.IntAttribute{IntValue: int64(key.From(tag))}
254+
case *event.UIntKey:
255+
return wire.IntAttribute{IntValue: int64(key.From(tag))}
256+
case *event.UInt8Key:
257+
return wire.IntAttribute{IntValue: int64(key.From(tag))}
258+
case *event.UInt16Key:
259+
return wire.IntAttribute{IntValue: int64(key.From(tag))}
260+
case *event.UInt32Key:
261+
return wire.IntAttribute{IntValue: int64(key.From(tag))}
262+
case *event.UInt64Key:
263+
return wire.IntAttribute{IntValue: int64(key.From(tag))}
264+
case *event.Float32Key:
265+
return wire.DoubleAttribute{DoubleValue: float64(key.From(tag))}
266+
case *event.Float64Key:
267+
return wire.DoubleAttribute{DoubleValue: key.From(tag)}
268+
case *event.BooleanKey:
269+
return wire.BoolAttribute{BoolValue: key.From(tag)}
270+
case *event.StringKey:
271+
return wire.StringAttribute{StringValue: toTruncatableString(key.From(tag))}
272+
case *event.ErrorKey:
273+
return wire.StringAttribute{StringValue: toTruncatableString(key.From(tag).Error())}
274+
case *event.ValueKey:
275+
return wire.StringAttribute{StringValue: toTruncatableString(fmt.Sprint(key.From(tag)))}
272276
default:
273-
return wire.StringAttribute{StringValue: toTruncatableString(fmt.Sprint(v))}
277+
return wire.StringAttribute{StringValue: toTruncatableString(fmt.Sprintf("%T", key))}
274278
}
275279
}
276280

0 commit comments

Comments
 (0)