metadata_provider.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ecsutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/ecsutil"
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "fmt"
  8. "go.opentelemetry.io/collector/component"
  9. "go.opentelemetry.io/collector/config/confighttp"
  10. "go.uber.org/zap"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/ecsutil/endpoints"
  12. )
  13. type MetadataProvider interface {
  14. FetchTaskMetadata() (*TaskMetadata, error)
  15. FetchContainerMetadata() (*ContainerMetadata, error)
  16. }
  17. type ecsMetadataProviderImpl struct {
  18. logger *zap.Logger
  19. client RestClient
  20. }
  21. var _ MetadataProvider = &ecsMetadataProviderImpl{}
  22. func NewTaskMetadataProvider(client RestClient, logger *zap.Logger) MetadataProvider {
  23. return &ecsMetadataProviderImpl{
  24. client: client,
  25. logger: logger,
  26. }
  27. }
  28. func NewDetectedTaskMetadataProvider(set component.TelemetrySettings) (MetadataProvider, error) {
  29. endpoint, err := endpoints.GetTMEFromEnv()
  30. if err != nil {
  31. return nil, err
  32. } else if endpoint == nil {
  33. return nil, fmt.Errorf("unable to detect task metadata endpoint")
  34. }
  35. clientSettings := confighttp.HTTPClientSettings{}
  36. client, err := NewRestClient(*endpoint, clientSettings, set)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return &ecsMetadataProviderImpl{
  41. client: client,
  42. logger: set.Logger,
  43. }, nil
  44. }
  45. // FetchTaskMetadata retrieves the metadata for a task running on Amazon ECS
  46. func (md *ecsMetadataProviderImpl) FetchTaskMetadata() (*TaskMetadata, error) {
  47. resp, err := md.client.GetResponse(endpoints.TaskMetadataPath)
  48. if err != nil {
  49. return nil, err
  50. }
  51. taskMetadata := &TaskMetadata{}
  52. err = json.NewDecoder(bytes.NewReader(resp)).Decode(taskMetadata)
  53. if err != nil {
  54. return nil, fmt.Errorf("encountered unexpected error reading response from ECS Task Metadata Endpoint: %w", err)
  55. }
  56. return taskMetadata, nil
  57. }
  58. // FetchContainerMetadata retrieves the metadata for the Amazon ECS Container the collector is running on
  59. func (md *ecsMetadataProviderImpl) FetchContainerMetadata() (*ContainerMetadata, error) {
  60. resp, err := md.client.GetResponse(endpoints.ContainerMetadataPath)
  61. if err != nil {
  62. return nil, err
  63. }
  64. containerMetadata := &ContainerMetadata{}
  65. err = json.NewDecoder(bytes.NewReader(resp)).Decode(containerMetadata)
  66. if err != nil {
  67. return nil, fmt.Errorf("encountered unexpected error reading response from ECS Container Metadata Endpoint: %w", err)
  68. }
  69. return containerMetadata, nil
  70. }