# Advanced, free, open-source application performance monitoring for your Python apps

Source: https://djangotv.com/videos/djangocon-europe/2019/djangocon-2019-advanced-free-open-source-application-performance-monitoring-for-your-python-apps/
Date: 2019-04-21
Speakers: Speaker 1, Speaker 2, Speaker 3, Speaker 4

## Summary

Application performance monitoring (APM) goes beyond CPU, memory, and server metrics to show which requests, code paths, database queries, and external calls are slow or failing. Emanuil Tolev compares Prometheus with Elastic APM, explaining that Prometheus is flexible and stable but requires explicitly defined metrics and additional work for logs and application-specific instrumentation, while Elastic APM provides automatic tracing, logs, SQL and asynchronous-worker monitoring, real-user monitoring, and distributed tracing through an open-source, self-hosted stack. He argues that the main trade-off of free APM is the effort of running and maintaining the monitoring system, and calls for better interoperability between open-source observability projects and shared metrics standards.

## Transcript

### [00:00] Application Performance Monitoring

**Speaker 1:** [00:02] But hello, hello, welcome to my talk on application performance monitoring. So today we'll we'll do a little bit of a dive into kind of the history and context of APM and why we want it, why it matters to us at least and why it maybe matters to other people. And we're going to look at some alternatives for actually using APM and integrating it into your applications. So my name is Emmanuel. Emanuel Tolev. I'm a community engineer for Elastic. The makers of Elasticsearch, Kibana

[00:47] and so on, monitoring tooling that you have likely heard of and also the open source search engine. The so I do work for a vendor, so Elastic does make a whole APM solution as well as various monitoring. solutions but I have tried to touch on other projects and on other things and kind of the wider context of this but I will later show you some stuff in Kibana and then APM. However, Restsured all of it is completely free to use, including in commercial context, so I'm not here to sell you anything So let's get started with kind of what IPM is.

[01:33] How many of you have an idea of what I'm talking about when I say application performance monitoring Okay, that's reasonable, there's still some who don't, but that's the point of these talks, right? So APM very quickly then APM at its core is search. It's a solution to a cognitive problem. When you Want to know what your system is really doing specifically at the edge, what your application is really doing. So not simply monitoring the percentage of CPU or the percentage of RAM or the you know the disk IO that's being used, but really getting in there and looking perhaps at individual lines of code, bottlenecks in your application, what is slow, what is breaking, errors, etc.

### [02:22] On-Call Motivation

**Speaker 1:** [02:22] So that's application performance monitoring and sort of a short story about why it matters to me and kind of how I got interested in this whole area as of course like probably many of you I have also been on call for nearly five years and that was the kind of it's pretty hardcore it was the beginning of my career actually in computing. So straight out of uni and I became a freelancer. And I joined this small hipster collective of freelancers, this agency And soon enough I was in charge somehow of determining the operational strategy for clients. for a couple of smaller clients and later for some bigger ones.

[03:09] And that's where kind of My initial enthusiasm started to grow out of bounds and to hit a snag, right? Because what happened was that we would build all of these features And then in the middle of the night I would get a page and I would have to get up and fix the website. that had fallen down. Unfortunately, I was in a 24-7 contract and I was the only person on call for a few years on that project. And then the but at some point you know you do this, you know, I'm I was like what at twenty-two, twenty-three, so you do this and you don't really mind it, you know. It's it's it's for the project, for the client.

[03:56] But then a few years ago, what happened was I w I got woken up at three and then again at six and then again at seven, same day. And then at eleven and then I spent the next week trying to desperately make this website work. And what was happening was we were getting access to I think legitimately from China, but it was just not a use case that we had predicted. And it got really out of hand. And that's where I got kind of really interested in how do I prevent this from happening to me or other people ever again and really understand it

[04:44] Is that okay? Can you hear me? Can you hear me? Alright, okay. I'm just gonna just gonna hold this then. So yeah, so that's where I kinda got really interested into how to solve this problem. And so as far as I could tell, I mean at least in my career the evolution was I went from looking at log files and htop for like the first couple of years when I was sort of helping out small companies to then trying to collect Fuck it. To then trying to collect server information.

### [05:30] Monitoring Evolution

**Speaker 1:** [05:30] so federated data collection. There we go. And then finally APM. So we moved past the servers, we moved off the servers, off the virtual machines, and into the code itself which was kind of mind-blowing for me So this is a little slide from applicationperformance. com and I came in towards the end of that, so we're about thereabouts here when you start seeing the emergence of kind of more complex software as a service providers and we'll talk about what can they offer in a second. But we're sort of now obviously past this stage, so we're looking at what's the what's the next step. At the time

[06:16] when that when at the end of that slide towards the bottom right, competition was Very difficult in the APM space. It was very labor intensive to build new solutions, including open source ones. Instead competitors went for a narrow focus. And so this is actually a Copenhagen company. So this is Upbeat. How many of you have heard of Upbeat? Wow, geez, okay. Right, I'm not gonna spend a lot of time here then. So it specialized in JS and Python, including Django, and later on it in 2018 it 2017-2018 it got acquired by Elastic, the company I now work for.

[07:04] And Elastic APM kind of fits within the wider infrastructure of the Elastic Stack. So Kibana, the ping thing, is used to visualize the data that you put into Elasticsearch and everything else is used to put data into Elasticsearch. Beats and so beats collect metrics So numbers with labels, logstash puts logs in there, so text, and APM puts application level metrics like the particular lines of code and transactions that might be slow. So let's see if the Wi-Fi works. So the landscape has evolved significantly since then. Yes. Yes, excellent. And this is an excellent website.

### [07:50] APM Landscape

**Speaker 1:** [07:50] It's called openapm. io and I love it. It really showcases where like how crazy things have gotten. So there's the elastic stuff, so if you click that it doesn't really interoperate with much else, so it's just the elastic stack. And you can use Grafana also to visualize it. And you can use certain things to do alerting on it, but it it's pretty much designed to work as a as a as all the pieces together. And people sometimes add elastic beats to it, which allows you to get other metrics like you know your node metrics. So how is how are your actual servers performing underneath your containers? Your application code.

[08:36] However, since we'll talk about that later, I want to talk a little bit about a few other solutions that you might use. So another possible one is Prometheus. And that's an open source metrics store and time series database and the whole toolkit around that. It's very cool. It's based on Google Borgmon, or it was based on Google Borgmon. Now it's its own a very mature project. They released version two. So what you do with Prometheus is you have like with all of these things, you have an exporter, so you have an agent, something that hooks deeply within your application. And extracts all the useful information out there, like what transactions are

[09:23] being processed. Then you have a collector, so where the agents that sent all of that information, and that's the Prometheus server. And then you have storage. In this case, uniquely it is also the Prometheus server because as I mentioned it's a very capable time series database. And to visualize it, so yeah, the Prometheus itself can do visualizations. They're sort of individual and kind of basic. So what people really tend to use with Prometheus is Grafana. And Grafana is a very good tool. It's very flexible, it allows you to embed visualizations, and it actually plays well with Elasticsearch as well. And so this is typically what happens here.

[10:11] So here you have again we're talking about monitoring your servers. So the operating system level metrics, orchestrator metrics, container metrics, as well as your application metrics. Right. So Prometheus, so there's so what sort of because you know the I guess the point of this is to I mean n not really compare because I can't really do that since I work for a company that makes this, but to talk about the different possibilities here of what you can do and maybe you walk away with an idea of what you can use to enhance your current stack or when starting a new project. So Prometheus is a great alternative.

[10:57] You can so the main thing with it that that that I like that really makes it stand out I guess is its pool data collection model. So with Prometheus you don't have you don't run like this risk of overloading your monitoring infrastructure because you configure at the monitoring infrastructure how often it should pull and you configure you monitor that for when it's overloaded. So there is very little chance that it'll fall over. It's pretty stable It's got a very powerful query language and it serves does all of this stuff in a single tool. So it collects metrics and does the viz and supports the querying. It's all in Prometheus server, which is pretty cool.

[11:43] Let's see what 's the time? Right. I have a video about this actually, but I'm not gonna risk it because there's also time and so on. But I do have some extracts from there to show you like roughly what that looks like. And here is where we get into a more interesting topic. So this you should totally if you're interested in using it, totally watch that video. It's an incredibly clean, like clear talk so this is how you generally use Prometheus. You define that they have counters, histograms, gauges, like various types of objects. So in Python you would define what you want to monitor and this could be anything.

### [12:29] Prometheus Instrumentation

**Speaker 1:** [12:29] So this could be application level metrics, which we'll see in a second or it could be business metrics. So it could be how many sales are we making? How you know how as well as how slow the website is, it's like how happy are our users, how many units of stock have we shifted, how slow is the website specifically for our administrators and so on. So it's very ex extremely flexible and powerful in what you can define but the Other side of that coin is that you have to define that. So here and what what's happening here is we're using the definitions from the previous slide

[13:14] so we're using the counters, the gauges, etc. to actually monitor something in our application with Prometheus or well to send it to Prometheus. So Over up there we can see the entire route essentially being monitored. That's like at request time. time. And here we see a particular piece of the code being monitored. It's the context, so with analyze time And so that stuff is going to record obviously how long that piece takes and send it to Prometheus. So that's pretty cool because at the same time, you know, we could enhance that information in any way we want. We can tie like essentially how long a particular piece of code takes to how much money we're making. So that's excellent, right? The

[13:59] the thing about it is that it's metrics that you have to define explicitly. And the other thing about it is that it's only metrics. So metrics in the sense of you have some number and you put some label to it It's only that. So you don't get logs with it. You can't do log monitoring in Prometheus and you can't correlate with log monitoring. With this you will need to do quite a lot of work to achieve the same a level of sophistication and ease of setup as you would get from a commercial software as a service provider. So

[14:45] that's a bit of an issue, and people have used this very successfully, and there are even community projects that help you get over that initial step. So, for example, there is a Prometheus Django kind of module that you can use. That does a bunch of this work for you, it provides you with middleware and it instruments your app. However It like the support and well how often these are maintained and updated and how well they might fit your specific application. So how much customization you'll have to do varies quite a lot because you know that that that in particular is just a community project. So how whoever's got time, you know, it's got like two, three hundred commits. This is how you actually install it to give you a quick idea.

[15:32] So you just got you put it Django Prometheus if you're using that and installed in Yeah, in installed apps, and you pop the middleware in there and this is gonna be very similar for what I'll show you later And of course and you also do its URLs because it's a pool, what happens is it exposes an endpoint called slash metrics. So that that's what this does And from then on the Prometheus server that you configure separately can pull from here. So it's really easy, really simple to install. It's basically settings and URLs, a couple of lines, and you're done. And that's what the built-in dashboard looks like. And this is what Grafana looks like.

### [16:17] Elastic APM

**Speaker 1:** [16:17] With it. So it's pretty reasonable. You know, you got your responses, response codes, and kind of breakdown of requests and there are more there is more like there's the 95th percentile, 99th percentile and so on. that you can do with this. So that's pretty good. Again, the only thing with it is that it doesn't do logging and Yeah, you may need to do more customization with it, I think at least. So here we get on to Elastic APM. So far what we had from that Slide that from this slide is you have the software as a service providers down at the bottom right where you literally just install something, you put in an agent.

[17:06] into your app and then it automatically sends all the information and you know everything is cool. You go and log into the dashboards and you understand everything about your application. Or you know that's the idea at least. Then we have Prometheus, which is very powerful, very flexible, but you might need to do some more setup to fit it to your specific app So that's where ElasticAPM for me came in. And actually that's one of the reasons I joined the company was because I was excited about this project. Because what happened is they bought Upbeat and then with rather you know a lot of flair they open sourced almost all of the code of this commercial SaaS provider that had been in development for five years

[17:51] What they didn't open source but is still free to use is the user interface, which you will see shortly. However they did nonetheless provide an in for the first time I guess or at least as far as far as I'm aware for the first time provide an entirely free solution that's like commercial grade has been developed for several years And you can just use it. So the advantages of this is that you have more consistent support and you also get the logs in the same place. And I guess it's pace of development when it comes to specific integrations is probably greater. Like you know, it has. NET and Ruby and lots of stuff that might otherwise

### [18:37] Transaction Tracing

**Speaker 1:** [18:37] fall behind a little bit or not just not continue development at the same pace. So Let's have a have a look at some actual integrating. So this is the app that we're instrumenting. It's a relatively simple app. It has products, it has some orders, and it has some customers. We're gonna look at the products page. Here.

[19:55] Lovely. That's what happens when you All right, number one. Let's go with this. So that's what APM looks like. And actually so this is because we're talking about APM, I'll start from here, but then I'll show you what the rest of the whole thing

[20:42] looks like so you kind of get an impression of what the entire monitoring solution actually is all right so This is not what I originally intended to show you, but unfortunately I have put aside the link somewhere to the actual demo and I have closed the tab with it so we're going to look at this one. So what this is, is you're probably familiar with this, this is a similar screen to what SaaS vendors will offer you. You have your transaction, you have the full details about your transaction, like the particular request, the response, the no the operating system information

[21:28] as well as being able to add custom tags. So here you can do stuff like customer tier. This is a you know very high value customer. Or you and user you can have things like the specific email address or user ID for the user. And then the interesting part, of course, is the actual tracing. So here in this case, this is an Elasticsearch request. So we can see the body of the search as well as the specific stack trace. So we can see all the way through the libraries and I do have a Django demo, but this is a Flask one. You can see the entire stack down through the libraries, down through Green Unicorn.

[22:14] And you can see exactly which part is taking how much. It also supports salary, so it supports asynchronous workers and it they will just turn up as soon as you instrument it And it supports SQL as well. So it will break out your SQL queries in the same way that we have this Elasticsearch bit here. It will break out the SQL too. In fact, let's have Quick look and let's see. This is not Python, but Let's see if I can show you that. Yeah, okay. So that that one is a bit more interesting. So you can see what's going on here and I assume a lot of you would have

### [22:59] Logs and Kibana

**Speaker 1:** [22:59] seen similar stuff in your applications. So the thing that I like about this besides that that it's that it's free is that it combines performance monitoring with all the rest of the stack. So we start from something like this. So this is Just your regular unstructured logging. So that's basically file beat, which is a log shipper application, so that's sitting on every server or you know everywhere where it needs to sit and it's just tailing specific files. It has a lot of defaults, so it's pretty easy to configure.

[23:45] Like you don't have to do a lot of configuration to tell it. get these files and these files and these files. It tends to find Apache and Nginx and all of your standard applications. So this is searchable as well. And yeah, that's you know, Elasticsearch, it's pretty good at that. So Then from unstructured logging you we can go to a more structured logging or we could if it was the demo if I prepared originally at least

[24:30] So as you can see it's pretty easy to set up a visualization in Kibana. Like the discover. Oh, that's a link. Bless you.

[25:20] I guess this is what happens when you lose your Plan demo. Okay. The thing is this definite so this is the official like the kind of core last search demo that's Kibana demo that's used by you know the entire company of over a thousand people so I think what's happened is that something has actually happened to it.

[26:07] It's Not just me, and this is always full of data and it processes about well it processes thousand thousands of requests per second. So There is definitely a problem here, but very possibly one that I will not be able to fix on my own on stage. So Let's have a let's trash that. Let's just try that again.

[26:57] So you generally simply select the file beat index and then you tell it what times what field it should use. in order to kind of separate the records in the index and to be able to get structured logs out of the unstructured information. And then ideally when you click discover, I wait a second, it could be simply the no Aha, right, okay. Yeah, so this should definitely be full of data. So something has gone wrong with our official demo. So there we go. Why not? On stage, you know, that's that's totally normal. Right.

[27:43] So that's the structured logging view. Woo. Right, cheers. Thanks for the support. Okay, so the probably yeah something has happened to our demo. I just got a message. Excellent. Very timely. Right. So never mind. So we the cool thing about this is that It allows you to do a whole bunch of interesting things with this information. So for example, we can see what we are running.

[28:29] So what the container name is And you you'll see in a second what this little table button is doing. Okay. So this is pretty cool. So I would have really liked to have this when I was just looking at logs and you know like trolling through log files. What this does is it takes one field out of the entire record and then it shows you the same field in a summary view. So as the records are collapsed, you can kind of go through and see So, you know, this is the first thing is the name of the container. So this is the actual web application and you know this is an Nginx instance and these here are different pods

[29:15] So in this in this case this is a Kubernetes setup, not what I was intending. But so This is super cool because you know you can do things like log levels so you can easily scroll through and find all the warnings or errors. You can filter this The additionally you can sort by pulling out specific fields. So purely as like a log navigation tool, it's very powerful. And yeah, it's it's pretty cool. So you get unstructured logging in logs, you get structured logging in here in discover and you get application metrics in APM.

[30:03] You can do a lot of other things with this as well. So in Visualize In visualize, you can define visualizations. And there should be about 300 here. So you can kind of build your own dashboards. So here is the bit where you can combine business metrics with your application performance metrics. So for example, you know, here is where you could plot. like sales on the one axis and 95th percentile performance you know under one second or however long it takes on the other axis. So which is pretty important, right? Because obviously that's how we relate what we do to the rest of the business that we are operating in.

[30:56] So far You've seen Elastic APM, you've seen the rest of Kibana, so you've seen logging kind of combined with that And you've also seen Prometheus and how to how to integrate that. So for Elastic APM, the way you integrate it is pretty similar. to Prometheus. Interest of time I'm not going to try that demo just 'cause yeah too much. But

### [31:41] Deployment and Advanced Features

**Speaker 1:** [31:41] What the it's it's basically extremely similar to Prometheus. What you need to do is you run Elasticsearch, you run Kibana, you run the APM server. Which are pretty much all Prometheus in the case of Prometheus and Grafana. So you run Elasticsearch, you run Kibana and you run the APM server, and then you instrument your apps with the APM client or the APM agent, which is simply pip install Elastic APM. And extremely similar to Prometheus, you simply show them you simply shove it into installed apps and you install some middleware And that's about it.

[32:27] The extra thing that this does is that I guess you can't absolutely do with Prometheus, you just need to pull in more components and configure them more. The extra thing that this does is that it also allows you to in the same manner instrument RUM, so real user monitoring. So sort of this is what I showed you so far, is all on the server side But there is also monitoring you can do on the client side. So when you yeah, so you kind of know when the JavaScript has loaded and at what points it has loaded, how long it took, how long certain parts of the page took to really load. And how long did the did it take for the user to get anywhere useful? So this is included as it is in most

[33:14] software as a service vendors. So additionally the kind of the other feature that's worth mentioning that I would have f like to show you, but there we go. Is distributed tracing. So if you have microservice architecture or you simply have an architecture that has enough components that they start calling each other in some manner APM supports this. Actually, I wonder whether I still can show you a picture because it's you know it's worth a thousand words. This is like really basic mode here.

[34:01] Yes, the different colors are different services. So the blue is a Ruby service calling a Python service in green Which calls a Java service in purple, which calls a Go service in red. And this is supported automatically. So There's like there's a complicated bit of inference that the APM server does when you instrument all four apps. with the Elastic APM agent and you just instruct them to send information to one server, it will infer that they're connected together. Which that's a pretty cool piece, I think. And Yeah, distributed tracing I should say

[34:48] is pretty standard nowadays. You would expect it from software as a service vendors. But hey you know now you can get it for free. Now I do keep saying for free, and it is, but of course it's only for free in the sense that now you have to put in the effort to maintain it and it's like you know, it's the old the old problem of who monitors the monitoring and who watches the watchers. So as most like I guess most product companies if you're you know like a startup or small to medium sized project, you don't really want to deal necessarily with the configuration and hosting of this stuff. So what you do is you go and get it somewhere.

[35:34] And there are multiple providers, Elastic sells this, but for Prometheus there are multiple cloud providers as well. So you can totally do this. And I expect not sure but I expect that the pricing of this will remain low because the actual underlying code is free to use and the majority of it is open source. So that puts some pressure on my company, you know, not to not rush up the prices too much. Whereas obviously and proprietary environment that pressure doesn't exist. You just get charged for the value right that you get out of it. So yeah, so

### [36:20] Open-Source Collaboration

**Speaker 1:** [36:20] I think it's a pretty cool project. It's a pretty like advanced one and I do really like that they went out there and they published something that was of kind of this sort of commercial great where it's just you install it and stuff just happens automatically And but the I do still feel that there is a fair bit missing from this space and like you know we've been talking about logging, monitoring and observability and this sort of logging and metrics in particular now for several years and

[37:05] In terms of actually innovative thought in this space, I haven't seen it progress that much, although there are some exciting new projects. That may progress this so we we'll see how that turns out but more importantly what I would like to see a bit more of is more collaboration between different open source projects. So you know Elastic imports Prometheus data and you can use Grafana with Elasticsearch. But ultimately I've seen the kinds of stacks people have in the wild and they're they're you know they're pulled together from all sorts of components. You know, you have Fluent D and Kafka for processing your messages and like

[37:52] It j just all sorts of stuff. And it's you know open source is supposed to solve this, right? It's supposed to not be this pain in the ass where if you want some component like Elastic APM you now suddenly need to migrate to the whole Elastic stack but you're currently using Prometheus So I would like to see more collaboration between these things and that can really only happen if there is pressure for it and if people actually collaborate on metrics. If you care about metrics, if you care about monitoring. And you know, you maybe don't that much. Maybe you just want something to use that's easy and relatively low cost, who knows. But if you do,

[38:38] please write to me and Also please tell me what your setups are because like the more data there is on what the setups are and I do plan to republish this information when I get enough of it. The more data there is on what the setups are, then the more obvious it's going to be to potential you know open source maintainers. that there is demand for this thing. And I think that there is demand for this because even in like in a small size project you may start with one stack, but soon enough even a medium-sized project is a couple of years old, you move to a jumble of different technologies and you know initiatives like open metrics which came out of Prometheus kind of you know give you this you know

[39:25] base level of cooperation and there's also the Elastic Commons schema which is also an open thing. But it's this is not quite there yet. I think we could do a lot more on open source collaboration. So thank you very much for listening to me and I'll just take some questions if we have time.

### [39:51] Questions

**Speaker 2:** [39:51] Awesome.

**Speaker 3:** [39:56] We do have a little time for questions. Again you can we can ask questions on Django Con QA on Twitter or you can do DjangoCon on the IRC. It is always awesome. The last two talks are open source in the wild. It's so cool. So ask some cool questions about it.

**Speaker 1:** [40:19] Go for it.

**Speaker 2:** [40:21] Okay. Hey, thank you for the nice talk and for your perseverance against adversity. I was wondering if the APM server had an API that could be queried from the outside, for example Just random example, a Raspberry Pi with one of those green yellow and red lights. And if my server load, for example, goes over five the light on my desk turns orange because it's getting information from that APM server.

**Speaker 1:** [40:54] Yeah, totally. Yeah. So that that's exactly how it works. And I mean what you saw there, Kibana is reading often APM The APM server is entirely open source and an entirely separate component. So yeah, you can totally do that.

**Speaker 2:** [41:10] Thank you

**Speaker 4:** [41:12] The internet may defy your demos, but the internet has questions as well. Can you compare the resource requirements for APM and the ELK stack behind it to other options like Prometheus and Sentry? ELK has somewhat of a reputation of being resource hungry. Does that how much re-ho how big a setup do you need to have to be able to log a small or medium-sized site?

**Speaker 1:** [41:36] Yeah, sure, yeah. So that's true. The for a APM is a kind of very different project from the core Elasticsearch. I probably not gonna really go into saying well this is faster than the other because I do work for one of the companies. And I you know, I mean like I I'm a web developer, but like I don't want to necessarily say it's faster because it also depends on the situation. Right and like what load you put it in and what per the particulars are. However, the APM team takes the benchmarking pretty seriously. I mean that's one of the good things about it being an open source component that has the backing of a commercial company, right? Because they can spend weeks benchmarking in very specific situations, right?

[42:21] And they do. So as far as I know, they're I know they're very proud of like how small the percentage overhead is. So as far as I'm aware, it's It's in a good class of percentage overhead. So it should be pretty fast and it's it's very different from the Elasticsearch. Piece of software which yeah you can like if you're a small project what I would probably do personally, right? This Yeah, it involves giving money to Elastic, so take a grain of salt, obviously. But I would probably go and just go on Elastic Cloud and the like cheapest tier that we offer and that offers APM

[43:06] and it's all fully set up. So you just send it stuff. You just instrument and send. So that's what I do personally.
