You shall not push - branch policies in VSTS

When working on a codebase with a team, you always want to make sure, that everything is kept clean and works smoothly. You have git-flow, you have code reviews - they ensure, that everyone can work without impacting others and the main branch is secured. There's one issue however - by default you cannot force team members to go through the whole process - creating a feature branch, pull request, code review. Fortunately VSTS allows you to set a branch policy, which will ensure, that no one breaks the rules.

Setting a branch policy

TO set-up a branch policy just go to Code->Branches page. Choose whichever branch you want and select Branch policies item.

You'll see a page, where you can choose to protect this particular branch. When you select the checkbox, you'll see different options to make sure it is secured. We'll go through each one to get a basic understanding what it gives us.

Minimum number of reviewers

It allows us to define what is the minimum number of reviewers to actually complete a pull request. What is important here is Allow users to approve their own changes checkbox - if you want to force, that someone has reviewed a PR, make sure it is not checked!

Check for linked items

Useful when working with VSTS issue tracker. Allows you to block a PR if a work item hasn't been linked to it.

Check for comment resolution

My favorite. Forces an author of a PR to make sure, that each comment has been reviewed and accepted. 

Build validation

Allows you to link a build definition to queue a build for a PR to make sure, that feature branch passes through the whole pipeline. No more broken builds!

Results

When a branch policy is set, let's try to do following thing - push a commit directly to a develop branch(or any other branch which is protected) and complete a pull request.

Pushing a commit directly to the protected branch will result in an error

In this case both build and approvals weren't finished

Summary

As you can see in VSTS you can easily set a branch policies, which will help you secure your main branch from broken features. What is more, they will ensure you, that each team member follows the same process and no change can affect other team members.

 

 

Digging deeper again - querying Application Insights data

In the one of the previous post I presented you the basics of using Analytics in Application Insights. Just to remind you - it's a tool, which allows you to get much more detailed information regarding how your application perform and behaves. In this post I'll try to encourage you to use this tool on daily basis, especially when something is clearly wrong and you don't want to click through all those tabs in Azure Portal.

Quickly finding an exception

Let's consider following example - you're informed by your client, that he or she got an error saying Object reference not set to an instance of an object. You know the time and assume, that we're facing NullReferenceException here. What could you do to quickly find more details regarding this exception? Let's consider following query:

/
exceptions
    | where timestamp >= ago(24h) 
        and type == "System.NullReferenceException" 
        and outerAssembly 
            contains "MyProject" 
    | project method, details, timestamp 

This query will project a simple result containing a method in which an exception was thrown, details(like inner stack) and the exact time when it occurred. You can modify it and use different timestamp to get results, which will satisfy you the most.

Finding pages which are loading too slow

This is another example - you have a SPA and you'd like to know, which pages are loading longer, that a fixed threshold. Fortunately we can use a really simple query, which can help us here:

/
pageViews
    | where duration > 500

Yes, this is really it. Of course you can extend this query and use other metrics to further filter the data like:

/
pageViews
    | where duration > 50
        and operation_Name == "some_operation"
        and timestamp > ago(1h)  

Or even visualize your data to be aware which slower requests are really the slowest ones:

/
pageViews
    | where duration >= 250
    | render piechart  

Custom metrics

You can use predefined metrics to gather some information about your application, but where this tool really shines is the possibility to query custom metric. This allows you to easily build your own reports and really focus on what you're interested in.

For example I'd like to know how many times given method is called so I can be aware of all potential hot-paths and focus my optimization efforts on it. To do this I need two things:

  • a tool, which will decorate methods calls with a custom metrics logging(AOP/assembly weaving will help here)
  • custom query in Application Insights Analytics

Let's say, that this decorator will log one thing - a method name. Now in Analytics we could create a query, which would use this custom metric to show us which method is called the most.