Hello, Cloud Gurus and welcome to the AWS Service Spotlight,
which going forward is our new name for what used to be called the AWS Release
Review. Welcome. In this series,
we put the spotlight on specific AWS services,
walking through detailed technical demos to explore their features, benefits,
and costs.
I'm Nick Triantafillou and today we're going to be talking about Amazon
CodeGuru.
Announced that re:Invent in 2019 and made generally available in
June 2020,
Amazon CodeGuru is an automatic code review service for developers
aimed at making your organization's code better.
Using the power of machine learnin, it comes with two main features.
There's the CodeGuru Profiler and the CodeGuru Reviewer.
The Amazon CodeGuru Profiler is a tool which analyzes your code and
will help you identify the most expensive lines. By expensive in this context,
I'm talking about the lines of code which use the most CPU power to execute,
and it provides visualizations and recommendations on how to fix the expensive
code to Amazon CodeGuru Reviewer. On the other hand,
it uses machine learning to identify critical issues and possibly hard to find
bugs in your code repositories, which helps you improve the overall quality.
It detects deviation from best practices of using the AWS APIs and
SDKs, and it will flag common issues it finds as well,
which lets you fix them before they cause more problems at a later date.
Let's take a look at the costs. Firstly,
there's a 90-day free trial that you can take advantage of in any region that
CodeGuru is available in and you can start this trial at any time. After this,
CodeGuru Profiler is going to cost you half a cent per sampling hour for the
first 36,000 sampling hours per profiling group per month.
Sampling hours refers to how long your application code is tested for.
If you have one application being profiled and it runs on 50 EC2
instances, you hit the 36,000 sampling hours in a month.
So the total cost is $180.
If you have 10 applications being profiled on 15 instances,
you only hit 10,800 sampling hours each,
but it's multiplied by 10 because the 36,000 limit is per profiling group
and you have 10 of them.
So the total you're going to pay here is $540.
CodeGuru is much simpler to calculate.
You simply pay 75 cents per a hundred lines of code, which are analyzed.
Let's take a look at the benefits of CodeGuru Profiler. First,
it really allows you to understand the runtime behavior of your applications,
how they interact with the CPU utilization of the system they're running on,
and how latency may be introduced to your application due to poorly structured
code.
You receive recommendations from the service as to how you can remediate the
issues as well. It doesn't simply tell you there's a problem.
It intelligently explains how you might be able to fix it.
The CodeGuru Reviewer has some fantastic benefits too.
It finds issues in your code and also tells you how to remediate them. In fact,
it identifies code quality issues in nine broad categories:
AWS best practices, Java best practices, concurrency,
deadlocks, resource leaks, sensitive information leaks, common code bugs,
code cloning, and input validation.
The fact that this can be automated is a huge benefit or addition to any manual
review systems that your organization.
May have in place.
Let's head to the demo and check it out.
Okay. Here we are in the Amazon CodeGuru console.
And as you can see under Getting Started,
we have a choice of two CodeGuru services, the Profiler and the Reviewer.
I won't go into setting up a full profiler group because AWS have a demo one
created for you,
which is excellent for the purposes of testing out the service to see how it
works. So if we just come down here a bit under CodeGuru Profiler,
we have try demo profiling group. Let's click on that.
And when you do that,
you're immediately brought into a graph for our profiling group,
which purposely has issues.
And you can mouse over each component here and actually see which tasks are
blocked,
which are idle and even get an estimate of what the CPU cost is for the task
over a year.
So we can see here it's $142 a year just for that.
I can come down,
$490 a year and it goes all the way down to the bottom where you can see your
entire application is estimated to have about
$2,133 per year.
You can also click on these to break it down,
even further to the smallest components.
So it's a very detailed insight into what your code is doing and certainly an
excellent way to find any issues that you might not even be aware of.
The service can also give you recommendations. So if we look up here,
there's four recommendations and we can click on that. And as you can see,
as we scroll down,
the first shows you that your profile spends about 18% of its time in the
runnable and blocked time on frames related to this issue.
But they're actually expecting less than 1% of your profile should be spent on
these frames in most circumstances.
And that's going to cost you close to $400 a year due to that.
And it also provides some steps to resolve this issue.
And it says you should create only one or as few AWS SDK service clients as
possible.
So it looks like what the code is doing here is creating a new AWS SDK service
client every time it's trying to connect to the AWS API.
And that is a waste of resources. If we scroll down,
it also shows that there's too much debug and trace logging,
which costs up to a hundred dollars a year.
Loggers are also wasting a lot of money and down the bottom here,
$222 a year wasted on Jackson Object Mapper Creation.
Now it's entirely possible that you or your developers aren't even aware these
things can cost your organization money for being inefficient.
So it's a great learning experience and it's a great service to show you these
things. Makes you become aware of them and you end up saving money. Now,
if you go back to your profiling groups up here and you can see we had the one
with issues that we looked at, there's also one without issues.
We can click on that. And if we come down to the all, it looks a lot better.
There's no extra idle time causing your application to cost you $2,000 a year.
So this is a much better application with zero recommendations for fixing
because it's working really well. So that's how CodeGuru Profiler works.
So let's jump back here. I'm going to click on CodeGuru,
back to the front page and we're going to go to the CodeGuru Reviewer.
Let's click on get started.
Now the first thing you can see here is that it wants to associate to a
repository.
So what we're going to do is we're going to open up our web browser at an Amazon
Code Reviewer sample application that AWS has provided,
and we're going to fork it over to our own repository.
So I'm going to open up a new tab, okay, we're in GitHub.
This is the Amazon CodeGuru Reviewer sample application.
And I'll have a link to this in the resources for this episode as well. Now,
what we need to do is fork it,
which is basically create a copy in our own repository. I'll select mine.
[Inaudible].
Now this is going to give me an exact copy,
but running in my own repository and I can do whatever I want with it. Okay.
Now that that's forked,
let's head back to the CodeGuru console and I have GitHub selected,
but you can see code commit Bitbucket and GitHub Enterprise are also supported,
and we need to connect to GitHub. So I'm going to click on this.
And that automatically connected because I've done it before.
But if you've never done that before,
you probably just have to login to GitHub and click on authorize. Now,
all my repositories will be listed here. Let's click on this one,
which is our CodeGuru Reviewer sample app. And we'll click on associate.
Now we'll give this a minute or so.
And what it's going to do is associate directly with that GitHub repository.
If you create any pull requests,
it's going to automatically look at your code in that repository.
And it's going to give you feedback on the code and tell you how to fix it.
So now what we have to do is push a change to our repository.
So I'm going to open up my terminal and now we're in the terminal.
We're going to clone and then link straight to that repository that we forked.
Okay, now we have a copy of the repository on our file system.
So let's go into that directory
and you can see all of the files there. Now we want to create a new branch.
So that is simply git checkout -b, and we call it dev.
So now I'm just going to copy one file from a directory into another directory,
which will basically duplicate the file and then we'll commit it back.
So I'm going to cp src/main/java/com/shipmentEvents/handlers,
and it's our event handler.
And I'm going to copy it into source main Java com
shipment events. And then we've got a demo directory into there.
So now that that's done, we're going to add it. So git add all, Oh,
we need two dashes there and we'll commit it.
And then we're going to push it back to GitHub, git push.
And we have to set the upstream origin dev.
That's going to ask for my username and my password.
Okay, that's all done. Now we need to create a pull request.
So we're going to go back to our GitHub website and you can see here,
we actually have two branches.
We've got the main and the dev one we just created. Okay,
now we're going to create a new pull request and we have to make sure our base
repository is our version of this, not the AWS samples one.
So that's mine. And there we go.
And we're going to compare that to the dev branch.
And you can see all that was changed. One file was added,
which was the event handler dot Java. And I copied that into the demo directory.
So that's fine. Let's click on create, pull requests.
We don't need anything here. We can just click create.
Now that is all we have to do for CodeGuru Reviewer to do its job.
It's already monitoring my repository for changes.
So it would have seen that pull request and would have immediately started to
analyze the code when it's done.
It posts its reviews in GitHub and in the CodeGuru console as well.
If I refresh here with an F5, let's see if it said anything yet.
Not yet, but we'll head back to the CodeGuru console and take a look.
And we can see here that our code review is pending.
I'm just going to jump into the completed one because it's exactly the same.
And it was just from when I was testing. So let's click on the name.
And as you can see, there are four recommendations. There were 74 lines of code,
which were metered, and we're going to scroll down. You can see here,
the first one is on line 79.
This code appears to be waiting for a resource before it runs.
You could use the waiters feature to help improve efficiency.
And then you even get a link to how you can fix it. It says here on line 100,
you might not get accurate results. This code uses an outdated API,
and this code is written so the client can not be reused across invocations of
the Lambda function. So all really good suggestions,
things you might not even pick up if you didn't know how the API works.
So it's also a great way to keep on top of your code and make sure it's always
up to date with the latest AWS SDK and API calls.
In addition to all of this,
you can give these a thumbs up and a thumbs down if they were helpful to help
AWS train their service to better review yours and everyone else's
code. Okay,
that's it for the very quick demonstration on how the two different CodeGuru
services work.
[Inaudible].
Well, let's go over some pros and cons. As we saw with the Profiler,
you were given easy to see metrics and recommendations on how to make your
application better. The Reviewer on the other hand was simple to set up and use.
All you had to do was associate it to your repository and it was completely
automatic as well, automatically reviewing any code in your pull request.
Any cons I see with these two services is that Profiler currently only supports
JVM applications, including Java and Scala.
So you need to already be using those languages and their subsets.
With Reviewer, it currently only supports Java stored in GitHub,
Bitbucket and Code Command.
The repositories probably aren't a large con as so many people use them already,
but not a lot of people will convert a project to Java just to use this service.
So it may have limited use for a lot of people not already building applications
in Java. So should you invest, evaluate or revisit?
[Inaudible].
My recommendation is to invest. Both CodeGuru services are fantastic,
and I think they're worth investing in if your organization has a compatible
code base and you are really looking to set up the overall quality of your code,
if you want to reduce expensive CPU cycles,
and just generally produce better code. And with machine learning involved,
the suggestions on how to improve your code and detect issues are just going to
get better and better as time goes on. So that's it!
Keep being awesome Cloud Gurus,
and I hope to see you next time on AWS Service Spotlight.