fastapi instrument with opentelemetry.propagators.cloud_trace_propagator - google-cloud-platform

Just looking around to see if anyone has a working POC using the FastApi auto instruments along with GCP. Turns out that the fastApi Middleware makes a call to the CloudTraceFormatPropagator when we set
set_global_textmap(CloudTraceFormatPropagator())
however the carrier is not correct and returns to many values ( carrier in this case is a HTTPScope
ive tried the below, I need to be able to do distributed tracing which you can see within the foobar
import fastapi
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
import time
import requests
from starlette.middleware.base import BaseHTTPMiddleware
from typing import Callable, Tuple
from uuid import uuid4
from fastapi import Request
from opentelemetry import metrics, trace
from opentelemetry.exporter.cloud_monitoring import (
CloudMonitoringMetricsExporter,
)
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
from opentelemetry.propagate import set_global_textmap, inject, extract
from opentelemetry.propagators.cloud_trace_propagator import (
CloudTraceFormatPropagator,
)
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor,SimpleSpanProcessor
set_global_textmap(CloudTraceFormatPropagator())
resource = Resource.create(
{
"service.name": "fastapi_e2e_server",
"service.namespace": "examples",
"service.instance.id": "instance123",
}
)
tracer_provider = TracerProvider()
cloud_trace_exporter = CloudTraceSpanExporter()
tracer_provider.add_span_processor(
# BatchSpanProcessor buffers spans and sends them in batches in a
# background thread. The default parameters are sensible, but can be
# tweaked to optimize your performance
SimpleSpanProcessor(cloud_trace_exporter)
)
meter_provider = MeterProvider(
metric_readers=[
PeriodicExportingMetricReader(
CloudMonitoringMetricsExporter(), export_interval_millis=5000
)
],
resource=resource,
)
trace.set_tracer_provider(tracer_provider)
metrics.set_meter_provider(meter_provider)
tracer = trace.get_tracer(__name__)
meter = metrics.get_meter(__name__)
app = fastapi.FastAPI(title="whata")
def the_injection():
headers={}
inject(headers)
return headers
#app.get("/foobar")
def foobar(request: Request):
with tracer.start_as_current_span("Get Alerts Parent span") as parent:
span = trace.get_current_span()
span.add_event("endpoint Called", {
"url": request.url.path
})
with tracer.start_as_current_span("Get Alerts child span") as child:
head = the_injection()
res = requests.get("http://0.0.0.0:8082/target", headers=head)
return res.text
FastAPIInstrumentor.instrument_app(app)

Related

python populate.py wont populate my database

After writing the following code and expecting the output to be an updated database with random names, websites, etc., I get no error message and no updated database
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'First_project.settings')
import django
django.setup()
import random
from first_app.models import AccessRecord,Webpage,Topic
from faker import Faker
fakegen = Faker()
topics = ['Search','Social','Marketplace','News','Games']
def add_topic():
t = Topic.object.get_or_create(top_name=random.choice(topics))[0]
t.save()
return t
def populate(N=5):
for entry in range(N):
top = add_topic()
fake_url = fakegen.url()
fake_date = fakegen.date()
fake_name = fakegen.company()
webpg = webpage.objects.get_or_create(topic=top, url=fake_ur, name=fake_name)[0]
acc_rec = AccessRecord.object.get_or_create(name=webpg,date=fake_date)[0]
if __name__ == ' __main__':
print("populate")
populate(20)
print("populating complete!")
please what do I do?
i ran the whole code in my terminal and it populated the database

Celery - identify the tasks of another chain in a chain

I have stepA and stepB which needs to be applied to ~100 sets of data. Since these sets are CPU intensive I want them to happen sequentially.
I am using Celery to create a chain with ~100 chains with (stepA and StepB).
It celery execution works fine, I want to show the status of each of the chain in a django page as each of the stepA and stepB may take an hour to complete.
I want to show the status of each of the sub chains in a django page. The problem is the AsyncResult sees all the children of the parent chain as tasks.
The following is a sample code snippet.
test_celery.py
from __future__ import absolute_import, division, print_function
from celery import Celery, chord, chain, group
from datetime import datetime
import time
app = Celery('tasks', backend='redis', broker='redis://')
#app.task
def ident(x):
print("Guru: inside ident: {}".format(datetime.now()))
print(x)
return x
#app.task
def tsum(numbers):
print("Guru: inside tsum: {}".format(datetime.now()))
print(numbers)
time.sleep(5)
return sum(numbers)
test.py
import test_celery
from celery import chain
from celery.result import AsyncResult
from celery.utils.graph import DependencyGraph
def method():
async_chain = chain(chain(test_celery.tsum.si([1, 2]), test_celery.ident.si(2)), chain(test_celery.tsum.si([2,3]), test_celery.ident.si(3)))
chain_task_names = [task.task for task in async_chain.tasks]
# run the chain
chain_results_tasks = async_chain.apply_async()
print("async_chain=", dir(chain_results_tasks))
print("result.get={}".format(chain_results_tasks.status))
# create a list of names and tasks in the chain
chain_tasks = zip(chain_task_names, reversed(list(get_chain_nodes(chain_results_tasks))))
xx = list(get_chain_nodes(chain_results_tasks))
#
print(dir(chain_results_tasks))
for task in async_chain.tasks:
print("dir task={}".format(dir(task)))
print("task_name={} task_id={}".format(task.task, task.parent_id))
for i in xx:
res = AsyncResult(i)
# print("res={}".format(dir(res)))
parent = get_parent_node(i)
print(parent.build_graph(intermediate=True))
print("parent_task={}".format(dir(parent)))
print(xx[-1].build_graph(intermediate=True))
method()
Any help is appreciated.

Google app engine: object has no attribute ToMessage

I am trying to implement a service which checks if the logged in user is on a datastore, if yes returns True, if not returns False.
Here is the code I am using:
import endpoints
from google.appengine.ext import ndb
from protorpc import remote
from protorpc import messages
from endpoints_proto_datastore.ndb import EndpointsModel
from google.appengine.api import users
class AuthRes(messages.Message):
message = messages.StringField(1)
class UserModel(EndpointsModel):
user = ndb.UserProperty()
#endpoints.api(name='myapi', version='v1', description='My Little API')
class MyApi(remote.Service):
#UserModel.method(path='myuser', http_method='GET', name='myuser.check')
def UserCheck(self, cls):
user = users.get_current_user()
if user:
myuser = cls.query().filter(cls.user.user_id() == user.user_id()).get()
if not myuser:
return AuthRes(message="False")
else:
return AuthRes(message="True")
else:
return AuthRes(message="False")
application = endpoints.api_server([MyApi], restricted=False)
I always get 'AuthRes' object has no attribute 'ToMessage'
I believe instead of this:
#UserModel.method(path='myuser', http_method='GET', name='myuser.check')
you want this:
from protorpc import message_types # add at the top
#endpoints.method(message_types.VoidMessage, AuthRes, path='myuser', http_method='GET', name='myuser.check')

Django QuerySet attribute as Float instead of Unicode

We are making a Pc builder compare website, so you can compare pc components prices and make a cheap pc. We are using external data with Scrapy that saves it into MongoDB, we use Django as framework.
I have a 'prijs' Array (price) of an object as unicode, this object is in the QuerySet. I want that each 'prijs' in that array is a Float instead of unicode. Is there some way to convert the unicode array to a normal float array. I Googled alot but no solution for me, I hope you can help me with our problem.
Sorry for my bad English, thanks in advance!
Here is one object of MongoDB:
> db.processoren.findOne()
{
"_id" : ObjectId("547db39af2125f612cb8a1e5"),
"info" : [
"Quad-core, Inclusief koeler"
],
"sku" : [
"BX80646I54590"
],
"prijs" : [
"188.00000000000000000000"
],
"categorie" : "processoren",
"herkomst" : "paradigit",
"naam" : [
"Intel Core i5-4590 - 3.3GHz - Socket 1150"
],
"link" : "http://www.paradigit.nl/intel-core-i5-4590-3-3ghz-socket-1150/80015736/details.aspx",
"stock" : [
"Op dit moment niet beschikbaar"
]
}
models.py
from django.db import models
from mongoengine import *
from APc.settings import DBNAME
connect(DBNAME)
class Processoren(Document):
herkomst = StringField(max_length=200)
categorie = StringField(max_length=120)
naam = StringField(max_length=500)
subnaam = StringField(max_length=500)
info = StringField(max_length=500)
prijs = FloatField()
stock = StringField(max_length=500)
ean = StringField(max_length=200)
sku = StringField(max_length=200)
herkomst = StringField(max_length=200)
link = StringField(max_length=200)
views.py
from django.shortcuts import render_to_response
from django.template.loader import render_to_string
from django.shortcuts import HttpResponseRedirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.template import RequestContext
from django.template import loader
from django.http import HttpResponse
from bson.json_util import dumps
from pcbuilder.compatibility import *
from pcbuilder.filters import *
import json as simplejson
from models import Processoren, Moederborden, Koeling, Behuizingen, Grafische, Harde, Dvd, Geheugen, Voeding
from itertools import chain
import json
def processoren(request):
processorenlijst = Processoren.objects
print type(processorenlijst[0].prijs[0])
This is the output of the print
<type 'unicode'>
You can easily convert a list of strings to floats by doing something like below.
unicode_list = [u'10', u'20', u'123.1']
[float(val) for val in unicode_list]
I've never used MongoDB but I know in db backends like MySQL and PSQL you enforce types. The ORM will then return the values as the correct types. Perhaps the real issue is the structure of your data.

'module' object has no attribute 'GraphAPI'

i am new in python.i want to facebook likes count using Facebook Graph API.but problem in graph api. Following code run in ipython but doesn't run in django
from django.http import HttpResponse, HttpResponseForbidden
import requests # pip install requests
import json
import facebook
from prettytable import PrettyTable
from collections import Counter
def tweet(request):
ACCESS_TOKEN = 'CAACEdEose0cBAGqqZAQTaCoCnGn9jhUR42LAuxtZBHBZCPCsverUSIngAqYidbLMgQ8K0gCnOoGFRmYEZCMoTVL0SF R0ZBKCi2TUZC8m8RXk4wAj1UQyu927GYXicFIXXv2zWVeKbPFXaGhqofwClOF7DHdewTL48ZCqy5ZBZBVsM1JopgpmGNldcNV9ZBbWtfZC4FwE7fWlCZAolwZDZD'
print("ACCESS_TOKEN:",ACCESS_TOKEN)
base_url = 'https://graph.facebook.com/me'
fields = 'id,name'
url = '%s?fields=%s&access_token=%s' % \
(base_url, fields, ACCESS_TOKEN,)
print(url)
content = requests.get(url).json()
print(json.dumps(content, indent=1))
g = facebook.GraphAPI(ACCESS_TOKEN)
friends = g.get_connections("me", "friends")['data']
likes = { friend['name'] : g.get_connections(friend['id'], "likes")['data']
for friend in friends }
print(likes)
friends_likes = Counter([like['name']
for friend in likes
for like in likes[friend]
if like.get('name')])
return HttpResponse(json.dumps(content, indent=1))