-
Notifications
You must be signed in to change notification settings - Fork 618
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
126 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package reporter | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"time" | ||
|
||
"github.com/aws/amazon-ecs-agent/agent/api" | ||
"github.com/aws/amazon-ecs-agent/agent/config" | ||
"github.com/aws/amazon-ecs-agent/agent/engine" | ||
"github.com/aws/amazon-ecs-agent/agent/version" | ||
"github.com/aws/amazon-ecs-agent/ecs-agent/doctor" | ||
"github.com/aws/amazon-ecs-agent/ecs-agent/eventstream" | ||
tcshandler "github.com/aws/amazon-ecs-agent/ecs-agent/tcs/handler" | ||
"github.com/aws/amazon-ecs-agent/ecs-agent/tcs/model/ecstcs" | ||
"github.com/aws/amazon-ecs-agent/ecs-agent/utils/retry" | ||
"github.com/aws/amazon-ecs-agent/ecs-agent/wsclient" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/cihub/seelog" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
// The maximum time to wait between heartbeats without disconnecting | ||
defaultHeartbeatTimeout = 1 * time.Minute | ||
defaultHeartbeatJitter = 1 * time.Minute | ||
// Default websocket client disconnection timeout initiated by agent | ||
defaultDisconnectionTimeout = 15 * time.Minute | ||
defaultDisconnectionJitter = 30 * time.Minute | ||
) | ||
|
||
func StartSession( | ||
ctx context.Context, | ||
containerInstanceArn string, | ||
credentialProvider *credentials.Credentials, | ||
cfg *config.Config, | ||
deregisterInstanceEventStream *eventstream.EventStream, | ||
ecsClient api.ECSClient, | ||
taskEngine engine.TaskEngine, | ||
metricsChannel <-chan ecstcs.TelemetryMessage, | ||
healthChannel <-chan ecstcs.HealthMessage, | ||
doctor *doctor.Doctor) { | ||
ok, cfgParseErr := isContainerHealthMetricsDisabled(cfg) | ||
if cfgParseErr != nil { | ||
seelog.Warnf("Error starting metrics session: %v", cfgParseErr) | ||
return | ||
} | ||
if ok { | ||
seelog.Warnf("Metrics were disabled, not starting the telemetry session") | ||
return | ||
} | ||
|
||
backoff := retry.NewExponentialBackoff(time.Second, 1*time.Minute, 0.2, 2) | ||
params := tcshandler.TelemetrySessionParams{ | ||
Ctx: ctx, | ||
ContainerInstanceArn: containerInstanceArn, | ||
Cluster: cfg.Cluster, | ||
DisableContainerHealthMetrics: cfg.DisableMetrics.Enabled(), | ||
CredentialProvider: credentialProvider, | ||
Cfg: &wsclient.WSClientMinAgentConfig{ | ||
AWSRegion: cfg.AWSRegion, | ||
AcceptInsecureCert: cfg.AcceptInsecureCert, | ||
DockerEndpoint: cfg.DockerEndpoint, | ||
IsDocker: true, | ||
}, | ||
DeregisterInstanceEventStream: deregisterInstanceEventStream, | ||
HeartbeatTimeout: defaultHeartbeatTimeout, | ||
HeartbeatJitterMax: defaultHeartbeatJitter, | ||
DisconnectTimeout: defaultDisconnectionTimeout, | ||
DisconnectJitterMax: defaultDisconnectionJitter, | ||
MetricsChannel: metricsChannel, | ||
HealthChannel: healthChannel, | ||
Doctor: doctor, | ||
} | ||
for { | ||
tcsError := startTelemetrySessionAttempt(¶ms, containerInstanceArn, taskEngine, ecsClient) | ||
if tcsError == nil || tcsError == io.EOF { | ||
seelog.Info("TCS Websocket connection closed for a valid reason") | ||
backoff.Reset() | ||
} else { | ||
seelog.Errorf("Error: lost websocket connection with ECS Telemetry service (TCS): %v", tcsError) | ||
params.Time().Sleep(backoff.Duration()) | ||
} | ||
select { | ||
case <-params.Ctx.Done(): | ||
seelog.Info("TCS session exited cleanly.") | ||
return | ||
default: | ||
} | ||
} | ||
} | ||
|
||
func startTelemetrySessionAttempt(params *tcshandler.TelemetrySessionParams, | ||
containerInstanceArn string, taskEngine engine.TaskEngine, ecsClient api.ECSClient) error { | ||
params.AgentVersion = version.Version | ||
params.AgentHash = version.GitHashString() | ||
if dockerVersion, getVersionErr := taskEngine.Version(); getVersionErr == nil { | ||
params.ContainerRuntime = tcshandler.ContainerRuntimeDocker | ||
params.ContainerRuntimeVersion = dockerVersion | ||
} | ||
tcsEndpoint, err := ecsClient.DiscoverTelemetryEndpoint(containerInstanceArn) | ||
if err != nil { | ||
seelog.Errorf("tcs: unable to discover poll endpoint: %v", err) | ||
} | ||
params.Endpoint = tcsEndpoint | ||
return tcshandler.StartMetricsSession(params) | ||
} | ||
|
||
func isContainerHealthMetricsDisabled(cfg *config.Config) (bool, error) { | ||
if cfg != nil { | ||
return cfg.DisableMetrics.Enabled() && cfg.DisableDockerHealthCheck.Enabled(), nil | ||
|
||
} | ||
return false, errors.New("Config is empty in the tcs session parameter") | ||
} |