hostid.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package splunk // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
  4. import (
  5. "fmt"
  6. "strings"
  7. "go.opentelemetry.io/collector/pdata/pcommon"
  8. conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
  9. )
  10. // HostIDKey represents a host identifier.
  11. type HostIDKey string
  12. const (
  13. // HostIDKeyAWS AWS HostIDKey.
  14. HostIDKeyAWS HostIDKey = "AWSUniqueId"
  15. // HostIDKeyGCP GCP HostIDKey.
  16. HostIDKeyGCP HostIDKey = "gcp_id"
  17. // HostIDKeyAzure Azure HostIDKey.
  18. HostIDKeyAzure HostIDKey = "azure_resource_id"
  19. // HostIDKeyHost Host HostIDKey.
  20. HostIDKeyHost HostIDKey = conventions.AttributeHostName
  21. )
  22. // HostID is a unique key and value (usually used as a dimension) to uniquely identify a host
  23. // using metadata about a cloud instance.
  24. type HostID struct {
  25. // Key is the key name/type.
  26. Key HostIDKey
  27. // Value is the unique ID.
  28. ID string
  29. }
  30. // ResourceToHostID returns a boolean determining whether or not a HostID was able to be
  31. // computed or not.
  32. func ResourceToHostID(res pcommon.Resource) (HostID, bool) {
  33. var cloudAccount, hostID, provider string
  34. attrs := res.Attributes()
  35. if attrs.Len() == 0 {
  36. return HostID{}, false
  37. }
  38. if attr, ok := attrs.Get(conventions.AttributeCloudAccountID); ok {
  39. cloudAccount = attr.Str()
  40. }
  41. if attr, ok := attrs.Get(conventions.AttributeHostID); ok {
  42. hostID = attr.Str()
  43. }
  44. if attr, ok := attrs.Get(conventions.AttributeCloudProvider); ok {
  45. provider = attr.Str()
  46. }
  47. switch provider {
  48. case conventions.AttributeCloudProviderAWS:
  49. var region string
  50. if attr, ok := attrs.Get(conventions.AttributeCloudRegion); ok {
  51. region = attr.Str()
  52. }
  53. if hostID == "" || region == "" || cloudAccount == "" {
  54. break
  55. }
  56. return HostID{
  57. Key: HostIDKeyAWS,
  58. ID: fmt.Sprintf("%s_%s_%s", hostID, region, cloudAccount),
  59. }, true
  60. case conventions.AttributeCloudProviderGCP:
  61. if cloudAccount == "" || hostID == "" {
  62. break
  63. }
  64. return HostID{
  65. Key: HostIDKeyGCP,
  66. ID: fmt.Sprintf("%s_%s", cloudAccount, hostID),
  67. }, true
  68. case conventions.AttributeCloudProviderAzure:
  69. if cloudAccount == "" {
  70. break
  71. }
  72. id := azureID(attrs, cloudAccount)
  73. if id == "" {
  74. break
  75. }
  76. return HostID{
  77. Key: HostIDKeyAzure,
  78. ID: id,
  79. }, true
  80. }
  81. if attr, ok := attrs.Get(conventions.AttributeHostName); ok {
  82. return HostID{
  83. Key: HostIDKeyHost,
  84. ID: attr.Str(),
  85. }, true
  86. }
  87. return HostID{}, false
  88. }
  89. func azureID(attrs pcommon.Map, cloudAccount string) string {
  90. var resourceGroupName string
  91. if attr, ok := attrs.Get("azure.resourcegroup.name"); ok {
  92. resourceGroupName = attr.Str()
  93. }
  94. if resourceGroupName == "" {
  95. return ""
  96. }
  97. var hostname string
  98. if attr, ok := attrs.Get("azure.vm.name"); ok {
  99. hostname = attr.Str()
  100. }
  101. if hostname == "" {
  102. return ""
  103. }
  104. var vmScaleSetName string
  105. if attr, ok := attrs.Get("azure.vm.scaleset.name"); ok {
  106. vmScaleSetName = attr.Str()
  107. }
  108. if vmScaleSetName == "" {
  109. return strings.ToLower(fmt.Sprintf(
  110. "%s/%s/microsoft.compute/virtualmachines/%s",
  111. cloudAccount,
  112. resourceGroupName,
  113. hostname,
  114. ))
  115. }
  116. instanceID := strings.TrimPrefix(hostname, vmScaleSetName+"_")
  117. return strings.ToLower(fmt.Sprintf(
  118. "%s/%s/microsoft.compute/virtualmachinescalesets/%s/virtualmachines/%s",
  119. cloudAccount,
  120. resourceGroupName,
  121. vmScaleSetName,
  122. instanceID,
  123. ))
  124. }