diff --git a/plugins/ecs/ecs.go b/plugins/ecs/ecs.go new file mode 100644 index 0000000..a60b04a --- /dev/null +++ b/plugins/ecs/ecs.go @@ -0,0 +1,41 @@ +package ecs + +import ( + "os" + "strings" + + "github.com/shogo82148/aws-xray-yasdk-go/xray" + "github.com/shogo82148/aws-xray-yasdk-go/xray/schema" +) + +type plugin struct { + ECS *schema.ECS +} + +// Init activates ECS Plugin at runtime. +func Init() { + uri := os.Getenv("ECS_CONTAINER_METADATA_URI") + if !strings.HasPrefix(uri, "http://") { + return + } + hostname, err := os.Hostname() + if err != nil { + return + } + xray.AddPlugin(&plugin{ + ECS: &schema.ECS{ + Container: hostname, + }, + }) +} + +// HandleSegment implements Plugin. +func (p *plugin) HandleSegment(seg *xray.Segment, doc *schema.Segment) { + if doc.AWS == nil { + doc.AWS = schema.AWS{} + } + doc.AWS.SetECS(p.ECS) +} + +// Origin implements Plugin. +func (*plugin) Origin() string { return schema.OriginECSContainer } diff --git a/plugins/ecs/init/init.go b/plugins/ecs/init/init.go new file mode 100644 index 0000000..7bb7108 --- /dev/null +++ b/plugins/ecs/init/init.go @@ -0,0 +1,7 @@ +package init + +import "github.com/shogo82148/aws-xray-yasdk-go/plugins/ecs" + +func init() { + ecs.Init() +}