Friday, April 10, 2015

Cognitive overhead of email

Looks like these days bitching about email is my favourite topic. What can I do no matter what I do I just cant keep up with email flow.  The new kind of emails just keep increasing, I was doing a good job 2 years ago and close to inbox zero daily and was writing a lot of code but two things have increased the email flow to me recently:

  1. Internal reports about production montioring from Newrelic, haproxy, exception analysis by team. 
  2. Review requests.  This is a recent one. Most review requests are sent to Java review group and somehow I atleast need to spend 1-2 min on every email to first decide whether this is for me or not.  Even if there are 30 review request per day it consumes 30 min of my time just to make sense of it. Daily when I see 100 emails to be answered I feel disheartened that I cant write any code today.  
 Both above items are necessary evil, #1 has increased production stability so my weekends are less busy.  #2 I am hoping eventually will increase code quality and lead to more ownership and processes that would lead to a self correcting system  requiring less involvement from leads.

But I need to do a better job or come up with some other way to do this. Its almost 5:00  and I had two 1 hour meetings, 1 Elastic search node down that consumed 1.5 hours and 2 team calls 30 min each. And I cleared some 60 emails. The only code I wrote is removing some junk code as it was causing some exception when a penetration tester was testing that flow.

Feeling frustrated as need to do something about this.  This Slack killing email sounds interesting.

Saturday, February 28, 2015

New relic and statuspage.io integration

Monitoring tools exposes a lot of data and we use Nagios, cacti, graphite,newrelic, mixpanel, flurry, boundary and many more tools.  But one of the ask for Support and marketing teams is how can they internally know if something is wrong. We cant expect them to wade through so many systems and so many applications to make sense of what is operational and what is not.  For e.g. we use a lot of services to serve the cloud filer server solution and this is the first page of status of our services in new relic and it spans 2 pages.

Support team and management relies on Operations team to notify them if an issue is on going. To make this easy I did a Proof of concept integration application responsible for serving main website with Statuspage.io.  The idea is simple

  1. Create public metrics in statuspage.io that are human readable.
  2. Query new relic and various systems for application status.
  3. Map new relic green/red/yellow status and other system status to statuspage.io status.
  4. If there is a status change then update statuspage.io metric status.
  5. Statuspage.io lets user subscribe to status change via SMS or email so support can  signup for that.
  6. Once this is fully baked then we can make this public and let customers do the same.
 

Now this is a proof of concept so I wrote a python script only with new relic integration with one application. I setup a cron job that runs every 2 min to do this integration. But we can enhance this script to derive status from various tools like new relic/nagios/boundary and map it transitively to human readable status. We can then even load this page up in various Tv screens in all offices to improve visibility.

The script I used to do the integration was for proof of concept and I cooked up in 2-3 hours including api research so use with caution.

import os
import sys
import requests
import logging
from logging.handlers import RotatingFileHandler

spiPageId = "PutYourPageIdHere"
cloudComponents = ["Metadata APIs","Web Interface","Webdav","Sharing"]
nrToSpiStatusMap = {"green":"operational", "yellow":"degraded_performance", "red":"major_outage"}

def getNewRelicApplications(apiKey):   
    interestedApps = ["cloud","mobile"]
    status = {}
    url ="https://api.newrelic.com/v2/applications.json"
    headers = {'X-Api-Key': apiKey}
    logging.info(url)
    r = requests.get(url, headers=headers)
    json=r.json()
    for application in json["applications"]:
        if application["name"] in interestedApps :
            status[application["name"]] = application["health_status"]
    return status

def getSpiComponents(spiApiKey):
    url ="https://api.statuspage.io/v1/pages/%s/components.json" % spiPageId
    headers = {'Authorization:': "OAuth %s" % spiApiKey}
    logging.info(url)
    r = requests.get(url, headers=headers)
    json=r.json()
    return json

def updateSpiComponent(spiApiKey, componentId, status):    
    url ="https://api.statuspage.io/v1/pages/%s/components/%s.json" % (spiPageId, componentId)
    headers = {'Authorization:': "OAuth %s" % spiApiKey}
    data = {"component[status]":status}
    logging.info(url)
    r = requests.patch(url, data=data, headers=headers)
    json=r.json()
    return json
   
def deriveSpiComponentToUpdate(spiApiKey, newRelicAppStatus):   
    spiComponents = getSpiComponents(spiApiKey)
    nrCloudStatus = newRelicAppStatus["cloud"]
    spiCloudStatus =  nrToSpiStatusMap[nrCloudStatus]
    componentsToUpdate = {}
    for component in spiComponents:
        if component["name"] in cloudComponents :            
            if component["status"] != spiCloudStatus :
                componentsToUpdate[component["id"]] = spiCloudStatus
    return componentsToUpdate

def updateSpiComponents(spiApiKey, componentsToUpdate):   
    for componentId in componentsToUpdate :
        status = componentsToUpdate[componentId]
        updateSpiComponent(spiApiKey, componentId, status)
            
def init():
    logger = logging.getLogger('')
    logger.setLevel(logging.DEBUG)

    log_format = 'Process-%(process)d %(asctime)s %(levelname)-8s %(message)s'
    handler = RotatingFileHandler("statuspage.log", maxBytes= 20 *1024 * 1024 , backupCount=10)   
    handler.setFormatter(logging.Formatter(log_format))
    logger.addHandler(handler)
   
# Main
if __name__ == "__main__":
    newRelicApiKey = sys.argv[1]
    spiApiKey = sys.argv[2]
    init()
    newRelicAppStatus = getNewRelicApplications(newRelicApiKey)
    componentsToUpdate = deriveSpiComponentToUpdate(spiApiKey, newRelicAppStatus)
    updateSpiComponents(spiApiKey, componentsToUpdate)    


Move fast break things but with monitoring

We run a complex system with multiple services and every 2 or 3 week we  update the Java applications.  I want to do it every week as most applications are stateless and can be patched anytime but the application serving the main website is using sticky session. We are working to make it failover sessions, once we do that, we can do mid week deployment and that will allow us to go faster than 3 weeks.  This week I pushed a huge infrastructure change related to user Id generation. I had asked ops team to check the status of new relic after the midnight deployment and it looked like this so everyone was happy.


I woke up and checked new relic mobile app and things looked ok to me. After finishing my morning routines I ran my daily exception report and one thing that caught the eye was 90K exceptions in last 12 hours in one of the files I had changed.  To gauge the impact I went in new relic and it showed me an error rate of 0.07 in one of the app

I then checked new relic and I see this blip that caused 90k errors, when the blip was there the status must have been red but then it was quickly green and Ops team didnt caught it


The issue was due to a wrong method invocation in one of the class used by this one specific application and it took just 15 min to fix after reproducing with a testcase.  So why didnt QA/UAT/automated tests caught it, well the issue was like a heisen bug and would occur only when the object is missing in cache. 99% of the calls would go to cache and only 0.07% were going to dao layer that had the bug.  I quickly made a patch and ops deployed it and I can see the issue is now gone.

Had there been no monitoring it would have been difficult to catch these kinds of bugs. New relic and internal monitoring tools makes life easy as it exposes anomalies.