Transcript for:
Introduction to Dapper at DapperCon 2021

[Music] hi everyone thank you so much for making the time to join me for this session i'm really honored to be able to be speaking to you all today at the first day of dappercon 2021. now my name is cecil phillip and i'm a senior cloud advocate at microsoft but what's more important is that i'm a huge huge huge fan of how dapper can make micro service developments so much easier now if you recall from the previous session you saw mark speaking with a few customers and they were talking about how dapper can give them the flexibility that they need to be able to solve some of the concerns they have inside of their infrastructure well in this particular session i want to take a little bit of a step back and instead we're going to focus on what that hello world getting started experience feels like inside adapter so i want to show you how to get the dapper tools and the cli installed on your machine we're going to see how you could quickly dapperize an application that you probably already have on your hard drive right now then we'll explore some of the different building blocks that are available out of the box with dapper and then also i want to show you how you can combine tools like docker compose and visual studio code to create a really interesting local development experience as you're diving into the world of dapper so hopefully that sounds fun because well that's what we're going to do so as folks are starting to explore this world of distributed systems one of the things that you could eventually run to is something that i like to call the microservice matrix and if you've ever heard that from anyone else before just know that they got it from you okay but inside of the world of distributed systems and microservices there's just so many options for tools that will help you achieve anything that you want to do inside of your application so if you think about it if i wanted to do something like service discovery for example i might have multiple instances of multiple services and i need to know what services are available where are they what's their health status what path do i need to take to get there and there's so many options like i mentioned for you to be able to solve that particular problem you might also have the concern of doing you know key value stores and state storage for your different components right we have tons of options for that as well some other things you might want to do include public subscribe maybe i want to do secrets management centralized configuration observability there's tons of things right and each of them have a ton of different options for folks to choose from now the problem with having so many different options is that well now we have to take the time to really understand which one of these options is going to work best for us and you know as you know you have more things that you want to do that matrix is going to become a lot more complicated we're going to need to figure out how these components can play together but also we're going to need to figure out well do they support the operating systems that we're building and deploying on do they support the programming languages and the runtimes we want to work with do they have sdks and apis that we could use to integrate more deeply into our app what about later on if we decide hey well i don't want to use this anymore and i want to swap it out for something else well how easy is it going to be for me to do that like how much technical debt am i going to have to dig through to be able to switch out this component and is that work even worth it at the end of the day so for me this is where dapper really shines dapper provides a portable lightweight event-driven runtime that abstracts some of those common use cases some of those common tasks that a lot of microservice developers you know have to go through today but for me the main thing that dapper gives me is the ability to focus right i get the time to focus on the things that really matter i bring value to the business and that and you know in my case it's usually around creating features and functionality and solving issues that our customers really care about i'm not necessarily spending the time to worry about the infrastructure so as the team for dapper went down this path to kind of achieve some of these goals what it ended up doing was creating a programming language agnostic host diagnostic runtime that almost anyone can plan so regardless of whatever your development environment looks like you should be able to plug into dapper and use the tools that you're familiar with in the environment that you're familiar with as well now another thing to know about dapper is that it is also open source and it's very extensible so if for whatever reason there's a component or there's a feature that's missing then you could very easily contribute that back to the core product and now we'll all be the better for it which i think is amazing now if you recall i mentioned that dapper provides us a set of abstractions and the way it you know shows us what provides us these abstractions is through a concept called building blocks and each building block provides a particular task a particular set of functionality that micro service developers might want to use so if you take a look at the slide you can see here that we have building blocks that represent things like service to service invocation or service discovery we have state management public subscribe when we want to publish messages to a topic there's even trigger zone bindings for that reactive style programming model if you're interested in that there's actors observability application secrets and tons more but one of the important things that i want you to recognize here is that all of these abstractions are exposed to you via http and grpc so that means that as long as the programming languages that you use support any of these types of protocols then you'll be able to plug into the world of dapper without any concerns at all another thing that i think is really important to point out even though dapper has all these different building blocks this is not an all or nothing type of approach you can pick and choose the different components and building blocks based on whatever makes sense so that means that i could choose you know to use just the service discovery or the state management building block and not the rest or maybe i want to use all of them or maybe i don't want to use any of them at all right the choice is completely up to you again dapper gives you the choice to use the things that make sense for the application that you're building now the reason i keep repeating this is because i think it it's it's really interesting for all types of applications and not just distributed micro services type applications as well so if you think about things like legacy apps maybe i have an older application that you might want to start modernizing well dapper could be the path that helps plug these two worlds together says i'm continuing on one side to develop these very lightweight and efficient services my legacy application can still plug into that world and all it has to do is go through dapper now why don't we take a moment and dive into our first demo and i can show you exactly what it takes to get started using dapper now before you really start diving in adapter the first thing i want to do is show you this web api that's been built with python and fast api as you can see here there's really not too much happening inside of this api but i do want to show you what it looks like before we start dapperize it now i'm going to start running this in a command line and what i'm going to do is start executing some requests against this api now i'm going to use the rust client extension for visual studio code but if you feel like you could use anything like curl or whatever other client that makes you comfortable now i'm going to set my host to localhost 8000 i'm going to execute the requests and as you can see here status okay right everything looks great if i come down here and i execute this next method i want to change this query string i'm going to say hello dappercon oh we can see that everything looks just fine so let's go ahead and stop this and now what i want to do is add a little bit of dapper now if i come back to my command line notice if i type dapper it says command not found so i don't have the dapper cli or any of the tools currently installed on my machine i think the next thing that we should do now is just to head over to the documentation because i believe every great open source project begins with great documentation now i'm going to click on that getting started link i'll head over to install.procli and here you'll see the different instructions based on the operating system that you're using now i'm using mac os so i'm going to look for the instructions for homebrew and i'm going to copy those because homebrew is what i prefer to use when i install stuff now if i head back over to visual studio code i can paste that command i just copied and hit enter and what homepro is going to do is go ahead and pull down that latest version of the dapper cli and add it to the path on my terminal so now if i type dapper i should be able to see all the various commands that i can execute and i can see that a really cool ascii art so you know we're in business the next thing i need to do before i actually start running dapper is i need to initialize it on my system so i'm going to type in dapper init on the command line now adapter is going to do is pull down some default components and set up some configuration that we can start using if you take a look at the output inside of the terminal you can see that dapper pulled down the dapper d binary a placement container a redis container and also a zipkin container and these have already been configured and are ready for us to use now let's go ahead and dapperize our python application one of the available commands for me to use is the dapper run command so let's see how we could use that to adapterize this python application now if i type dapper run now if i'm going to run my application with dapper i need to give it an id so this is what we're going to refer to this application or the service as so i'm just going to call that api i got to let dapper know the port that it's running at so if you recall this application runs on port 8000 and the last thing that i need to do is i also have to specify the port that dapper is going to run at so dapper has its own http port and by default i like to set that to 3500 now i have to tell dapper the application i wanted to run and if you recall that's my python main application so i'm going to go ahead and run this and now you're going to see a lot of stuff being spit out to the console there's a few things i want you to recognize here one dapper has discovered our application running on port 8000 so that means it now knows how to get to it also if we start to scroll up a little bit in the logs you could see that metrics has been enabled tracing has also been enabled and also if i go up a little bit more it's loaded some of those components that we spoke about earlier so here you see it's configured the redis component for both pub sub and also for state storage so now that that's running let's get a little bit of space and let's open up this http request file again now i'm going to change this port and instead of us running on port 8000 i'm going to run a 3500 which is the dap report i'm going to send my requests and notice my body comes back and my status is still okay but one thing is a little bit different and you'll now notice that we have that trace parent http header and that's been included by dapper to enable some of those distributed tracing scenarios that it provides for us let's see what happens when we hit the next request notice that works just fine as well so with changing no code in my application we've just dapperized it and enabled a lot of really interesting features i will see how we can really make use of them as we go on in this session now before we move on there's one more feature i definitely want to show you because i think this is really cool i want to show you the dapper dashboard now this dashboard by default is going to run a port 8000 and if i open it up on localhost you'll see it shows me all of the different dapper applications that i have running in our case it's only our flask api for now if i click on this section for components the dashboard would show me all of the components that have been configured for my dapper application so right now we have a pub sub and we also have a state store i can go a little bit further and even click on some of these components to get a little bit more information about how they've been configured how long they've been running for i can even see that default yaml file that created them so if you want to quickly inspect what's going on inside of your dapper apps definitely check out the dashboard dashboard and it's included by default with adapter cli now let's continue moving forward and take a look at some of the components and building blocks that are available for us inside of dapper now that we have dapper installed and ready to go on our machines why don't we take a little bit of a deeper step to get a better understanding of how some of it works now dapper operates in what's called the sidecar model so that means that for every instance of every service that makes up our solution we're going to have an associated instance of that dapper sidecar process so here you see i have my app if i had 10 instances of that i'd have 10 instances of that dapper sidecar process running next to them usually on localhost and now once the instance of my application and also the sidecar up and running they could start communicating with each other over those grpc or http apis that we mentioned earlier but my application doesn't really need to be concerned about anything else outside of that environment right now if it needs to get pub sub if it needs to do state storage secrets management or if it even needs to invoke a service on another instance of another service somewhere else within my infrastructure the only thing it needs to do is to communicate through dapper and though those dapper sidecars are going to be self-aware right they're going to find each other and they're going to be able to know who's going to be responsible for dealing with this request so here as an example you can see that we're going to make a request over to the cart service and we're going to invoke the method new order and notice how it's using that very standard http type contract the same thing that could be said for using state storage right i'm going to make a http request over the state and i want to get the inventory state storage and i want to retrieve the item 67. here's an example how you can do publish subscribe right like i can create a http payload using my http client of choice based on the programming language i'm using and i can send that over to this endpoint to publish a message on the orders topic and here now also you can see an example of how you can get secrets out of a secret store so maybe i want to store passwords or api keys or things of that nature now at this point you might be wondering well okay i see dapper can give us these abstractions but what about the concrete implementations that's implementing these abstractions and well that's where dapper components come into play and if you remember as i said earlier dapper is very extensible so for every building block in here we have tons of different options for concrete implementations for each of those building blocks so you can see for the state store we support a variety of different databases like cosmos db and redis and cassandra for publish subscribe and pub sub brokers we have support for service bus you know redis rabbitmq again and the list goes on right for every dapper component there's tons of different options for things that you could make use of now like i mentioned earlier dapper is open source so a lot of the components that dapper supports today were actually contributed by the community if you head over to the dapper organization on github and you go to the components.comtrip repo you'll be able to see tons of different components that you can make use of inside of your applications now i think it's time we dive into another demo and see a more concrete example of how we could start making use of some of these dapper components inside of a more real world application this next demo i want to show you is just a little bit more involved but i like showing it off because it has a lot of different dapper components working together to create a complete solution now contoso crafts is a storefront that has a list of products that you could choose from and if you want to you can click on the info button and get more information about a particular item or you can even go to the different items that you want and you can add them to the cart so as you notice here i can click add to cart as i'm doing that that increment button here at the top right side keeps going up if i click on that shopping cart icon you'll notice here to all the items that i added to the cart and i want to hit submit hopefully at some point someone's going to be able to get my order process it and those items will get shipped to my house now even though this looks like a simple application this is actually made up of three different micro services working together through dapper the first service is actually the web ui this is built with asp.net core blazer and its job is really just to display the interface that you're seeing right now the next one is actually the products api and that's responsible for giving us all this product information that we're seeing so everything from where the pictures are the description the votes and even the name of what the product is and then the last one is the checkout service and this job is just to listen to the background as new orders are being published to your queue and it'll process those orders accordingly now let's head over to visual studio code and see what this looks like inside of the demo 2 folder i'm going to open the source directory and here you can see i have three separate projects like i mentioned one is the website project the products api project and also the checkout processor project now if you're familiar with net you know we have this concept of a cs proj file and the cs profile is where we list all the dependencies for what we need inside of our applications like different packages we're going to use and things of that nature so as you can see here my products api is making use of mongodb because that's where a lot of that information is being stored from last one i'm going to show you here is the checkout processor now the thing that i want you to notice that's common between all of these different cs files is that there's absolutely no reference to dapper so then your question might be well how exactly does dapper get plugged into this application so that these services can talk to each other well that's what i'm going to show you right now now to get all these services running at the same time inside of my application i'm actually making use of docker compose and not only is it running my dapper components and my application but it's also running the infrastructure pieces that includes things like mongodb redis random queue and some other really interesting things now let's take a look at the dockercompose.profile now if you recall earlier when we were showing the slides i mentioned that that dapper runs using what's called a sidecar model so that means for every instance of your application you're going to have an instance of the dapper process running right next to it preferably on local host so here inside my docker compose file you can see that i'm specifying some options for that main website service but also right next to it i'm also specifying the dapper sidecar that's going to be attached to it as well if you look at line 25 through line 35 you'll see i have a command where i'm executing that dapper d binary and just like we did with the dapper cli i'm passing in an app id an app port but i'm also passing in some additional information so you can know about the components that i wanted to configure for me as we continue scrolling down inside of this docker compose file you'll see that i have a products api and just like the website i have some options here that tell docker compose how to build my application into a container but more importantly right next to it i have a products api dapper sidecar which has options that are very similar to that website dapper sidecar that we saw before as we scroll even further down you probably guessed it there's a checkout processor and there's also a checkout processor sidecar always try to remember for every instance of your application you're going to have an instance of the dapper sidecar and your application is going to be communicating with that sidecar to get access to all those different components and building blocks that we have configured let's head over to the web application and see exactly how some of this communication happens now inside of the startup.cs file you can see here on line 23 i'm configuring an http client to use localhost 3500. now this is the address that i'm going to expect that dapper sci car process to be running on so whenever i make a request for some services that's the end point that i'm going to use next let's open the services folder inside of that same project and you'll see that i have a file called dapperproductservice.cs inside of this class i have a method called get products on line 30. i'm going to make use of that dapper http client that we just configured so i create a new http request message i give it the path i add the dapper app id as an header and now i go ahead and make my request now once my sidecar process gets that request it's going to forward it along to that products api it's going to process that request get those results back from mongodb and bring it back to me but my web front end knows nothing about where that product api lives and this pattern is going to be the same for the state store pub sub and a lot of the other components that we use inside adapter speaking of other components let's take a look and see how some of the other ones work i'm going to open up this components folder and i'm going to look for the product list base now inside of here i should have a method that shows me how to actually add something to the cart and on line 49 you can see i have that add to cart method that takes the product id and the title of the thing that i want to add to the cart now similarly to how you saw us interact with service invocation i'm going to use the dapper api endpoint that's specific to working with state stores so you can see here i'm going to specify v 1.0 state i'm going to give it the name of the state store and the state that i want to retrieve so in this case i want to retrieve the cart state then i'm going to make sure if my card is empty or not i'm going to add items to the card and then when i'm done doing whatever business logic i need to execute at the end on line 87 and 88 i'm going to make another request using that same endpoint but this time i'm going to be doing a post request so i could save the state versus retrieving it last but not least let's take a look at our checkout now this could be found under the components folder in the checkout model i'm just going to scroll down here a little bit to where the code is and inside of our submit checkout model again you're going to see the similar pattern i'm going to use that dapper http client but if you head down to line 75 we're going to serialize that shopping cart content that we want to have published then we're going to make a post request over to v 1.0 slash publish slash the name of the pub sub and then checkout is the name of the topic that we want to publish to let's take a look over at the checkout processor and see exactly how it does this now inside of the checkout processor project i have a controllers folder and that controllers folder has this class called dapper controller so if we take a look through what's going on here you can see i have two methods one is the subscribe method and another one is that slash checkout method what's important here to know is whenever our application stands up and that dapper sidecar discovers it the sidecar is going to make a call to our app at the endpoint slash dapper subscribe and that's pretty much how it's going to check to see if our application is interested in listening to any topics in this case you can see that i want to listen to the topic called checkout and then any messages that come in from that topic i want you to pass it to the checkout route now if we scroll down a little bit that checkout route is assigned to that checkout order method so once a message arrives i'm going to deserialize that information that comes back in and then i'm just going to write out a simple lock to say that the order was received one thing you might notice here that's really special is that that message is being retrieved from the request using cloud events and cloud events is just the format that dapper uses by default as it's sending pub sub messages back and forth now really diving into cloud events is a little bit past what we're going to be doing in this session but i definitely recommend you check it out if you're interested in the standards compliant way of sending and receiving messages to any message broker okay now we've seen how our application code works how exactly do we configure dapper well actually if you recall let's head back over to our dapper docker compose file and if i was supposed to open one of these sidecars remember every time we invoke dapper d we're going to pass it a config switch to config.yml file and also the path to a components folder that has the configuration for all of the components that we want to have used inside of our dapper applications well that components folder actually exists right here inside of our project so if i was supposed to open it up you can see that i have that config file and i also have this folder that has a few components configured now if i click on this config.yml file you can see that this has some basic configuration for how we can set up tracing using zipkin and i'll show you that a little bit later as we go on let's open up this local folder now inside this folder i have a few different components that are already set up for us now now if i click on the rabbitmq pub sub.yaml file you can see exactly how this component is being configured to be used inside of the pub sub building block inside adapter looking at this configuration you can see that we have to specify things like the type of component that this is the connection information and then some other options that are very specific to rabbitmq let's take a look at the state store for state storage we're actually using redis and as you can imagine it's following a similar format we have to specify the type in this case it's state.redis and then in the metadata i have to provide some connection information of how exactly we're going to be able to connect to this instance of redis one of the things that you'll notice is a little bit different here is i have the host and port of my redis service but i don't have the password that i need to log into it instead i'm making use of something called a secret key reference and if you remember from the previous slides secrets are just another building block that we could use inside adapter not only we could use them inside of our applications but we can actually use them inside of our configuration files as well and you can see that we're specifying what secret store we're using on line 16 inside of this file now you might be asking how do i configure secrets well there's a yaml file for that and inside of that same folder i'm actually using local secrets now i wouldn't recommend that you use local secrets for production but it's good for development local secrets just keep your files inside of a json file on your machine where you could use it as a key value store so it's definitely not the most secure option but if you stick around for the rest of dappercon my teammate jessica dean has a really awesome session that's going to show you how you could production harden some of your dapper configurations let's head back to our web browser really quickly and see how dapper uses some of these components so if i head over to zipkin and i have this one exposed on port 9411 i can click on dependencies i can hit the search button and now i get this really interesting trace about the different services that are inside of my architecture and also what's communicating with what so if you look here you notice that the website is communicating a lot with the product api but it's also communicating with the checkout processor just not as often if you want to search for any traces or spans you can easily do so instead of find a trace section inside a zipkin all you have to do is enter your query and then go ahead and execute it now let's head back over to visual studio code the last bit i want to show you is how you could quickly debug these applications inside of visual studio code if you open up the dot vs code folder you'll notice that there are launch settings.json on tasks.json and inside of these have configurations that allow you to debug into a container so why don't we go ahead and do that so let's go and set a breakpoint right here inside of our checkout order and now i'm going to hit the run and debug section and notice i'm going to select the docker checkout processor attached right so i want to attach to that running container inside of my docker compose group so i'm going to select the contoso group and now i have to select what container i want so here i'm going to select the checkout processor notice at the bottom now it's asking attaching to a container requires the net core debugger in the container do you want to copy it sure why not yolo now the debugger should be successfully attached let's head back to the browser really quickly go back to our project i'm going to submit a checkout really quickly and in a few seconds three oh there we go notice that i've hit the breakpoint and i have full debugging capability right here inside of visual studio so notice i have my local variables i have the debug console available to me i could step through step over and inspect values inside of the editor now let's head back over to the slides now before you say anything i know i get it that was a lot we covered so many different topics and concepts in such a short period of time in that last demo but my goal there was to show you how amazing dapper could be for micro service development by just adding a few lines and a few files of yaml configuration you can see how we enable tracing and metrics and pub sub and tons of these other building blocks that we could use also i think being able to spin up that entire environment locally and then debug into a running container using docker compose and visual studio code is really cool so definitely had to show you that now if you're interested in learning a little bit more about dapper and i hope that you are definitely recommend that you head over to dapper.io make sure you check out the documentation and go through the getting started guide to see what it actually feels like to get you know get into dapper we also have communities across a lot of different platforms so if you're into discord make sure you check out our discord channel with a lot of folks are sharing tips and tricks and projects and blog posts and all the cool things that they're building and doing with dapper there's a youtube channel where there's regularly scheduled community calls with the team that give you updates about what's going on with the future of the project also the team is on github adapter is open source so you can see all the different components you can see all the check-ins you could read the code you can contribute and submit issues so i think that's a really great place for you to get plugged into what's happening and if you're a dot-net developer like i am i definitely recommend that you check out thedapperfor.net ebook this is a freely available ebook you don't have to put in your email address to download it you can even read it online if you wanted to but i think this is a great way for you to get started with dapper and even if you're not a dotnet developer it gives you a really good overview about how you could start putting applications together using dapper now i hope this session has been great for you i hope you enjoyed it please enjoy the rest of napr con i wish i could be there with you all in person but until we are again please take care and i'll see you all soon bye [Music]