68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""views.py의 span 패턴을 새로운 방식으로 변환"""
|
|
import re
|
|
|
|
with open('users/views.py', 'r') as f:
|
|
content = f.read()
|
|
|
|
# 1. set_span_attributes(span, request, ...) -> enrich_span(request, ...)
|
|
content = re.sub(
|
|
r'set_span_attributes\(span, request, request\.user\)',
|
|
'enrich_span(request, request.user)',
|
|
content
|
|
)
|
|
content = re.sub(
|
|
r'set_span_attributes\(span, request\)',
|
|
'enrich_span(request)',
|
|
content
|
|
)
|
|
|
|
# 2. span.add_event(...) -> span_event(...)
|
|
content = re.sub(
|
|
r'span\.add_event\(',
|
|
'span_event(',
|
|
content
|
|
)
|
|
|
|
# 3. span.set_attribute(...) -> span_set_attribute(...)
|
|
content = re.sub(
|
|
r'span\.set_attribute\(',
|
|
'span_set_attribute(',
|
|
content
|
|
)
|
|
|
|
# 4. with tracer.start_as_current_span("...") as span: 패턴 변환
|
|
# 여러 줄에 걸친 패턴도 처리
|
|
def replace_span_block(match):
|
|
indent = match.group(1)
|
|
span_name = match.group(2)
|
|
# operation 이름 추출 (공백, 따옴표 제거)
|
|
op_name = span_name.strip().strip('"\'')
|
|
# 짧은 이름으로 변환
|
|
op_name = op_name.replace(" POST", ".post").replace(" GET", ".get")
|
|
op_name = op_name.replace(" PUT", ".put").replace(" PATCH", ".patch")
|
|
op_name = op_name.replace(" DELETE", ".delete")
|
|
op_name = op_name.replace("View", "").lower()
|
|
return f'{indent}enrich_span(request, operation="{op_name}")'
|
|
|
|
# 단일 줄 패턴
|
|
content = re.sub(
|
|
r'^(\s*)with tracer\.start_as_current_span\(([^)]+)\) as span:\s*(?:#.*)?$',
|
|
replace_span_block,
|
|
content,
|
|
flags=re.MULTILINE
|
|
)
|
|
|
|
# 여러 줄에 걸친 패턴 (줄바꿈 포함)
|
|
content = re.sub(
|
|
r'^(\s*)with tracer\.start_as_current_span\(\s*\n\s*([^)]+)\s*\) as span:\s*(?:#.*)?$',
|
|
replace_span_block,
|
|
content,
|
|
flags=re.MULTILINE
|
|
)
|
|
|
|
with open('users/views.py', 'w') as f:
|
|
f.write(content)
|
|
|
|
print("변환 완료")
|