|
14 | 14 |
|
15 | 15 | package ecsobserver
|
16 | 16 |
|
17 |
| -import "fmt" |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/aws/aws-sdk-go/aws" |
| 21 | + "go.uber.org/multierr" |
| 22 | + "go.uber.org/zap" |
| 23 | + |
| 24 | + "github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer/ecsobserver/internal/errctx" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + defaultMetricsPath = "/metrics" |
| 29 | +) |
18 | 30 |
|
19 | 31 | // CommonExporterConfig should be embedded into filter config.
|
20 | 32 | // They set labels like job, metrics_path etc. that can override prometheus default.
|
@@ -45,3 +57,106 @@ type commonExportSetting struct {
|
45 | 57 | func (s *commonExportSetting) hasContainerPort(containerPort int) bool {
|
46 | 58 | return s.metricsPorts[containerPort]
|
47 | 59 | }
|
| 60 | + |
| 61 | +// taskExporter converts annotated Task into PrometheusECSTarget. |
| 62 | +type taskExporter struct { |
| 63 | + logger *zap.Logger |
| 64 | + cluster string |
| 65 | +} |
| 66 | + |
| 67 | +func newTaskExporter(logger *zap.Logger, cluster string) *taskExporter { |
| 68 | + return &taskExporter{ |
| 69 | + logger: logger, |
| 70 | + cluster: cluster, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// exportTasks loops a list of tasks and export prometheus scrape targets. |
| 75 | +// It keeps track of error but does NOT stop when error occurs. |
| 76 | +// The returned targets are valid, invalid targets are saved in a multi error. |
| 77 | +// Caller can ignore the error because the only source is failing to get ip and port. |
| 78 | +// The error(s) can generates debug log or metrics. |
| 79 | +// To print the error with its task as context, use printExporterErrors. |
| 80 | +func (e *taskExporter) exportTasks(tasks []*Task) ([]PrometheusECSTarget, error) { |
| 81 | + var merr error |
| 82 | + var allTargets []PrometheusECSTarget |
| 83 | + for _, t := range tasks { |
| 84 | + targets, err := e.exportTask(t) |
| 85 | + multierr.AppendInto(&merr, err) // if err == nil, AppendInto does nothing |
| 86 | + // Even if there are error, returned targets are still valid. |
| 87 | + allTargets = append(allTargets, targets...) |
| 88 | + } |
| 89 | + return allTargets, merr |
| 90 | +} |
| 91 | + |
| 92 | +// exportTask exports all the matched container within a single task. |
| 93 | +// One task can contain multiple containers. One container can have more than one target |
| 94 | +// if there are multiple ports in `metrics_port`. |
| 95 | +func (e *taskExporter) exportTask(task *Task) ([]PrometheusECSTarget, error) { |
| 96 | + // All targets in one task shares same IP. |
| 97 | + privateIP, err := task.PrivateIP() |
| 98 | + if err != nil { |
| 99 | + return nil, errctx.WithValue(err, errKeyTask, task) |
| 100 | + } |
| 101 | + |
| 102 | + // Base for all the containers in this task, most attributes are same. |
| 103 | + baseTarget := PrometheusECSTarget{ |
| 104 | + Source: aws.StringValue(task.Task.TaskArn), |
| 105 | + MetricsPath: defaultMetricsPath, |
| 106 | + ClusterName: e.cluster, |
| 107 | + TaskDefinitionFamily: aws.StringValue(task.Definition.Family), |
| 108 | + TaskDefinitionRevision: int(aws.Int64Value(task.Definition.Revision)), |
| 109 | + TaskStartedBy: aws.StringValue(task.Task.StartedBy), |
| 110 | + TaskLaunchType: aws.StringValue(task.Task.LaunchType), |
| 111 | + TaskGroup: aws.StringValue(task.Task.Group), |
| 112 | + TaskTags: task.TaskTags(), |
| 113 | + HealthStatus: aws.StringValue(task.Task.HealthStatus), |
| 114 | + } |
| 115 | + if task.Service != nil { |
| 116 | + baseTarget.ServiceName = aws.StringValue(task.Service.ServiceName) |
| 117 | + } |
| 118 | + if task.EC2 != nil { |
| 119 | + ec2 := task.EC2 |
| 120 | + baseTarget.EC2InstanceID = aws.StringValue(ec2.InstanceId) |
| 121 | + baseTarget.EC2InstanceType = aws.StringValue(ec2.InstanceType) |
| 122 | + baseTarget.EC2Tags = task.EC2Tags() |
| 123 | + baseTarget.EC2VpcID = aws.StringValue(ec2.VpcId) |
| 124 | + baseTarget.EC2SubnetID = aws.StringValue(ec2.SubnetId) |
| 125 | + baseTarget.EC2PrivateIP = privateIP |
| 126 | + baseTarget.EC2PublicIP = aws.StringValue(ec2.PublicIpAddress) |
| 127 | + } |
| 128 | + |
| 129 | + var targetsInTask []PrometheusECSTarget |
| 130 | + var merr error |
| 131 | + for _, m := range task.Matched { |
| 132 | + container := task.Definition.ContainerDefinitions[m.ContainerIndex] |
| 133 | + // Shallow copy task level attributes |
| 134 | + containerTarget := baseTarget |
| 135 | + // Add container specific info |
| 136 | + containerTarget.ContainerName = aws.StringValue(container.Name) |
| 137 | + containerTarget.ContainerLabels = task.ContainerLabels(m.ContainerIndex) |
| 138 | + // Multiple targets for a single container |
| 139 | + for _, matchedTarget := range m.Targets { |
| 140 | + // Shallow copy from container |
| 141 | + target := containerTarget |
| 142 | + mappedPort, err := task.MappedPort(container, int64(matchedTarget.Port)) |
| 143 | + if err != nil { |
| 144 | + err = errctx.WithValues(err, map[string]interface{}{ |
| 145 | + errKeyTarget: matchedTarget, |
| 146 | + errKeyTask: task, |
| 147 | + }) |
| 148 | + } |
| 149 | + // Skip this target and keep track of port error, does not abort. |
| 150 | + if multierr.AppendInto(&merr, err) { |
| 151 | + continue |
| 152 | + } |
| 153 | + target.Address = fmt.Sprintf("%s:%d", privateIP, mappedPort) |
| 154 | + if matchedTarget.MetricsPath != "" { |
| 155 | + target.MetricsPath = matchedTarget.MetricsPath |
| 156 | + } |
| 157 | + target.Job = matchedTarget.Job |
| 158 | + targetsInTask = append(targetsInTask, target) |
| 159 | + } |
| 160 | + } |
| 161 | + return targetsInTask, merr |
| 162 | +} |
0 commit comments