Tips&Tricks - Adding a prefix to git commits automatically

This time I decided to write something, which is not Azure related, yet some people should find it interesting. To make a long story short - when working in a team using a git repository, we often agree on different kind of conventions, which help in keeping it clean and allow for easy integration with other tools(like issue trackers). The downside of such agreement is mainly a need to remember all structures and prefixes. In this post I show you how automate at least part of it - it's not something brand new, still I tried to present something elegant and easy to use.

git hooks

git has many cool features and hooks are one of them. If you haven't heards about them -  those are simple scripts written in shell(though you can use other languages like Python also), which are executed at the specific moment while working with your repository(like opening a commit message window). To make hooks working, they have to have a specific filename and must be stored in `.git/hooks` directory of your repository.

To start working with git hooks the easiest way is to initialize a dummy repository:

/
$ git init

and then go to `.git/hooks` repository and copy full content of the directory. When you open any file, you should see an example of a hook with some comments. Take your time and read them carefully since most of them provide some useful information.

Modifying a commit message

This time we'd like to modify a commit message. There are a few hooks, which could help here, but I decided to go for prepare-commit-message. I won't go into details here why this particular hooks has been chosen by me - commit-msg would fit here also. Personally I found it the most semantically correct for my purposes.

To modify a commit message you could use following shell script:

/
BRANCH=`git branch | grep '^\*' | cut -d '/' -f 2`
TASK=`echo $BRANCH | cut -d '-' -f 1,2`

echo -e "[$TASK] \n$(cat $1)" > $1

This script fetches a branch name, splits it and extracts two values from an array(which in my case are the project identifier and a task number). Let's say you have following branch name:

/
feature/TIMP-1-this-is-my-project-one

if you go to your git client and create a commit with a message This is my message!, you'll get following result:

/
[TIMP-1] This is my message!

How cool is that?

Enabling git hooks

All right, actually I lied a little - having only a script won't make, that a hook works. What you have to do is the following:

  • make sure a hook has a correct name(without .sample extension)
  • lies in .git/hooks directory

Using a script from an example your .git folder should contain following file:

  • .git
    • hooks
      • prepare-commit-msg

Now it should work flawlessly.

Summary

git hooks are great way to automate many things related to your daily work with a git repository. I strongly encourage you to dive deeper into this topic and automate as many things as you can.

Monitoring your Function App with ease

This post was created thanks to great support from people directly involved in Azure Functions in Microsoft - Donna Malayeri and Paul Batum

Introduction

Azure Functions make developing small services easier, especially with all those triggers and bindings, which allow you to skip writing boilerplate code and focus directly on your business needs. They are also provided with a possibility to "pay-as-you-go" with Consumption Plan supported. Currently many people take advantage of this feature and try to make the most of it by not exceeding a monthly free cap of executions and execution time. What if you'd like to monitor somehow how many times you function's been executed? Fortunately there're two ways of doing it and both are a piece of cake.

A graph presenting executions of a function triggered once per 5 minutes

Using Azure Portal

The easiest way to monitor your Function App(unfortunately you cannot monitor each function separately, at least not now) is to go to portal and check metrics of an App service plan, which is used to host it(to be more specific - its metrics). To do so I'll quote Paul directly here:

/
> There appears to be a bug that is making this harder than it should be. Try the following steps...
> Open Function App. Platform Features -> All Settings -> Click on Function Execution Count graph -> uncheck count, check units.

By doing those steps, you should be able to see a chart with a metric selected:

Weekly metrics of my function

There's one gotcha here however:

/
> Function Execution units are in mbmilliseconds, you'll need to divide by 1024000 to get gbsec.

One more thing - there's now way to know what's the aggregated value e.g. from a month - for now you have to do it on your own.

Using API

There's one more way to check the metrics - using a REST API of Azure Monitoring. This method is described on StackOverflow also by Paul and with a direct reference to walkthrough of Azure Monitoring REST API. The main idea is to call API, which will return to you something similar to following:

/
{
  "value": [
    {
      "data": [
        {
          "timeStamp": "2016-12-10T00:00:00Z",
          "total": 0
        },
        {
          "timeStamp": "2016-12-10T00:01:00Z",
          "total": 140544
        },
        {
          "timeStamp": "2016-12-10T00:02:00Z",
          "total": 0
        },
        {
          "timeStamp": "2016-12-10T00:03:00Z",
          "total": 0
        },
        {
          "timeStamp": "2016-12-10T00:04:00Z",
          "total": 0
        }
      ],      
      "name": {
        "value": "FunctionExecutionUnits",
        "localizedValue": "Function Execution Units"
      },
      "type": "Microsoft.Insights/metrics",
      "unit": "0"
    }
  ]
}

Once you have the result, it's easy to write a custom tool, which will calculate all metrics and give you an insight into your's functions performance.