cinek810 / snow-grafana-proxy

Connector for grafana simple-json data source and ServiceNow incidents retieved over ServiceNow API
MIT License
23 stars 5 forks source link

Lambda Python version #6

Closed jangaraj closed 5 years ago

jangaraj commented 6 years ago

Could you create AWS Lambda python version, please?

cinek810 commented 6 years ago

You mean AWS Lambda deployment package? Shouldn't be an issue. I'll add it.

jangaraj commented 6 years ago

Not a package, just a function:

cinek810 commented 6 years ago

I don't know AWS Lambda very well, why .zip package with all features does't meet your requrirements? Requests, json and sys packages are a must. Specification of queriees in environment variables will be a mess. For something very specific you can hardcode it starting with the version from 1st commits..

jangaraj commented 6 years ago

Functions should be fast because you are paying for execution time. And execution (daemon in your case) + logging (logging library in your case) is handled by AWS infrastructure. Sure, you can use json, requests and sys, etc.. Just try to minimize them, because you will have to upload them if they are not available by default. For example, you don't need calendar, because you can use already imported time:

>>> int(time.mktime(time.gmtime()))
1532200528
>>> calendar.timegm(time.gmtime())
1532200528

Example lambda function:

import json
from botocore.vendored import requests

def lambda_handler(event, context):
    if os.environ['DEBUG'] == 'true':
        print 'DEBUG: Starting'
    r = requests.get(
         url = os.environ['SERVICENOW_URL'],
         auth = requests.auth.HTTPBasicAuth(os.environ['SERVICENOW_LOGIN'], os.environ['SERVICENOW_PASSWORD']))
    snowJson = json.loads(r.text)
    # transform serviceJson into grafanaSimpleJson
    if os.environ['DEBUG'] == 'true':
        print 'DEBUG: response ' + str(grafanaSimpleJson)
    return {'statusCode': 200, 'body': grafanaSimpleJson}
cinek810 commented 6 years ago

Is the execution time measured by cputime or just walltime ? The whole concept is that grafana makes calls to snow-grafana-proxy when refresh happens, so it has to keep listening on requests from grafana. Is it possible to configure aws lambda to start execution on tcp/http request ? If yes is it allowed to keep memory allocated between executions?

jangaraj commented 6 years ago

Total executions time. Minimum billable time is 100ms. Lambda function doesn't need to listen. There is another AWS component which listens (API Gateway) and which forward request to lambda (event, context variables). The function must be stateless, so if you need state, then you have to save it (DB, global cache). If you are thinking about the cache, then again you don't need to implement it - it's one click setting in the API Gateway and response will be cached.

cinek810 commented 6 years ago

Sounds cool. Are those components available as free tier if I need it for test/dev?

jangaraj commented 6 years ago

Yes:

cinek810 commented 6 years ago

WIP. Pushed some initial attempt to lambda branch.

cinek810 commented 6 years ago

@jangaraj Do you have time to give it a try? I think that current state of lambda branch is workable, however, the main issue may be slow reply from ServiceNow which may make this expensive ~20-30 s per call for my developer instance.

jangaraj commented 6 years ago

Thank you, my quick notes:

cinek810 commented 6 years ago

@jangaraj Two mentioned issues addressed. Logs are visibile - at least when I use test button over webinterface. I'm not sure if with apigateway it's possible to get the query URL and select function in code - I tried to get context where I had a hope to find those details but it wasn't there.