resource.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package awsecscontainermetrics // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awsecscontainermetricsreceiver/internal/awsecscontainermetrics"
  4. import (
  5. "strings"
  6. "go.opentelemetry.io/collector/pdata/pcommon"
  7. conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
  8. "go.uber.org/zap"
  9. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/ecsutil"
  10. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/docker"
  11. )
  12. func containerResource(cm ecsutil.ContainerMetadata, logger *zap.Logger) pcommon.Resource {
  13. resource := pcommon.NewResource()
  14. image, err := docker.ParseImageName(cm.Image)
  15. if err != nil {
  16. docker.LogParseError(err, cm.Image, logger)
  17. }
  18. resource.Attributes().PutStr(conventions.AttributeContainerName, cm.ContainerName)
  19. resource.Attributes().PutStr(conventions.AttributeContainerID, cm.DockerID)
  20. resource.Attributes().PutStr(attributeECSDockerName, cm.DockerName)
  21. resource.Attributes().PutStr(conventions.AttributeContainerImageName, image.Repository)
  22. resource.Attributes().PutStr(attributeContainerImageID, cm.ImageID)
  23. resource.Attributes().PutStr(conventions.AttributeContainerImageTag, image.Tag)
  24. resource.Attributes().PutStr(attributeContainerCreatedAt, cm.CreatedAt)
  25. resource.Attributes().PutStr(attributeContainerStartedAt, cm.StartedAt)
  26. if cm.FinishedAt != "" {
  27. resource.Attributes().PutStr(attributeContainerFinishedAt, cm.FinishedAt)
  28. }
  29. resource.Attributes().PutStr(attributeContainerKnownStatus, cm.KnownStatus)
  30. if cm.ExitCode != nil {
  31. resource.Attributes().PutInt(attributeContainerExitCode, *cm.ExitCode)
  32. }
  33. return resource
  34. }
  35. func taskResource(tm ecsutil.TaskMetadata) pcommon.Resource {
  36. resource := pcommon.NewResource()
  37. region, accountID, taskID := getResourceFromARN(tm.TaskARN)
  38. resource.Attributes().PutStr(attributeECSCluster, getNameFromCluster(tm.Cluster))
  39. resource.Attributes().PutStr(conventions.AttributeAWSECSTaskARN, tm.TaskARN)
  40. resource.Attributes().PutStr(attributeECSTaskID, taskID)
  41. resource.Attributes().PutStr(conventions.AttributeAWSECSTaskFamily, tm.Family)
  42. // Task revision: aws.ecs.task.version and aws.ecs.task.revision
  43. resource.Attributes().PutStr(attributeECSTaskRevision, tm.Revision)
  44. resource.Attributes().PutStr(conventions.AttributeAWSECSTaskRevision, tm.Revision)
  45. resource.Attributes().PutStr(attributeECSServiceName, tm.ServiceName)
  46. resource.Attributes().PutStr(conventions.AttributeCloudAvailabilityZone, tm.AvailabilityZone)
  47. resource.Attributes().PutStr(attributeECSTaskPullStartedAt, tm.PullStartedAt)
  48. resource.Attributes().PutStr(attributeECSTaskPullStoppedAt, tm.PullStoppedAt)
  49. resource.Attributes().PutStr(attributeECSTaskKnownStatus, tm.KnownStatus)
  50. // Task launchtype: aws.ecs.task.launch_type (raw string) and aws.ecs.launchtype (lowercase)
  51. resource.Attributes().PutStr(attributeECSTaskLaunchType, tm.LaunchType)
  52. switch lt := strings.ToLower(tm.LaunchType); lt {
  53. case "ec2":
  54. resource.Attributes().PutStr(conventions.AttributeAWSECSLaunchtype, conventions.AttributeAWSECSLaunchtypeEC2)
  55. case "fargate":
  56. resource.Attributes().PutStr(conventions.AttributeAWSECSLaunchtype, conventions.AttributeAWSECSLaunchtypeFargate)
  57. }
  58. resource.Attributes().PutStr(conventions.AttributeCloudRegion, region)
  59. resource.Attributes().PutStr(conventions.AttributeCloudAccountID, accountID)
  60. return resource
  61. }
  62. // https://docs.aws.amazon.com/AmazonECS/latest/userguide/ecs-account-settings.html
  63. // The new taskARN format: New: arn:aws:ecs:region:aws_account_id:task/cluster-name/task-id
  64. //
  65. // Old(current): arn:aws:ecs:region:aws_account_id:task/task-id
  66. func getResourceFromARN(arn string) (string, string, string) {
  67. if !strings.HasPrefix(arn, "arn:aws:ecs") {
  68. return "", "", ""
  69. }
  70. splits := strings.Split(arn, "/")
  71. taskID := splits[len(splits)-1]
  72. subSplits := strings.Split(splits[0], ":")
  73. region := subSplits[3]
  74. accountID := subSplits[4]
  75. return region, accountID, taskID
  76. }
  77. // The Amazon Resource Name (ARN) that identifies the cluster. The ARN contains the arn:aws:ecs namespace,
  78. // followed by the Region of the cluster, the AWS account ID of the cluster owner, the cluster namespace,
  79. // and then the cluster name. For example, arn:aws:ecs:region:012345678910:cluster/test.
  80. func getNameFromCluster(cluster string) string {
  81. if cluster == "" || !strings.HasPrefix(cluster, "arn:aws") {
  82. return cluster
  83. }
  84. splits := strings.Split(cluster, "/")
  85. return splits[len(splits)-1]
  86. }