Skip to content

Commit

Permalink
Add configure
Browse files Browse the repository at this point in the history
  • Loading branch information
seonsfx committed Apr 2, 2019
1 parent 928a465 commit 5a90b53
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
4 changes: 4 additions & 0 deletions signalfx_serverless_common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
import os
import datetime

from . import utils
from . import metrics
from . import tracing
from .version import name, version

def configure(source, fields):
utils.set_source(source)
utils.set_fields(fields)

# backwards compatibility
def wrapper(*args, **kwargs):
Expand Down
4 changes: 1 addition & 3 deletions signalfx_serverless_common/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,10 @@ def call(*args, **kwargs):
global ingest
# timeout for connecting = 1 and responding 0.3
ingest = sfx.ingest(access_token, timeout=(1, ingest_timeout))
# context = args[1] # expect context to be second argument

global default_dimensions
# default_dimensions.update(utils.get_fields(context))
default_dimensions.update(utils.get_fields())
default_dimensions['metric_source'] = 'common_wrapper'
default_dimensions['metric_source'] = utils.get_source() + '_wrapper'

global is_cold_start
start_counters = [
Expand Down
5 changes: 3 additions & 2 deletions signalfx_serverless_common/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
def wrapper(func):
@functools.wraps(func)
def call(*args, **kwargs):
source = utils.get_source()
function_name = os.environ.get('FUNCTION_NAME')
if function_name is None:
warnings.warn('FUNCTION_NAME cannot be found. Abort sending tracing')
Expand All @@ -18,9 +19,9 @@ def call(*args, **kwargs):
tracer = init_jaeger_tracer(function_name)

span_tags = utils.get_fields()
span_tags['component'] = 'python-gcf-wrapper'
span_tags['component'] = 'python-' + source + '-wrapper'

span_prefix = os.getenv('SIGNALFX_SPAN_PREFIX', 'gcf_python_')
span_prefix = os.getenv('SIGNALFX_SPAN_PREFIX', source + '_python_')

try:
with tracer.start_active_span(span_prefix + function_name, tags=span_tags) as scope:
Expand Down
16 changes: 11 additions & 5 deletions signalfx_serverless_common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@

from .version import name, version

source = None
fields = {}
dim_prefix = 'gcf'

def get_source():
return source

def get_fields():
return fields.copy()

def set_fields(fs):
for key, value in fs.items():
fields[key] = value

def get_metrics_url():
url = os.environ.get('SIGNALFX_INGEST_ENDPOINT')
if url:
Expand Down Expand Up @@ -48,3 +47,10 @@ def get_access_token():
token = os.environ.get('SIGNALFX_AUTH_TOKEN')

return token

def set_source(s):
source = s

def set_fields(fs):
for key, value in fs.items():
fields[key] = value

0 comments on commit 5a90b53

Please sign in to comment.