signalfxv2_event_to_logdata.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package signalfxreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/signalfxreceiver"
  4. import (
  5. sfxpb "github.com/signalfx/com_signalfx_metrics_protobuf/model"
  6. "go.opentelemetry.io/collector/pdata/pcommon"
  7. "go.opentelemetry.io/collector/pdata/plog"
  8. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
  9. )
  10. // signalFxV2ToMetricsData converts SignalFx event proto data points to
  11. // plog.LogRecordSlice. Returning the converted data and the number of dropped log
  12. // records.
  13. func signalFxV2EventsToLogRecords(events []*sfxpb.Event, lrs plog.LogRecordSlice) {
  14. lrs.EnsureCapacity(len(events))
  15. for _, event := range events {
  16. lr := lrs.AppendEmpty()
  17. attrs := lr.Attributes()
  18. attrs.EnsureCapacity(2 + len(event.Dimensions) + len(event.Properties))
  19. for _, dim := range event.Dimensions {
  20. attrs.PutStr(dim.Key, dim.Value)
  21. }
  22. // The EventType field is stored as an attribute.
  23. eventType := event.EventType
  24. if eventType == "" {
  25. eventType = "unknown"
  26. }
  27. attrs.PutStr(splunk.SFxEventType, eventType)
  28. // SignalFx timestamps are in millis so convert to nanos by multiplying
  29. // by 1 million.
  30. lr.SetTimestamp(pcommon.Timestamp(event.Timestamp * 1e6))
  31. if event.Category != nil {
  32. attrs.PutInt(splunk.SFxEventCategoryKey, int64(*event.Category))
  33. } else {
  34. // This gives us an unambiguous way of determining that a log record
  35. // represents a SignalFx event, even if category is missing from the
  36. // event.
  37. attrs.PutEmpty(splunk.SFxEventCategoryKey)
  38. }
  39. if len(event.Properties) > 0 {
  40. propMap := attrs.PutEmptyMap(splunk.SFxEventPropertiesKey)
  41. propMap.EnsureCapacity(len(event.Properties))
  42. for _, prop := range event.Properties {
  43. // No way to tell what value type is without testing each
  44. // individually.
  45. switch {
  46. case prop.Value.StrValue != nil:
  47. propMap.PutStr(prop.Key, prop.Value.GetStrValue())
  48. case prop.Value.IntValue != nil:
  49. propMap.PutInt(prop.Key, prop.Value.GetIntValue())
  50. case prop.Value.DoubleValue != nil:
  51. propMap.PutDouble(prop.Key, prop.Value.GetDoubleValue())
  52. case prop.Value.BoolValue != nil:
  53. propMap.PutBool(prop.Key, prop.Value.GetBoolValue())
  54. default:
  55. // If there is no property value, just insert a null to
  56. // record that the key was present.
  57. propMap.PutEmpty(prop.Key)
  58. }
  59. }
  60. }
  61. }
  62. }