Giter Site home page Giter Site logo

aws-samples / aws-xray-fargate Goto Github PK

View Code? Open in Web Editor NEW
94.0 29.0 38.0 176 KB

Code examples showing how to run AWS X-Ray as a sidecar container on Fargate for deep application insights.

License: Apache License 2.0

JavaScript 58.24% Dockerfile 41.76%
aws-xray aws-fargate aws-ecs container distributed-tracing

aws-xray-fargate's Introduction

Application Tracing on Fargate with AWS X-Ray

This is a continuation of an earlier post on application tracking on Kubernetes with AWS X-Ray.

As microservices proliferate, the ability to trace calls between different different services becomes increasingly important. This can be accomplished using AWS X-Ray, a managed service that provides distributed application tracing functionality. X-Ray was built to help you understand how your services are interacting with each other by surfacing information such as call latency and exceptions, which together, helps you analyze, debug, and resolve issues faster.

Containers have become a popular vehicle for packaging and deploying microservices because they’re lightweight, polyglot, and run consistently as they’re moved across different environments. It’s not unusual for these types of applications to be deployed using a container orchestration platform like ECS. ECS simplifies running containers by providing a managed control plane for managing cluster resources and scheduling your containers onto a fleet of EC2 instances. These instances, however, are still your responsibility. Since managing a large fleet of hosts can be complex, we built Fargate, a managed service that removes “undifferentiated heavy lifting” that often accompanies management of a fleet of container instances. Fargate allows you to concentrate on building and operating your application, rather than infrastructure and gives you yet another option for running your microservices applications.

Running X-Ray on Fargate

The remainder of this post explains how to run X-Ray as a sidecar container in a Fargate task. By doing this, you will be able to provide application tracing capabilities to services running on Fargate.

The code, including a simple Node.js based demo application is available in the src folder, so you can quickly get started with X-Ray.

The sample application within the repository consists of two simple microservices, Service-A and Service-B. The following diagram illustrates how each service is deployed with 2 Fargate services:

xray-traffic-flow

  • Requests are sent to the Service-A from clients.
  • Service-A then contacts Service-B.
  • The requests are serviced by Service-B.
  • Service-B adds a random delay to each request to show different response times in X-Ray.

To test out the sample applications on your own ECS/Fargate cluster use the Dockerfiles provided in the GitHub repository, then follow the steps in the README file.

Prerequisites

If you do not have an ECS cluster running within your AWS environment, create it now using the following command:

aws ecs create-cluster --cluster-name <cluster_name> --region us-east-1

NOTE: At present, you can only create Fargate tasks in us-east-1, us-east-2, us-west-2 and eu-west-1.

Deploy X-Ray as a sidecar container

In Christoph Kassen’s blog post, he describes how to deploy the X-Ray pod as a Kubernetes daemonset. A daemonset is a special type of deployment strategy that places one instance of a pod onto each instance in the cluster. It will also deploy an instance of that pod onto Kubernetes workers as they’re added to the cluster. Since Fargate doesn’t yet have support for daemonsets, you have to deploy X-Ray as a sidecar container. A sidecar container is a container that runs alongside another container in the same Fargate task.

xray-sidecar

The task definitions in the GitHub repository are already configured to run X-Ray as a sidecar container.

Connecting to the X-Ray daemon

To integrate application tracing with your applications, use the X-Ray SDK for one of the supported programming languages:

  • Java
  • Node.js
  • .NET (Framework and Core)
  • Go
  • Python

The SDKs provide classes and methods for generating and sending trace data to the X-Ray daemon. Trace data includes information about incoming HTTP requests served by the application, and calls that the application makes to downstream services using the AWS SDK or HTTP clients.

By default, the X-Ray SDK expects the daemon to be available on 127.0.0.1:2000. Since we’re running X-Ray as a sidecar container, no changes are necessary.

Sending tracing information to AWS X-Ray

Sending tracing information from your application is straightforward with the X-Ray SDKs. The example code below serves as a starting point to instrument your application with traces. Take a look at the two sample applications in the GitHub repository to see how to send traces from Service A to Service B. The diagram below visualizes the flow of requests between the services.

Since Fargate is a service that manages the instances your tasks run on, access to the underlying host is prohibited. Consequently, the ECSPlugin and EC2Plugins for X-Ray will not work.

var app = express();

//...

var AWSXRay = require('aws-xray-sdk');

app.use(AWSXRay.express.openSegment('defaultName')); //required at the start of your routes

app.get('/', function (req, res) {
res.render('index');
});

app.use(AWSXRay.express.closeSegment()); //required at the end of your routes / first in error handling routes

For more information about all options and possibilities to instrument your application code, see the X-Ray documentation page for the corresponding SDK information.

The image below shows the resulting service map that provides insights into the flow of requests through the microservice landscape. You can drill down here into individual traces and see which path each request has taken.

xray-service-map

From the service map, you can drill down into individual requests and see where they originated from and how much time was spent in each service processing the request.

xray-trace

You can also view details about every individual segment of the trace by clicking on it. This displays gives more details.

xray-segment

Summary

In this post I shared how to deploy and run X-Ray running as a sidecar container in a Fargate task. By using X-Ray for distributed tracing you can get insights into how well your applications are performing and spot issues early before them become real problems.

License

This library is licensed under the Apache 2.0 License.

aws-xray-fargate's People

Contributors

jicowan avatar jpeddicord avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aws-xray-fargate's Issues

cluster_name to be specified in ecs-cli compose service up command

the current command does not mention the cluster name, and hence the service is not getting created. Need to add --cluster option to command as below.

ecs-cli compose service up --deployment-max-percent 100 --deployment-min-healthy-percent 0 --target-group-arn $TARGET_GROUP_ARN --launch-type FARGATE --container-name service-b --container-port 8080 --cluster <cluster_name>

xray instructions for fargate missing

Hi,

I don't see the instructions for the xray fargate. So far so good except I'm getting this message from service-a log:

2019-06-23 06:22:32 at next (/usr/src/app/node_modules/express/lib/router/index.js:275:10)
at next (/usr/src/app/node_modules/express/lib/router/index.js:275:10)
2019-06-23 06:22:32 at Layer.handle_error (/usr/src/app/node_modules/express/lib/router/layer.js:67:12)
at Layer.handle_error (/usr/src/app/node_modules/express/lib/router/layer.js:67:12)
2019-06-23 06:22:32Error: Failed to get the current sub/segment from the context.
Error: Failed to get the current sub/segment from the context.
2019-06-23 06:22:32 at Object.contextMissingRuntimeError [as contextMissing] (/usr/src/app/node_modules/aws-xray-sdk-core/lib/context_utils.js:21:15)
at Object.contextMissingRuntimeError [as contextMissing] (/usr/src/app/node_modules/aws-xray-sdk-core/lib/context_utils.js:21:15)
2019-06-23 06:22:32 at Object.getSegment (/usr/src/app/node_modules/aws-xray-sdk-core/lib/context_utils.js:92:45)
at Object.getSegment (/usr/src/app/node_modules/aws-xray-sdk-core/lib/context_utils.js:92:45)
2019-06-23 06:22:32 at Object.resolveSegment (/usr/src/app/node_modules/aws-xray-sdk-core/lib/context_utils.js:73:19)
at Object.resolveSegment (/usr/src/app/node_modules/aws-xray-sdk-core/lib/context_utils.js:73:19)
2019-06-23 06:22:32 at close (/usr/src/app/node_modules/aws-xray-sdk-express/lib/express_mw.js:95:29)
at close (/usr/src/app/node_modules/aws-xray-sdk-express/lib/express_mw.js:95:29)
2019-06-23 00:05:21Running on http://0.0.0.0:8080

Error in Xray logs

Hi guys, I have followed your guide and spinned the services but when looking at xray logs I can see:

16:30:48
2018-05-22T15:30:48Z [Info] Initializing AWS X-Ray daemon 2.1.2

16:30:48
2018-05-22T15:30:48Z [Info] Using buffer memory limit of 39 MB

16:30:48
2018-05-22T15:30:48Z [Info] 624 segment buffers allocated

16:30:48
2018-05-22T15:30:48Z [Info] Using region: us-east-1

16:30:49
2018-05-22T15:30:49Z [Error] Get instance id metadata failed: RequestError: send request failed

16:30:49
caused by: Get http://169.254.169.254/latest/meta-data/instance-id: dial tcp 169.254.169.254:80: connect: invalid argument
caused by: Get http://169.254.169.254/latest/meta-data/instance-id: dial tcp 169.254.169.254:80: connect: invalid argument

Issue on fargate when using ECS plugin

I think that ECS plugin is not working for fargate at least:

2018-05-23 09:13:18.951 ERROR 7 --- [ main] com.amazonaws.xray.plugins.ECSPlugin : Could not get docker container ID from hostname.

This is due to the fact that fargate container hostname is not really something that can be resolved to IP, when I had similar problem I had to fetch the IP from the task metadata endpoint

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.