python opentracing with jaeger extract span not logging in UI using microservices - python

I have a problem using the jaeger open tracing project within our microservice system.
The config I use is as below.
config = Config(
config={
'sampler': {
'type': 'const',
'param': 1,
},
'local_agent': {
'reporting_host': '127.0.0.1',
},
'logging': True,
},
service_name='DB - API',
validate=True,
)
tracer = config.initialize_tracer()
active_span_source = FixedActiveSpanSource()
tracer_interceptor = open_tracing_client_interceptor(
tracer,
log_payloads=True,
active_span_source=active_span_source)
The origin of the trace is the same as this.
with t.tracer.start_span('Request Post') as span:
print(
f"Created span: trace_id:{span.trace_id:x}, span_id:{span.span_id:x}, parent_id:{span.parent_id}, flags:{span.flags:x}")
span.log_kv({'event': 'Request Posted', 'life': 42})
with t.tracer.start_span('GraphQl', child_of=span) as span2:
span2.log_kv({'event': 'Graphql Executed', 'life': 42})
carrier = {}
t.tracer.inject(span, Format.HTTP_HEADERS, carrier)
print(carrier)
This works fine and is logged within the UI and with printed results below.
Created span: trace_id:2b55203a8773aa14, span_id:77cceca94f4cfe74, parent_id:None, flags:1
{'uber-trace-id': '2b55203a8773aa14:77cceca94f4cfe74:0:1'}
But then when I extract {'uber-trace-id': '2b55203a8773aa14:77cceca94f4cfe74:0:1'} within another service it says a new span has been created as expected, but nothing is logged within the UI.
carrier = {'uber-trace-id': '2b55203a8773aa14:77cceca94f4cfe74:0:1'}
parentspan = t.tracer.extract(Format.HTTP_HEADERS, carrier)
print(t.tracer.sampler)
with t.tracer.start_span('TEST', child_of=parentspan) as span3:
print(
f"Created span: trace_id:{span3.trace_id:x}, span_id:{span3.span_id:x}, parent_id:{span3.parent_id:x}, flags:{span3.flags:x}")
span3.log_kv({'event': 'test', 'life': 42})
time.sleep(1)
span3.log_kv({'event': 'test34', 'life': 43})
time.sleep(30)
Print result:
Created span: trace_id:2b55203a8773aa14, span_id:af7fef928ff1cd13, parent_id:77cceca94f4cfe74, flags:1
With both Format.HTTP_HEADERS and Format.TEXT_MAP it is the same result.
Does anyone know why nothing is logged in the UI and how to fix this?
Thanks in advance.

Related

Accessing nested data in a supposed dict

Alright, I'm stumped. I have googled everything I can think of from nested Dicts, Dicts inside Lists inside Dicts, to JSON referencing and have no idea how to get to this data.
I have this AWS Lambda handler that is reading Slack events and simply reversing someone's message and then spitting it out back to Slack. However, the bot can respond to itself (creating an infinite loop). I thought I had this solved, however, that was for the legacy stuff. I am Python stupid, so how do reference this data?
Data (slack_body_dict print from below):
{'token': 'NgapUeqidaGeTf4ONWkUQQiP', 'team_id': 'T7BD9RY57', 'api_app_id': 'A01LZHA7R9U', 'event': {'client_msg_id': '383aeac2-a436-4bad-8e19-7fa68facf916', 'type': 'message', 'text': 'rip', 'user': 'U7D1RQ9MM', 'ts': '1612727797.024200', 'team': 'T7BD9RY57', 'blocks': [{'type': 'rich_text', 'block_id': 'gA7K', 'elements': [{'type': 'rich_text_section', 'elements': [{'type': 'text', 'text': 'rip'}]}]}], 'channel': 'D01MK0JSNDP', 'event_ts': '1612727797.024200', 'channel_type': 'im'}, 'type': 'event_callback', 'event_id': 'Ev01MN8LJ117', 'event_time': 1612727797, 'authorizations': [{'enterprise_id': None, 'team_id': 'T7BD9RY57', 'user_id': 'U01MW6UK55W', 'is_bot': True, 'is_enterprise_install': False}], 'is_ext_shared_channel': False, 'event_context': '1-message-T7BD9RY57-D01MK0JSNDP'}
There is an 'is_bot' there under 'authorizations' I want to check. I assume this will let the bot stop responding to itself. However, for the life of me, I cannot reference it. It seems to be nested in there.
I have tried the following:
def lambda_handler(api_event, api_context):
print(f"Received event:\n{api_event}\nWith context:\n{api_context}")
# Grab relevant information form the api_event
slack_body_raw = api_event.get('body')
slack_body_dict = json.loads(slack_body_raw)
request_headers = api_event["headers"]
print(f"!!!!!!!!!!!!!!!!!!!!!!!body_dict:\n{slack_body_dict}")
print(f"#######################is_bot:\n{slack_body_dict('is_bot')}")
print(f"#######################is_bot:\n{slack_body_dict("is_bot")}")
print(f"#######################is_bot:\n{slack_body_dict(['is_bot']}")
print(f"#######################is_bot:\n{slack_body_dict(["is_bot"]}")
print(f"#######################is_bot:\n{slack_body_dict['authorizations']['is_bot']}")
As you can see I have absolutely no clue how to get to that variable to tell if it is true or false. Every 'is_bot' print reference results in an error. Can someone tell me how to reference that variable or give me something to google? Appreciate it. Code is below in case it is relevant.
import json
import os
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
def is_challenge(slack_event_body: dict) -> bool:
"""Is the event a challenge from slack? If yes return the correct response to slack
Args:
slack_event_body (dict): The slack event JSON
Returns:
returns True if it is a slack challenge event returns False otherwise
"""
if "challenge" in slack_event_body:
LOGGER.info(f"Challenge Data: {slack_event_body['challenge']}")
return True
return False
def lambda_handler(api_event, api_context):
# Grab relevant information form the api_event
slack_body_raw = api_event.get('body')
slack_body_dict = json.loads(slack_body_raw)
request_headers = api_event["headers"]
# This is to appease the slack challenge gods
if is_challenge(slack_body_dict):
challenge_response_body = {
"challenge": slack_body_dict["challenge"]
}
return helpers.form_response(200, challenge_response_body)
# This parses the slack body dict to get the event JSON
slack_event_dict = slack_body_dict["event"]
# Build the slack client.
slack_client = WebClient(token=os.environ['BOT_TOKEN'])
# We need to discriminate between events generated by
# the users, which we want to process and handle,
# and those generated by the bot.
if slack_body_dict['is_bot']: #THIS IS GIVING ME THE ERROR. I WANT TO CHECK IF BOT HERE.
logging.warning("Ignore bot event")
else:
# Get the text of the message the user sent to the bot,
# and reverse it.
text = slack_event_dict["text"]
reversed_text = text[::-1]
# Get the ID of the channel where the message was posted.
channel_id = slack_event_dict["channel"]
try:
response = slack_client.chat_postMessage(
channel=channel_id,
text=reversed_text
)
except SlackApiError as e:
# You will get a SlackApiError if "ok" is False
assert e.response["error"] # str like 'invalid_auth', 'channel_not_found'
The structure of the data is:
{
"authorizations": [
{
"is_bot": true
}
]
}
So you would need to first index "authorizations", then to get the first item 0, and lastly "is_bot".
data["authorizations"][0]["is_bot"]
Alternativly, you could iterate over all the authorizations and check if any (or all) of them are marked as a bot like so:
any(auth["is_bot"] for auth in slack_body_dict["authorizations"])

Stawpoll can't create poll

I am working on a program that includes a strawpoll. As far as I can tell this code should work to create a poll however it is returning an error page instead of a poll. Here is the api
json = json.dumps({"title": "Question", "options": ["option1", "option2", "option3"]})
poll = requests.post("http://strawpoll.me/api/v2/polls", data = json, headers = {"Content-Type": "application/json"})
This is the url it returns
https://www.strawpoll.me/error?aspxerrorpath=/api/v2/polls
Unfortunately, strawpoll.me' api have been broken for quite some time now. You can still retrieve the polls but creating one redirects you to the error landing page.
Try posting your request to https://www.strawpoll.me/api/v2/polls:
data = {"title": "Question", "options": ["option1", "option2", "option3"]}
poll = requests.post("https://www.strawpoll.me/api/v2/polls", json=data,
headers={"Content-Type": "application/json"})
print(poll.url)
# https://www.strawpoll.me/api/v2/polls
print(poll.json())
# {'multi': False, 'title': 'Question', 'votes': [0, 0, 0], 'id': 16578754,
# 'captcha': False, 'dupcheck': 'normal', 'options': ['option1', 'option2', 'option3']}

What Promoted Object should I use when creating an AdSet with lead_generation optimization goal?

I'm using facebookads python api, v2.6.
I'm trying to create an AdSet with optimization goal = lead_generation.
This is my code:
ad_set = AdSet(parent_id = 'act_%s' % FB_ACCOUNT)
ad_set[AdSet.Field.name]= 'Teste AdSet'
ad_set[AdSet.Field.campaign_id]='6043402838999'
ad_set[AdSet.Field.status]=AdSet.Status.paused
ad_set[AdSet.Field.billing_event] = AdSet.BillingEvent.impressions
ad_set[AdSet.Field.optimization_goal] = AdSet.OptimizationGoal.lead_generation
ad_set[AdSet.Field.daily_budget]= 100
ad_set[AdSet.Field.bid_amount]= 1
ad_set[AdSet.Field.start_time]= '2016-07-01'
ad_set[AdSet.Field.promoted_object]=
ad_set[AdSet.Field.targeting]= {Targeting.Field.geo_locations: { 'countries': ['BR']},Targeting.Field.genders: [1],Targeting.Field.age_min: 20,Targeting.Field.age_max: 24}
ad_set.remote_create()
But when I run this I get this error:
Status: 400
Response:
{
"error": {
"code": 100,
"is_transient": false,
"error_subcode": 1885024,
"error_user_msg": "When creating an ad set within a campaign using the Body of an error/warning message. Title is: Promoted Object Missing objective, a promoted object must be specified.",
"error_user_title": "Promoted Object Missing",
"message": "Invalid parameter",
"type": "OAuthException",
"fbtrace_id": "B9hyZlpzS7O"
}
}
I tried to find any documentation about this, but could not. On the official docs I don't see LEAD_GENERATION on the promoted objects options:
https://developers.facebook.com/docs/marketing-api/reference/ad-campaign#Creating
Anyone had this problem?
In case anyone has the same issue, you have to use page_id.
The ad set must have its promoted_object set to the corresponding <PAGE_ID>.
reference:
https://developers.facebook.com/docs/marketing-api/guides/lead-ads/create#create
you have to specify your associated page_id
promoted_object={"page_id": "<PAGE_ID>"}
Below code may help u
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.adset import AdSet
from facebook_business.api import FacebookAdsApi
access_token = '<ACCESS_TOKEN>'
app_secret = '<APP_SECRET>'
app_id = '<APP_ID>'
id = '<AD_ACCOUNT_ID>'
FacebookAdsApi.init(access_token=access_token)
fields = [
]
params = {
'name': 'A CPA Ad Set',
'campaign_id': '<adCampaignLinkClicksID>',
'daily_budget': '5000',
'start_time': '2019-01-09T21:31:19-0800',
'end_time': '2019-01-16T21:31:19-0800',
'billing_event': 'IMPRESSIONS',
'optimization_goal': 'REACH',
'bid_amount': '1000',
'promoted_object': {'page_id':'<pageID>'},
'targeting': {'geo_locations':{'countries':['US']}},
'user_os': 'iOS',
'publisher_platforms': 'facebook',
'device_platforms': 'mobile',
}
print AdAccount(id).create_ad_set(
fields=fields,
params=params,
)

Telegram bot API - Inline bot getting Error 400 while trying to answer inline query

I have a problem coding a bot in Python that works with the new inline mode.
The bot gets the query, and while trying to answer, it receives error 400.
Here is a sample of data sent by the bot at this time:
{
'inline_query_id': '287878416582808857',
'results': [
{
'type': 'article',
'title': 'Convertion',
'parse_mode': 'Markdown',
'id': '287878416582808857/0',
'message_text': 'blah blah'
}
]
}
I use requests library in to make requests, and here is the line that does it in the code:
requests.post(url = "https://api.telegram.org/bot%s%s" % (telegram_bot_token, "/answerInlineQuery"), data = myData)
With myData holding the data described in the sample.
Can you help me solve this, please?
I suspect it is because you haven't JSON-serialized the results parameter.
import json
results = [{'type': 'article',
'title': 'Convertion',
'parse_mode': 'Markdown',
'id': '287878416582808857/0',
'message_text': 'blah blah'}]
my_data = {
'inline_query_id': '287878416582808857',
'results': json.dumps(results),
}
requests.post(url="https://api.telegram.org/bot%s%s" % (telegram_bot_token, "/answerInlineQuery"),
params=my_data)
Note that I use params to supply the data.
I am getting the correct response after doing some POC. I am using java com.github.pengrad.
Below the code.
GetUpdatesResponse updatesResponse = bot.execute(new GetUpdates());
List updates = updatesResponse.updates();
for(Update update:updates){
InlineQuery inlineQuery = update.inlineQuery();
System.out.println(update);
System.out.println(inlineQuery);
System.out.println("----------------");
if(inlineQuery!=null) {
InlineQueryResult r1 = new InlineQueryResultPhoto("AgADBQADrqcxG5q8tQ0EKSz5JaZjzDWgvzIABL0Neit4ar9MsXYBAAEC", "https://api.telegram.org/file/bot230014106:AAGtWr8xUCqUy8HjSgSFrY3aCs4IZs00Omg/photo/file_1.jpg", "https://api.telegram.org/file/bot230014106:AAGtWr8xUCqUy8HjSgSFrY3aCs4IZs00Omg/photo/file_1.jpg");
BaseResponse baseResponse = bot.execute(new AnswerInlineQuery(inlineQuery.id(), r1)
.cacheTime(6000)
.isPersonal(true)
.nextOffset("offset")
.switchPmParameter("pmParam")
.switchPmText("pmText"));
System.out.println(baseResponse.isOk());
System.out.println(baseResponse.toString());
System.out.println(baseResponse.description());
}
}
Below the console output:
Update{update_id=465103212, message=null, edited_message=null, inline_query=InlineQuery{id='995145139265927135', from=User{id=231700283, first_name='Manabendra', last_name='Maji', username='null'}, location=null, query='hi', offset=''}, chosen_inline_result=null, callback_query=null}
InlineQuery{id='995145139265927135', from=User{id=231700283, first_name='Manabendra', last_name='Maji', username='null'}, location=null, query='hi', offset=''}
true
BaseResponse{ok=true, error_code=0, description='null'}
null
And I am getting proper response in my mobile telegram app also.

PayPal Python Pay request ClientDetails

I'm working with PayPals API which is really badly documented and need to ask for some help.
I am extending my site on the PayPal Adaptive API which allows me to setup Preapproved payments before.
Along with the details sent I'd like to add some user information.
It seems like it can be done according to their documentation, but nowhere in the IPN does it get captured.
simple payment
def test_pay():
response = paypal.pay(
actionType = 'PAY',
cancelUrl = cancelUrl,
currencyCode = currencyCode,
senderEmail = EMAIL_ACCOUNT,
feesPayer = 'EACHRECEIVER',
memo = 'Simple payment example',
preapprovalKey = 'PA-0HA01893HK6322232',
receiverList = { 'receiver': [
{ 'amount':"10.0", 'email':API_EMAIL, 'primary':True },
{ 'amount':"5.0", 'email':SECONDARY_EMAIL, 'primary':False }
]},
clientDetailsType = { 'customerId': 1, 'customerType': 'Normal' },
returnUrl = returnUrl,
ipnNotificationUrl = notificationUrl
)
# if response['responseEnvelope']['ack'] == "Success":
print response['responseEnvelope']['ack']
# if response['paymentExecStatus'] == "COMPLETED":
print response['paymentExecStatus']
# if response.has_key('payKey'):
print response['payKey']
print response
test_pay()
The IPN response
pay_key=AP-8J7165865F7541310&transaction%5B0%5D.id_for_sender_txn=4GL2853573576212V&transaction%5B0%5D.pending_reason=NONE&charset=windows-1252&log_default_shipping_address_in_transaction=false&transaction%5B0%5D.id=6XD76450JV9737605&notify_version=UNVERSIONED&preapproval_key=PA-93P236141R834703C&transaction%5B1%5D.id=9R07347926768733A&test_ipn=1&transaction%5B0%5D.status=Completed&status=COMPLETED&action_type=PAY&memo=Simple+payment+example&transaction%5B0%5D.receiver=a.smit_1329744569_biz%40mac.com&transaction%5B1%5D.status=Completed&payment_request_date=Wed+Feb+22+05%3A30%3A49+PST+2012&transaction%5B1%5D.id_for_sender_txn=2D9633797C888500H&verify_sign=AIDiik4kxSLiNqbMmTDHplFnCnz3A3ORrDVlBVOzrtltyUx-NoxxgSc6&transaction%5B1%5D.pending_reason=NONE&transaction%5B0%5D.status_for_sender_txn=Completed&transaction%5B1%5D.status_for_sender_txn=Completed&transaction%5B0%5D.is_primary_receiver=true&transaction%5B1%5D.receiver=a.smit_1298362298_per%40mac.com&transaction%5B1%5D.amount=USD+5.00&ipn_notification_url=http%3A%2F%2F108.166.107.74%2Fyour-ipn-location%2F&transaction%5B0%5D.amount=USD+10.00&transaction_type=Adaptive+Payment+PAY&cancel_url=http%3A%2F%2F108.166.107.74%2F&reverse_all_parallel_payments_on_error=false&sender_email=a.smit_1329128659_per%40mac.com&transaction%5B1%5D.is_primary_receiver=false&fees_payer=EACHRECEIVER&return_url=http%3A%2F%2F108.166.107.74%2F
Nowhere in the response can I see the customerType or customerId
Any ideas?
customerId and customerType aren't returned according to the PayPal API. I imagine it's a field that will show up in your transaction history on the PayPal site. Why there is no API function to return customer data is beyond me, as well.

Categories

Resources