Skip to content

Commit

Permalink
chore: 升级依赖版本 #36 (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrenZhang authored May 30, 2024
1 parent 4f30845 commit 1de4022
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 84 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/unittest_py3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Unittest Py3

on:
push:
branches: [ "master" ]
branches: [ "main" ]
pull_request:
branches: [ "master" ]
branches: [ "*" ]

permissions:
contents: read
Expand All @@ -16,14 +16,13 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.6
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.6"
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install "django>=3,<4"
pip install -r requirements.txt
pip install -r requirements_dev.txt
- name: Test with pytest
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2.1
v1.2.2
14 changes: 0 additions & 14 deletions bk_audit/constants/contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
to the current version of the project delivered to anyone in the future.
"""

from packaging import version

DJANGO_SETTING_NAME = "BK_AUDIT_SETTINGS"


Expand All @@ -27,15 +25,3 @@ class LoggingDefaultConfig:
HANDLER_CLS = "logging.handlers.RotatingFileHandler"
FILE_MAX_BYTES = 1024 * 1024 * 100 # 10M
FILE_BACKUP_COUNT = 5


class OTVersion:
"""OT 更新版本"""

# _log release
# https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.7.1
v1_7_1 = version.parse("1.7.1")

# Change OTLPHandler to LoggingHandler
# https://github.com/open-telemetry/opentelemetry-python/pull/2528
v1_11_0 = version.parse("1.11.0")
11 changes: 2 additions & 9 deletions bk_audit/contrib/django/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import abc
import datetime
import sys
from typing import Optional, Union
from functools import cached_property
from typing import Optional, TypedDict, Union

from bk_resource import Resource
from bk_resource.base import Empty
Expand All @@ -39,13 +39,6 @@
AuditResourceType,
)

if sys.version_info >= (3, 8):
from functools import cached_property
from typing import TypedDict
else:
cached_property = property
from typing_extensions import TypedDict


# noinspection PyCompatibility
class AuditEvent(TypedDict):
Expand Down
6 changes: 3 additions & 3 deletions bk_audit/contrib/opentelemetry/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
to the current version of the project delivered to anyone in the future.
"""

from opentelemetry.sdk._logs.export import BatchLogProcessor
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor


class LazyBatchLogProcessor(BatchLogProcessor):
class LazyBatchLogProcessor(BatchLogRecordProcessor):
def __init__(self, *args, **kwargs):
super(LazyBatchLogProcessor, self).__init__(*args, **kwargs)
# shutdown
Expand Down Expand Up @@ -52,4 +52,4 @@ def shutdown(self) -> None:
def force_flush(self, timeout_millis=None):
if self._shutdown or self._worker_thread is None:
return True
super().force_flush(timeout_millis)
super(LazyBatchLogProcessor, self).force_flush(timeout_millis)
40 changes: 14 additions & 26 deletions bk_audit/contrib/opentelemetry/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,15 @@
import os
from typing import Type

from opentelemetry._logs import set_logger_provider
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter
from opentelemetry.sdk._logs import (
LogEmitterProvider,
LogProcessor,
set_log_emitter_provider,
)
from opentelemetry.sdk._logs.export import SimpleLogProcessor
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler, LogRecordProcessor
from opentelemetry.sdk._logs.export import SimpleLogRecordProcessor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.version import __version__ as _ot_version
from packaging import version

from bk_audit.constants.contrib import OTVersion
from bk_audit.constants.utils import OT_LOGGER_NAME
from bk_audit.contrib.opentelemetry.processor import LazyBatchLogProcessor

OT_VERSION = version.parse(_ot_version)


def setup(
client,
Expand All @@ -56,34 +48,30 @@ def setup(
@param endpoint: 上报地址
"""

if OT_VERSION < OTVersion.v1_11_0:
from opentelemetry.sdk._logs import OTLPHandler as LoggingHandler
else:
from opentelemetry.sdk._logs import LoggingHandler

# init service config
service_name = client.service_name
bk_data_id = bk_data_id or int(os.getenv("BKAPP_OTEL_LOG_BK_DATA_ID", -1))
bk_data_token = bk_data_token or os.getenv("BKAPP_OTEL_LOG_BK_DATA_TOKEN", "")

# init log emitter
log_emitter_provider = LogEmitterProvider(
logger_provider = LoggerProvider(
resource=Resource.create(
{"service.name": service_name, "bk_data_id": bk_data_id, "bk.data.token": bk_data_token}
{
"service.name": service_name,
"bk_data_id": bk_data_id,
"bk.data.token": bk_data_token,
}
)
)
set_log_emitter_provider(log_emitter_provider)
set_logger_provider(logger_provider)

# init exporter
processor: Type[LogProcessor] = LazyBatchLogProcessor
processor: Type[LogRecordProcessor] = LazyBatchLogProcessor
if os.getenv("BKAPP_USE_SIMPLE_LOG_PROCESSOR"):
processor = SimpleLogProcessor
processor = SimpleLogRecordProcessor
exporter = OTLPLogExporter(endpoint=endpoint or os.getenv("BKAPP_OTEL_LOG_ENDPOINT"))
log_emitter_provider.add_log_processor(processor(exporter))
logger_provider.add_log_record_processor(processor(exporter))

# init logging
handler = LoggingHandler(
level=logging.NOTSET,
log_emitter=log_emitter_provider.get_log_emitter(service_name),
)
handler = LoggingHandler(level=logging.NOTSET, logger_provider=logger_provider)
logging.getLogger(OT_LOGGER_NAME).addHandler(handler)
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![logo.png](https://github.com/TencentBlueKing/bk-audit-python-sdk/blob/master/assests/logo.png)

[![license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/TencentBlueKing/bk-audit-python-sdk/blob/master/LICENSE.txt)
[![Release Version](https://img.shields.io/badge/release-1.2.1-brightgreen.svg)](https://github.com/TencentBlueKing/bk-audit-python-sdk/releases)
[![Release Version](https://img.shields.io/badge/release-1.2.2-brightgreen.svg)](https://github.com/TencentBlueKing/bk-audit-python-sdk/releases)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/TencentBlueKing/bk-audit-python-sdk/pulls)
[![codecov](https://codecov.io/github/TencentBlueKing/bk-audit-python-sdk/branch/master/graph/badge.svg?token=CUG20ZMOVQ)](https://codecov.io/github/TencentBlueKing/bk-audit-python-sdk)
[![Test](https://github.com/TencentBlueKing/bk-audit-python-sdk/actions/workflows/unittest_py3.yml/badge.svg)](https://github.com/TencentBlueKing/bk-audit-python-sdk/actions/workflows/unittest_py3.yml)
Expand Down
2 changes: 1 addition & 1 deletion readme_en.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![logo.png](https://github.com/TencentBlueKing/bk-audit-python-sdk/blob/master/assests/logo.png)

[![license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/TencentBlueKing/bk-audit-python-sdk/blob/master/LICENSE.txt)
[![Release Version](https://img.shields.io/badge/release-1.2.1-brightgreen.svg)](https://github.com/TencentBlueKing/bk-audit-python-sdk/releases)
[![Release Version](https://img.shields.io/badge/release-1.2.2-brightgreen.svg)](https://github.com/TencentBlueKing/bk-audit-python-sdk/releases)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/TencentBlueKing/bk-audit-python-sdk/pulls)
[![codecov](https://codecov.io/github/TencentBlueKing/bk-audit-python-sdk/branch/master/graph/badge.svg?token=CUG20ZMOVQ)](https://codecov.io/github/TencentBlueKing/bk-audit-python-sdk)
[![Test](https://github.com/TencentBlueKing/bk-audit-python-sdk/actions/workflows/unittest_py3.yml/badge.svg)](https://github.com/TencentBlueKing/bk-audit-python-sdk/actions/workflows/unittest_py3.yml)
Expand Down
4 changes: 4 additions & 0 deletions release.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 版本日志

## v1.2.2

- 更新支持的 OT SDK 版本为 >=1.20.0

## v1.2.1

- 修复 Django 初始化异常
Expand Down
11 changes: 4 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# IAM
bk_iam

# Version
packaging

# OT
protobuf==3.19.5
opentelemetry-api>=1.7.1,<1.13.0
opentelemetry-sdk>=1.7.1,<1.13.0
opentelemetry-exporter-otlp>=1.7.1,<1.13.0
opentelemetry-api>=1.20.0
opentelemetry-sdk>=1.20.0
opentelemetry-exporter-otlp>=1.20.0

# bk_resource
bk_resource==0.4.8
bk_resource==0.4.11
2 changes: 2 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ pytest-html==3.1.1
xmlrunner==1.7.7
pyparsing==2.2.0
PyYAML==6.0

django>=3,<5
17 changes: 7 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

setup(
name="bk-audit",
version="1.2.1",
version="1.2.2",
author="blueking",
url="https://bk.tencent.com",
author_email="[email protected]",
Expand All @@ -46,29 +46,26 @@
],
requires=[
"bk_iam",
"packaging",
],
extras_require={
# OT 仅支持 Py3.6 及以上版本
"opentelemetry": [
"protobuf>=3.19.5",
"opentelemetry-api>=1.7.1,<1.13.0",
"opentelemetry-sdk>=1.7.1,<1.13.0",
"opentelemetry-exporter-otlp>=1.7.1,<1.13.0",
"opentelemetry-api>=1.20.0",
"opentelemetry-sdk>=1.20.0",
"opentelemetry-exporter-otlp>=1.20.0",
],
# BKResource 仅支持 Py3.6 及以上版本
"bk_resource": [
"bk_resource>=0.4.0",
],
},
classifiers=[
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*, <4",
python_requires=">=3.10, <4",
)
7 changes: 0 additions & 7 deletions tests/test_ot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from opentelemetry.sdk._logs import LogData, LogRecord
from opentelemetry.sdk.util.instrumentation import InstrumentationScope

from bk_audit.constants.contrib import OTVersion
from bk_audit.contrib.opentelemetry.exporters import OTLogExporter
from bk_audit.contrib.opentelemetry.processor import LazyBatchLogProcessor
from bk_audit.contrib.opentelemetry.setup import setup
Expand Down Expand Up @@ -57,12 +56,6 @@ def test_setup(self):
"""测试OT初始化"""
setup(self.client)

@patch("bk_audit.contrib.opentelemetry.setup.OT_VERSION", OTVersion.v1_7_1)
def test_setup_1_7_1(self):
"""测试OT初始化(v1.7.1)"""
with self.assertRaises(ImportError):
setup(self.client)

def test_service_name(self):
"""测试基类"""
self.assertEqual(BaseServiceNameHandler(str()).get_service_name(), None)
Expand Down

0 comments on commit 1de4022

Please sign in to comment.