stats_provider.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. "encoding/json"
  6. "fmt"
  7. "go.uber.org/zap"
  8. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/ecsutil"
  9. )
  10. // StatsProvider wraps a RestClient, returning an unmarshaled metadata and docker stats
  11. type StatsProvider struct {
  12. rc ecsutil.RestClient
  13. metadataProvider ecsutil.MetadataProvider
  14. }
  15. // NewStatsProvider returns a new stats provider
  16. func NewStatsProvider(rc ecsutil.RestClient, logger *zap.Logger) *StatsProvider {
  17. return &StatsProvider{rc: rc, metadataProvider: ecsutil.NewTaskMetadataProvider(rc, logger)}
  18. }
  19. // GetStats calls the ecs task metadata endpoint and unmarshals the data
  20. func (p *StatsProvider) GetStats() (map[string]*ContainerStats, ecsutil.TaskMetadata, error) {
  21. stats := make(map[string]*ContainerStats)
  22. var metadata ecsutil.TaskMetadata
  23. taskMetadata, err := p.metadataProvider.FetchTaskMetadata()
  24. if err != nil {
  25. return stats, metadata, fmt.Errorf("cannot read data from task metadata endpoint: %w", err)
  26. }
  27. if taskMetadata != nil {
  28. metadata = *taskMetadata
  29. }
  30. taskStats, err := p.rc.GetResponse(TaskStatsPath)
  31. if err != nil {
  32. return stats, metadata, fmt.Errorf("cannot read data from task metadata endpoint: %w", err)
  33. }
  34. err = json.Unmarshal(taskStats, &stats)
  35. if err != nil {
  36. return stats, metadata, fmt.Errorf("cannot unmarshall task stats: %w", err)
  37. }
  38. return stats, metadata, nil
  39. }