def main(unused_argv): """Main entry point for SDK Fn Harness.""" fn_log_handler, sdk_harness, sdk_pipeline_options = create_harness( os.environ) experiments = sdk_pipeline_options.view_as(DebugOptions).experiments or [] dataflow_service_options = (sdk_pipeline_options.view_as( GoogleCloudOptions).dataflow_service_options or []) if (_ENABLE_GOOGLE_CLOUD_PROFILER in experiments) or (_ENABLE_GOOGLE_CLOUD_PROFILER in dataflow_service_options): try: import googlecloudprofiler job_id = os.environ["JOB_ID"] job_name = os.environ["JOB_NAME"] if job_id and job_name: googlecloudprofiler.start(service=job_name, service_version=job_id, verbose=1) _LOGGER.info('Turning on Google Cloud Profiler.') else: raise RuntimeError( 'Unable to find the job id or job name from envvar.') except Exception as e: # pylint: disable=broad-except _LOGGER.warning( 'Unable to start google cloud profiler due to error: %s' % e) try: _LOGGER.info('Python sdk harness starting.') sdk_harness.run() _LOGGER.info('Python sdk harness exiting.') except: # pylint: disable=broad-except _LOGGER.exception('Python sdk harness failed: ') raise finally: if fn_log_handler: fn_log_handler.close()
def init_cloud_profiler(): project_id = None try: project_id = os.environ["GCP_PROJECT_ID"] except KeyError: # Environment variable not set pass for retry in range(1, 4): try: if project_id: googlecloudprofiler.start( service="email_server", service_version="1.0.0", verbose=0, project_id=project_id, ) else: googlecloudprofiler.start(service="email_server", service_version="1.0.0", verbose=0) logger.info("Successfully started Stackdriver Profiler.") return except (BaseException) as exc: logger.info("Unable to start Stackdriver Profiler Python agent. " + str(exc)) if retry < 4: logger.info( "Sleeping %d to retry initializing Stackdriver Profiler" % (retry * 10)) time.sleep(1) else: logger.warning( "Could not initialize Stackdriver Profiler after retrying, giving up" )
def run(): if HAIL_SHOULD_PROFILE: profiler_tag = f'{DEFAULT_NAMESPACE}' if profiler_tag == 'default': profiler_tag = DEFAULT_NAMESPACE + f'-{HAIL_SHA[0:12]}' googlecloudprofiler.start( service='batch-driver', service_version=profiler_tag, # https://cloud.google.com/profiler/docs/profiling-python#agent_logging verbose=3) app = web.Application(client_max_size=HTTP_CLIENT_MAX_SIZE) setup_aiohttp_session(app) setup_aiohttp_jinja2(app, 'batch.driver') setup_common_static_routes(routes) app.add_routes(routes) app.router.add_get("/metrics", server_stats) app.on_startup.append(on_startup) app.on_cleanup.append(on_cleanup) web.run_app(deploy_config.prefix_application( app, 'batch-driver', client_max_size=HTTP_CLIENT_MAX_SIZE), host='0.0.0.0', port=5000, access_log_class=AccessLogger, ssl_context=get_server_ssl_context())
def initStackdriverProfiling(): project_id = None try: project_id = os.environ["GCP_PROJECT_ID"] except KeyError: # Environment variable not set pass for retry in xrange(1, 4): try: if project_id: googlecloudprofiler.start(service='recommendation_server', service_version='1.0.0', verbose=0, project_id=project_id) else: googlecloudprofiler.start(service='recommendation_server', service_version='1.0.0', verbose=0) logger.info("Successfully started Stackdriver Profiler.") return except (BaseException) as exc: logger.info("Unable to start Stackdriver Profiler Python agent. " + str(exc)) if (retry < 4): logger.info( "Sleeping %d seconds to retry Stackdriver Profiler agent initialization" % (retry * 10)) time.sleep(1) else: logger.warning( "Could not initialize Stackdriver Profiler after retrying, giving up" ) return
def ready(self): if settings.USE_PROFILER: import googlecloudprofiler try: googlecloudprofiler.start(verbose=3) except Exception as e: print("Skipping profiler: %s" % (e))
def init_profiler(): try: googlecloudprofiler.start( service='isucon-python-docker', service_version='1.0.0', verbose=3, ) except (ValueError, NotImplementedError) as exc: print(exc) # Handle errors herea
def main(): """Run.""" parser = argparse.ArgumentParser() parser.add_argument('--endpoint', default='[::]:50051', help='Server endpoint') parser.add_argument( '--forseti_db', help=('Forseti database string, formatted as ' '"mysql+pymysql://<db_user>@<db_host>:<db_port>/<db_name>"')) parser.add_argument('--config_file_path', help='Path to Forseti configuration file.') services = sorted(SERVICE_MAP.keys()) parser.add_argument('--services', nargs='+', choices=services, help=('Forseti services i.e. at least one of: %s.' % ', '.join(services))) parser.add_argument('--log_level', default='info', choices=['debug', 'info', 'warning', 'error'], help='Sets the threshold for Forseti\'s logger.' ' Logging messages which are less severe' ' than the level you set will be ignored.') parser.add_argument('--enable_console_log', action='store_true', help='Print log to console.') parser.add_argument('--enable_profiler', action='store_true', help='Enable Cloud Profiler.') args = vars(parser.parse_args()) exit_code, error_msg = check_args(args) if exit_code: sys.stderr.write('%s\n\n' % error_msg) parser.print_usage() sys.exit(exit_code) if args['enable_profiler'] and CLOUD_PROFILER_IMPORTED: try: googlecloudprofiler.start( service='forseti-server', verbose=2, # project_id must be set if not running on GCP, such as in your # local dev environment. # project_id='my_project_id', ) except (ValueError, NotImplementedError) as exc: LOGGER.warning('Unable to enable Cloud Profiler: %s', exc) serve(args['endpoint'], args['services'], args['forseti_db'], args['config_file_path'], args['log_level'], args['enable_console_log'])
def main(): # Profiler initialization. It starts a daemon thread which continuously # collects and uploads profiles. Best done as early as possible. try: googlecloudprofiler.start( service='hello-profiler', service_version='1.0.1', # verbose is the logging level. 0-error, 1-warning, 2-info, # 3-debug. It defaults to 0 (error) if not set. verbose=3, # project_id must be set if not running on GCP. # project_id='my-project-id', ) except (ValueError, NotImplementedError) as exc: print(exc) # Handle errors here # [END profiler_python_quickstart] busyloop()
def main(): # Profiler initialization. It starts a daemon thread which continuously # collects and uploads profiles. Best done as early as possible. try: googlecloudprofiler.start( service='flask_any_response', service_version=VERSION_DEP, # verbose is the logging level. 0-error, 1-warning, 2-info, # 3-debug. It defaults to 0 (error) if not set. verbose=3, # project_id must be set if not running on GCP. # project_id='my-project-id', ) except (ValueError, NotImplementedError) as exc: print(exc) # Handle errors here # [END profiler_python_quickstart] busyloop()
def start_profiler(): # Profiler initialization. It starts a daemon thread which continuously # collects and uploads profiles. Best done as early as possible. try: # Google Cloud Profiler import googlecloudprofiler googlecloudprofiler.start( service='zinger-backend', service_version='1.0.1', # verbose is the logging level. 0-error, 1-warning, 2-info, # 3-debug. It defaults to 0 (error) if not set. verbose=3, # project_id must be set if not running on GCP. project_id=os.getenv('gcp_project'), ) print("Profiler started for " + os.getenv('gcp_project')) profiler = True except (ValueError, NotImplementedError) as exc: print(exc) # Handle errors here print("failed")
def start_if_needed(service): """Start Google Cloud Profiler if |USE_PYTHON_PROFILER| environment variable is set.""" if not environment.get_value('USE_PYTHON_PROFILER'): return True project_id = utils.get_application_id() service_with_platform = '{service}_{platform}'.format( service=service, platform=environment.platform().lower()) try: # Import the package here since it is only needed when profiler is enabled. # Also, this is supported on Linux only. import googlecloudprofiler googlecloudprofiler.start(project_id=project_id, service=service_with_platform) except Exception: logs.log_error('Failed to start the profiler for service %s.' % service_with_platform) return False return True
def run(): if HAIL_SHOULD_PROFILE and CLOUD == 'gcp': profiler_tag = f'{DEFAULT_NAMESPACE}' if profiler_tag == 'default': profiler_tag = DEFAULT_NAMESPACE + f'-{HAIL_SHA[0:12]}' googlecloudprofiler.start( service='batch-driver', service_version=profiler_tag, # https://cloud.google.com/profiler/docs/profiling-python#agent_logging verbose=3, ) app = web.Application(client_max_size=HTTP_CLIENT_MAX_SIZE, middlewares=[monitor_endpoints_middleware]) setup_aiohttp_session(app) setup_aiohttp_jinja2(app, 'batch.driver') setup_common_static_routes(routes) app.add_routes(routes) app.router.add_get("/metrics", server_stats) app.on_startup.append(on_startup) app.on_cleanup.append(on_cleanup) asyncio.get_event_loop().add_signal_handler(signal.SIGUSR1, dump_all_stacktraces) web.run_app( deploy_config.prefix_application(app, 'batch-driver', client_max_size=HTTP_CLIENT_MAX_SIZE), host='0.0.0.0', port=5000, access_log_class=AccessLogger, ssl_context=internal_server_ssl_context(), )
# -*- coding: utf-8 -*- import os import time import googlecloudprofiler from celery import Celery googlecloudprofiler.start( service='celery-gevent-bug', verbose=3, # May comment out the following line if the value would be derived automatically. project_id=os.environ.get('PROFILER_PROJECT_ID')) app = Celery(__name__, broker='redis://redis:6379/0') @app.task() def test_task(): start_time = time.time() while time.time() - start_time < 0.5: pass
import googleclouddebugger import googlecloudprofiler import graphene from flask import Flask from flask_graphql import GraphQLView from lime_comb_api.auth import jwt_validate from lime_comb_api.schema import Mutation, Query googleclouddebugger.enable() googlecloudprofiler.start( service="lime-comb-api", service_version="0.0.0", # verbose is the logging level. 0-error, 1-warning, 2-info, # 3-debug. It defaults to 0 (error) if not set. verbose=3, # project_id must be set if not running on GCP. # project_id='lime-comb', ) schema = graphene.Schema(query=Query) view_func = GraphQLView.as_view( "graphql", schema=graphene.Schema(query=Query, mutation=Mutation), graphiql=True, # get_context=lambda: {"session": db_session}, ) app = Flask(__name__) app.add_url_rule("/", view_func=jwt_validate(view_func))
logging.basicConfig(level=logging.DEBUG) application = app = Flask(__name__) ENV = os.environ FULL_METHODS = ['POST', 'GET', 'HEAD', 'PUT', 'DELETE'] VERSION_DEP = os.getenv('VERSION_DEP', 'nover') GOOGLE_CLOUD_PROJECT = os.getenv('GOOGLE_CLOUD_PROJECT', '') # gcp profiler try: logging.info( f'ERROR_vars_init: VERSION_DEP={VERSION_DEP} -- GOOGLE_CLOUD_PROJECT={GOOGLE_CLOUD_PROJECT}' ) import googlecloudprofiler if GOOGLE_CLOUD_PROJECT: googlecloudprofiler.start(service='flask_any_response', service_version=VERSION_DEP, verbose=3, project_id=GOOGLE_CLOUD_PROJECT) else: googlecloudprofiler.start(service='flask_any_response', service_version=VERSION_DEP, verbose=3) except (ValueError, NotImplementedError) as exc: logging.info(f'ERROR_flaskanyresponse_profiler: {exc}') def print_request(request, title="Response"): response = f'<h1>{title}</h1>' for i in dir(request): try: key = str(i)
#!/usr/bin/python3 from flask import Flask from multiprocessing import Pool from multiprocessing import cpu_count import googlecloudprofiler googlecloudprofiler.start(service='newrelic-python-demo', service_version='0.0.1', verbose=3) app = Flask(__name__) def f(l, i): l.acquire() print('worker ', i) x = 0 while x < 1000: print(x) x += 1 l.release() @app.route("/") def hello(): return "This is a dumb flask app" @app.route("/work") def work():
def create_app(): app = Flask(__name__, static_folder="dist", static_url_path="") if os.environ.get('FLASK_ENV') in ['production', 'staging', 'autopush']: createMiddleWare(app, StackdriverExporter()) import googlecloudprofiler # Profiler initialization. It starts a daemon thread which continuously # collects and uploads profiles. Best done as early as possible. try: # service and service_version can be automatically inferred when # running on GCP. googlecloudprofiler.start(verbose=3) except (ValueError, NotImplementedError) as exc: logging.error(exc) # Setup flask config cfg = libconfig.get_config() app.config.from_object(cfg) # Init extentions from cache import cache cache.init_app(app) # apply the blueprints to the app from routes import (browser, dev, factcheck, place, placelist, ranking, redirects, static, tools) app.register_blueprint(browser.bp) app.register_blueprint(dev.bp) app.register_blueprint(place.bp) app.register_blueprint(placelist.bp) app.register_blueprint(ranking.bp) app.register_blueprint(redirects.bp) app.register_blueprint(tools.bp) from routes.api import (browser as browser_api, chart, choropleth, place as place_api, landing_page, ranking as ranking_api, stats, translator) app.register_blueprint(browser_api.bp) app.register_blueprint(chart.bp) app.register_blueprint(choropleth.bp) app.register_blueprint(factcheck.bp) app.register_blueprint(place_api.bp) app.register_blueprint(landing_page.bp) app.register_blueprint(ranking_api.bp) app.register_blueprint(static.bp) app.register_blueprint(stats.bp) app.register_blueprint(translator.bp) # Load chart config with open('chart_config.json', encoding='utf-8') as f: chart_config = json.load(f) app.config['CHART_CONFIG'] = chart_config if not cfg.TEST and not cfg.LITE: secret_client = secretmanager.SecretManagerServiceClient() secret_name = secret_client.secret_version_path( cfg.SECRET_PROJECT, 'maps-api-key', '1') secret_response = secret_client.access_secret_version(secret_name) app.config['MAPS_API_KEY'] = secret_response.payload.data.decode( 'UTF-8') if cfg.TEST or cfg.WEBDRIVER or cfg.LITE: app.config['PLACEID2DCID'] = { "ChIJCzYy5IS16lQRQrfeQ5K5Oxw": "country/USA", "ChIJPV4oX_65j4ARVW8IJ6IJUYs": "geoId/06" } else: # Load placeid2dcid mapping from GCS storage_client = storage.Client() bucket = storage_client.get_bucket(app.config['GCS_BUCKET']) blob = bucket.get_blob('placeid2dcid.json') app.config['PLACEID2DCID'] = json.loads(blob.download_as_string()) # Initialize translations babel = Babel(app, default_domain='all') app.config['BABEL_DEFAULT_LOCALE'] = i18n.DEFAULT_LOCALE app.config['BABEL_TRANSLATION_DIRECTORIES'] = 'i18n' @app.before_request def before_request(): requested_locale = request.args.get('hl', i18n.DEFAULT_LOCALE) g.locale_choices = i18n.locale_choices(requested_locale) g.locale = g.locale_choices[0] @babel.localeselector def get_locale(): return g.locale # Propagate hl parameter to all links (if not 'en') @app.url_defaults def add_language_code(endpoint, values): if 'hl' in values or g.locale == i18n.DEFAULT_LOCALE: return values['hl'] = g.locale # Provides locale parameter in all templates @app.context_processor def inject_locale(): return dict(locale=get_locale()) return app
The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/3.0/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path, include urlpatterns = [ path('', include('publications.urls')), path('dgjhsdfiulganflidj/', admin.site.urls), ] # Profiler initialization. It starts a daemon thread which continuously # collects and uploads profiles. Best done as early as possible import googlecloudprofiler try: # service and service_version can be automatically inferred when # running on App Engine. project_id must be set if not running # on GCP. googlecloudprofiler.start(service='django_app', verbose=3) except (ValueError, NotImplementedError) as exc: print(exc) # Handle errors here
def create_app(): app = Flask(__name__, static_folder="dist", static_url_path="") if os.environ.get('FLASK_ENV') in ['production', 'staging', 'autopush']: createMiddleWare(app, StackdriverExporter()) import googlecloudprofiler # Profiler initialization. It starts a daemon thread which continuously # collects and uploads profiles. Best done as early as possible. try: # service and service_version can be automatically inferred when # running on GCP. googlecloudprofiler.start(verbose=3) except (ValueError, NotImplementedError) as exc: logging.error(exc) # Setup flask config cfg = libconfig.get_config() app.config.from_object(cfg) # Init extentions from cache import cache if app.config['PRIVATE']: cache.init_app(app, {'CACHE_TYPE': 'null'}) else: cache.init_app(app) register_routes_common(app) if cfg.SUSTAINABILITY: register_routes_sustainability(app) else: register_routes_main_app(app) # Load topic page config topic_page_configs = libutil.get_topic_page_config() app.config['TOPIC_PAGE_CONFIG'] = topic_page_configs app.config['TOPIC_PAGE_SUMMARY'] = libutil.get_topics_summary( topic_page_configs) # Load chart config chart_config = libutil.get_chart_config() app.config['CHART_CONFIG'] = chart_config ranked_statvars = set() for chart in chart_config: ranked_statvars = ranked_statvars.union(chart['statsVars']) if 'relatedChart' in chart and 'denominator' in chart['relatedChart']: ranked_statvars.add(chart['relatedChart']['denominator']) app.config['RANKED_STAT_VARS'] = ranked_statvars if not cfg.TEST and not cfg.LITE: secret_client = secretmanager.SecretManagerServiceClient() secret_name = secret_client.secret_version_path( cfg.SECRET_PROJECT, 'maps-api-key', '1') secret_response = secret_client.access_secret_version(name=secret_name) app.config['MAPS_API_KEY'] = secret_response.payload.data.decode( 'UTF-8') # Initialize translations babel = Babel(app, default_domain='all') app.config['BABEL_DEFAULT_LOCALE'] = i18n.DEFAULT_LOCALE app.config['BABEL_TRANSLATION_DIRECTORIES'] = 'i18n' if not cfg.TEST: timeout = 120 # seconds counter = 0 isOpen = False while not isOpen: try: urllib.request.urlopen(cfg.API_ROOT + "/version") break except urllib.error.URLError: time.sleep(1) counter += 1 if counter > timeout: raise RuntimeError("Mixer not ready after %s second" % timeout) @app.before_request def before_request(): requested_locale = request.args.get('hl', i18n.DEFAULT_LOCALE) g.locale_choices = i18n.locale_choices(requested_locale) g.locale = g.locale_choices[0] @babel.localeselector def get_locale(): return g.locale # Propagate hl parameter to all links (if not 'en') @app.url_defaults def add_language_code(endpoint, values): if 'hl' in values or g.locale == i18n.DEFAULT_LOCALE: return values['hl'] = g.locale # Provides locale parameter in all templates @app.context_processor def inject_locale(): return dict(locale=get_locale()) return app
def create_app(): app = Flask(__name__, static_folder="dist", static_url_path="") if os.environ.get('FLASK_ENV') in ['production', 'staging']: createMiddleWare(app, StackdriverExporter()) import googlecloudprofiler # Profiler initialization. It starts a daemon thread which continuously # collects and uploads profiles. Best done as early as possible. try: # service and service_version can be automatically inferred when # running on App Engine. googlecloudprofiler.start(verbose=3) except (ValueError, NotImplementedError) as exc: logging.error(exc) # Setup flask config if os.environ.get('FLASK_ENV') == 'test': cfg = import_string('configmodule.TestConfig')() elif os.environ.get('FLASK_ENV') == 'production': cfg = import_string('configmodule.ProductionConfig')() elif os.environ.get('FLASK_ENV') == 'webdriver': cfg = import_string('configmodule.WebdriverConfig')() elif os.environ.get('FLASK_ENV') == 'staging': cfg = import_string('configmodule.StagingConfig')() elif os.environ.get('FLASK_ENV') == 'development': cfg = import_string('configmodule.DevelopmentConfig')() elif os.environ.get('FLASK_ENV') == 'minikube': cfg = import_string('configmodule.MinikubeConfig')() cfg.GCS_BUCKET = os.environ.get('GCS_BUCKET') cfg.SECRET_PROJECT = os.environ.get('SECRET_PROJECT') elif os.environ.get('FLASK_ENV') == 'gke': cfg = import_string('configmodule.GKEConfig')() cfg.GCS_BUCKET = os.environ.get('GCS_BUCKET') cfg.SECRET_PROJECT = os.environ.get('SECRET_PROJECT') else: raise ValueError("No valid FLASK_ENV is specified: %s" % os.environ.get('FLASK_ENV')) cfg.MAPS_API_KEY = os.environ.get('MAPS_API_KEY') app.config.from_object(cfg) # Init extentions from cache import cache cache.init_app(app) # apply the blueprints to the app from routes import (browser, dev, factcheck, place, placelist, ranking, redirects, static, tools) app.register_blueprint(browser.bp) app.register_blueprint(dev.bp) app.register_blueprint(place.bp) app.register_blueprint(placelist.bp) app.register_blueprint(ranking.bp) app.register_blueprint(redirects.bp) app.register_blueprint(tools.bp) from routes.api import (browser as browser_api, chart, choropleth, place as place_api, landing_page, ranking as ranking_api, stats, translator) app.register_blueprint(browser_api.bp) app.register_blueprint(chart.bp) app.register_blueprint(choropleth.bp) app.register_blueprint(factcheck.bp) app.register_blueprint(place_api.bp) app.register_blueprint(landing_page.bp) app.register_blueprint(ranking_api.bp) app.register_blueprint(static.bp) app.register_blueprint(stats.bp) app.register_blueprint(translator.bp) # Load chart config with open('chart_config.json') as f: chart_config = json.load(f) app.config['CHART_CONFIG'] = chart_config if not cfg.TEST: secret_client = secretmanager.SecretManagerServiceClient() secret_name = secret_client.secret_version_path(cfg.SECRET_PROJECT, 'maps-api-key', '1') secret_response = secret_client.access_secret_version(secret_name) app.config['MAPS_API_KEY'] = secret_response.payload.data.decode( 'UTF-8') if cfg.TEST or cfg.WEBDRIVER: app.config['PLACEID2DCID'] = { "ChIJCzYy5IS16lQRQrfeQ5K5Oxw": "country/USA", "ChIJPV4oX_65j4ARVW8IJ6IJUYs": "geoId/06" } else: # Load placeid2dcid mapping from GCS storage_client = storage.Client() bucket = storage_client.get_bucket(app.config['GCS_BUCKET']) blob = bucket.get_blob('placeid2dcid.json') app.config['PLACEID2DCID'] = json.loads(blob.download_as_string()) return app
def create_app(): app = Flask(__name__, static_folder='dist', static_url_path='') if os.environ.get('FLASK_ENV') in ['production', 'staging', 'autopush']: createMiddleWare(app, StackdriverExporter()) import googlecloudprofiler # Profiler initialization. It starts a daemon thread which continuously # collects and uploads profiles. Best done as early as possible. try: # service and service_version can be automatically inferred when # running on GCP. googlecloudprofiler.start(verbose=3) except (ValueError, NotImplementedError) as exc: logging.error(exc) # Setup flask config cfg = libconfig.get_config() app.config.from_object(cfg) # Init extentions from cache import cache if app.config['PRIVATE']: cache.init_app(app, {'CACHE_TYPE': 'null'}) else: cache.init_app(app) register_routes_common(app) if cfg.PRIVATE: register_routes_private_dc(app) else: register_routes_base_dc(app) if cfg.ADMIN: register_routes_admin(app) # Load topic page config topic_page_configs = libutil.get_topic_page_config() app.config['TOPIC_PAGE_CONFIG'] = topic_page_configs app.config['TOPIC_PAGE_SUMMARY'] = libutil.get_topics_summary( topic_page_configs) # Load chart config chart_config = libutil.get_chart_config() app.config['CHART_CONFIG'] = chart_config ranked_statvars = set() for chart in chart_config: ranked_statvars = ranked_statvars.union(chart['statsVars']) if 'relatedChart' in chart and 'denominator' in chart['relatedChart']: ranked_statvars.add(chart['relatedChart']['denominator']) app.config['RANKED_STAT_VARS'] = ranked_statvars if not cfg.TEST and not cfg.LITE: secret_client = secretmanager.SecretManagerServiceClient() secret_name = secret_client.secret_version_path( cfg.SECRET_PROJECT, 'maps-api-key', 'latest') secret_response = secret_client.access_secret_version(name=secret_name) app.config['MAPS_API_KEY'] = secret_response.payload.data.decode( 'UTF-8') if cfg.ADMIN: secret_client = secretmanager.SecretManagerServiceClient() secret_name = secret_client.secret_version_path( cfg.SECRET_PROJECT, 'oauth-client', 'latest') secret_response = secret_client.access_secret_version(name=secret_name) oauth_string = secret_response.payload.data.decode('UTF-8') oauth_json = json.loads(oauth_string) app.config['GOOGLE_CLIENT_ID'] = oauth_json['web']['client_id'] tf = tempfile.NamedTemporaryFile() with open(tf.name, 'w') as f: f.write(oauth_string) app.config['OAUTH_FLOW'] = Flow.from_client_secrets_file( client_secrets_file=tf.name, redirect_uri=oauth_json['web']['redirect_uris'][0], scopes=[ 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email', 'openid', ]) if app.config['LOCAL']: os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' if app.config['API_PROJECT']: secret_client = secretmanager.SecretManagerServiceClient() secret_name = secret_client.secret_version_path( cfg.API_PROJECT, 'mixer-api-key', 'latest') secret_response = secret_client.access_secret_version(name=secret_name) app.config['API_KEY'] = secret_response.payload.data.decode('UTF-8') # Initialize translations babel = Babel(app, default_domain='all') app.config['BABEL_DEFAULT_LOCALE'] = i18n.DEFAULT_LOCALE app.config['BABEL_TRANSLATION_DIRECTORIES'] = 'i18n' # Initialize the AI module. app.config['AI_CONTEXT'] = ai.Context() if not cfg.TEST: timeout = 5 * 60 # seconds counter = 0 isOpen = False while not isOpen: try: urllib.request.urlopen(cfg.API_ROOT + '/version') break except urllib.error.URLError: time.sleep(10) counter += 1 if counter > timeout: raise RuntimeError('Mixer not ready after %s second' % timeout) @app.before_request def before_request(): requested_locale = request.args.get('hl', i18n.DEFAULT_LOCALE) g.locale_choices = i18n.locale_choices(requested_locale) g.locale = g.locale_choices[0] @babel.localeselector def get_locale(): return g.locale # Propagate hl parameter to all links (if not 'en') @app.url_defaults def add_language_code(endpoint, values): if 'hl' in values or g.locale == i18n.DEFAULT_LOCALE: return values['hl'] = g.locale # Provides locale parameter in all templates @app.context_processor def inject_locale(): return dict(locale=get_locale()) @app.teardown_request def log_unhandled(e): if e is not None: logging.error('Error thrown for request: %s, error: %s', request, e) return app
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """An example of using https://cloud.google.com/profiler on GAE flex.""" from flask import Flask # [START profiler_python_appengine_flex] import googlecloudprofiler # Profiler initialization. It starts a daemon thread which continuously # collects and uploads profiles. Best done as early as possible. try: # service and service_version can be automatically inferred when # running on App Engine. project_id must be set if not running # on GCP. googlecloudprofiler.start(verbose=3) except (ValueError, NotImplementedError) as exc: print(exc) # Handle errors here # [END profiler_python_appengine_flex] app = Flask(__name__) @app.route('/') def hello(): """Return a friendly HTTP greeting.""" return 'Hello World!'