From f7f83d5fbaf630249ed54836d5f8240325293b3a Mon Sep 17 00:00:00 2001 From: Ichinose Shogo Date: Tue, 31 Mar 2020 14:19:12 +0900 Subject: [PATCH] implement ecs plugin --- plugins/ecs/ecs.go | 41 ++++++++++++++++++++++++++++++++++++++++ plugins/ecs/init/init.go | 7 +++++++ 2 files changed, 48 insertions(+) create mode 100644 plugins/ecs/ecs.go create mode 100644 plugins/ecs/init/init.go 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() +}