Skip to main content

modern flask framework

Project description

Modern Flask

性能基准

gunicorn 为服务器,以 --workers 1 --threads 1 为性能基准

目的是为了在容器环境下,以 1 CPU 为基准进行调度

配置

Flask.config

使用环境变量 CONFIG_SOURCE 来判断配置来源,只支持 Python 类型

  • 本地文件
    • xxxx.py
  • Kubernetes 资源
    • kubernetes://[NAMESPACE]/configmaps/[NAME]/[KEY]
    • kubernetes://[NAMESPACE]/secrets/[NAME]/[KEY]
    • kubernetes://configmaps/[NAME]/[KEY]
    • kubernetes://secrets/[NAME]/[KEY]
  • HTTP 资源
    • http://xxxxx.xxxx/xxxx.py
    • https://xxxxx.xxxx/xxxx.py

默认为本地文件 config.py

可观测性

可观测性框架基于 OpenTelemetry,使用 Zipkin 或者 Jaeger 作为导出格式, 默认启用了对以下组件的观测

  • logging
  • flask
  • sqlalchemy
  • redis

切换导出格式

使用 环境变量 OTEL_EXPORTER 切换 zipkinjaeger

设置 Kubernetes 相关环境变量

[
  {
    name: 'OTEL_EXPORTER',
    value: 'zipkin',
  },
  {
    name: 'OTEL_EXPORTER_ZIPKIN_ENDPOINT',
    value: 'http://zipkin.observability:9411/api/v2/spans',
  },
  {
    name: 'NODE_NAME',
    valueFrom: {
      fieldRef: {
        fieldPath: 'spec.nodeName',
      },
    },
  },
  {
    name: 'POD_NAME',
    valueFrom: {
      fieldRef: {
        fieldPath: 'metadata.name',
      },
    },
  },
  {
    name: 'POD_NAMESPACE',
    valueFrom: {
      fieldRef: {
        fieldPath: 'metadata.namespace',
      },
    },
  },
  {
    name: 'SERVICE_NAME',
    value: name,
  },
  {
    name: 'CONFIG_SOURCE',
    value: 'kubernetes://secrets/' + name + '-cfg/config.py',
  },
  {
    name: 'WORKLOAD_TYPE',
    value: type,
  }
]

设置其他参数

所有 Flask.config 中,以 OTEL_ 开头的配置会自动写入环境变量,用以配置 OpenTelemetry 的其他组件

详细参考:

指标

使用标准 Prometheus 库,暴露于 /metrics 路径下

数据库

使用 SQLAlchemy

Engine 配置来自于 Flask.config, 前缀 DATABASE_ENGINE_, 参考配置如下:

DATABASE_ENGINE_URL = "sqlite+pysqlite:///:memory:"
DATABASE_ENGINE_ECHO = True
DATABASE_ENGINE_FUTURE = True

详细参考 https://github.com/sqlalchemy/sqlalchemy/blob/533f5718904b620be8d63f2474229945d6f8ba5d/lib/sqlalchemy/engine/create.py#L43

Session 配置来自于 Flask.config, 前缀 DATABASE_SESSION_, 参考配置如下:

DATABASE_SESSION_AUTOFLUSH = False

详细参考 https://github.com/sqlalchemy/sqlalchemy/blob/533f5718904b620be8d63f2474229945d6f8ba5d/lib/sqlalchemy/orm/session.py#L3866

Redis

使用 Redis 库, 配置来自于 Flask.config, 前缀 REDIS_, 参考配置如下:

REDIS_HOST = "127.0.0.1"

详细参考 https://github.com/redis/redis-py/blob/12c17bfc436ea6784bbc8b2d327d981520858eb7/redis/client.py#L853

存储

目前只支持 腾讯云 COS 和 阿里云 OSS

配置来自于 Flask.config,前缀 STORAGE_,参考配置如下

腾讯云 COS

STORAGE_VENDOR = "cos"

STORAGE_SECRET_ID = "xxxxxxxxx"
STORAGE_SECRET_KEY = "xxxxxxxxx"
STORAGE_REGION = "ap-guangzhou"
STORAGE_ENDPOINT = "xxx.xxx.com"

详细参考 https://github.com/tencentyun/cos-python-sdk-v5/blob/3b6e2f1bdf28ebd7e29a211105c8f4dc6b46eddd/qcloud_cos/cos_client.py#L36

阿里云 OSS

STORAGE_VENDOR = "oss"

STORAGE_ACCESS_KEY_ID = ""
STORAGE_ACCESS_KEY_SECRET = ""
STORAGE_ENDPOINT = ""
STORAGE_BUCKET = ""

详细参考 https://pypi.org/project/oss2/

Celery

使用 Flask.config 配置 CELERY_ 前缀参数

许可证

Guo Y.K., MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modern-flask-202204b3.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

modern_flask-202204b3-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file modern-flask-202204b3.tar.gz.

File metadata

  • Download URL: modern-flask-202204b3.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.12

File hashes

Hashes for modern-flask-202204b3.tar.gz
Algorithm Hash digest
SHA256 48c4e47e5bdd92c9b4701defb5df471ba0439f31206cd12849a4e0cef29b2e2c
MD5 babbdfc1bfe554fd513d60c392d4b443
BLAKE2b-256 414cacc0d2fae45c8150a56e2fe402e0773c81d29182bbcd5b362c0e552a23e4

See more details on using hashes here.

File details

Details for the file modern_flask-202204b3-py3-none-any.whl.

File metadata

  • Download URL: modern_flask-202204b3-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.12

File hashes

Hashes for modern_flask-202204b3-py3-none-any.whl
Algorithm Hash digest
SHA256 7f2c6d57f15f8c43ffe2274a999266b60a24f58c6b2cc24da4e3550d9baf2ccf
MD5 65ec29e4de86e81203f6259b50ac249a
BLAKE2b-256 aa05bd31486a5fef4f1124139da4a122888b5811ba3fbb56fcb9b9bba43ac667

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page