Skip to content

Commit

Permalink
[INFRA] Add structured logging linter script to github workflow (#3840)
Browse files Browse the repository at this point in the history
<!--
Thanks for sending a pull request!  Here are some tips for you:
1. If this is your first time, please read our contributor guidelines:
https://github.com/delta-io/delta/blob/master/CONTRIBUTING.md
2. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP]
Your PR title ...'.
  3. Be sure to keep the PR description updated to reflect all changes.
  4. Please write your PR title to summarize what this PR proposes.
5. If possible, provide a concise example to reproduce the issue for a
faster review.
6. If applicable, include the corresponding issue number in the PR title
and link it in the body.
-->

#### Which Delta project/connector is this regarding?
<!--
Please add the component selected below to the beginning of the pull
request title
For example: [Spark] Title of my pull request
-->

- [ ] Spark
- [ ] Standalone
- [ ] Flink
- [ ] Kernel
- [x] Other (Infra)

## Description

<!--
- Describe what this PR changes.
- Describe why we need the change.
 
If this PR resolves an issue be sure to include "Resolves #XXX" to
correctly link and close the issue upon merge.
-->
Add a python linter script to enforce new code to adhere to structured
logging. There is a similar effort in Spark:
apache/spark#47239.
## How was this patch tested?

<!--
If tests were added, say they were added here. Please make sure to test
the changes thoroughly including negative and positive cases if
possible.
If the changes were tested in any way other than unit tests, please
clarify how you tested step by step (ideally copy and paste-able, so
that other reviewers can test and check, and descendants can verify in
the future).
If the changes were not tested, please explain why.
-->
github workflow passed.
## Does this PR introduce _any_ user-facing changes?

<!--
If yes, please clarify the previous behavior and the change this PR
proposes - provide the console output, description and/or an example to
show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change
compared to the released Delta Lake versions or within the unreleased
branches such as master.
If no, write 'No'.
-->
No
  • Loading branch information
zedtang authored Nov 5, 2024
1 parent 796f518 commit c60437b
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/spark_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ jobs:
pipenv run pip install pyarrow==8.0.0
pipenv run pip install numpy==1.20.3
if: steps.git-diff.outputs.diff
- name: Scala structured logging style check
run: |
if [ -f ./dev/spark_structured_logging_style.py ]; then
python3 ./dev/spark_structured_logging_style.py
fi
if: steps.git-diff.outputs.diff
- name: Run Scala/Java and Python tests
# when changing TEST_PARALLELISM_COUNT make sure to also change it in spark_master_test.yaml
run: |
Expand Down
113 changes: 113 additions & 0 deletions dev/spark_structured_logging_style.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env python3

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
#

#
# Copyright (2021) The Delta Lake Project Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
#

import os
import sys
import re
import glob


def main():
log_pattern = r"log(?:Info|Warning|Error)\(.*?\)\n"
inner_log_pattern = r'".*?"\.format\(.*\)|s?".*?(?:\$|\"\+(?!s?")).*|[^"]+\+\s*".*?"'
compiled_inner_log_pattern = re.compile(inner_log_pattern)

# Regex patterns for file paths to exclude from the Structured Logging style check
excluded_file_patterns = [
"[Tt]est",
"sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen"
"/CodeGenerator.scala",
"streaming/src/main/scala/org/apache/spark/streaming/scheduler/JobScheduler.scala",
"sql/hive-thriftserver/src/main/scala/org/apache/spark/sql/hive/thriftserver"
"/SparkSQLCLIService.scala",
"core/src/main/scala/org/apache/spark/deploy/SparkSubmit.scala",
]

nonmigrated_files = {}

target_directories = ["../spark", "../iceberg", "../hudi"]
scala_files = []
for directory in target_directories:
scala_files.extend(glob.glob(os.path.join(directory, "**", "*.scala"), recursive=True))

for file in scala_files:
skip_file = False
for exclude_pattern in excluded_file_patterns:
if re.search(exclude_pattern, file):
skip_file = True
break

if not skip_file and not os.path.isdir(file):
with open(file, "r") as f:
content = f.read()

log_statements = re.finditer(log_pattern, content, re.DOTALL)

if log_statements:
nonmigrated_files[file] = []
for log_statement in log_statements:
log_statement_str = log_statement.group(0).strip()
# trim first ( and last )
first_paren_index = log_statement_str.find("(")
inner_log_statement = re.sub(
r"\s+", "", log_statement_str[first_paren_index + 1 : -1]
)

if compiled_inner_log_pattern.fullmatch(inner_log_statement):
start_pos = log_statement.start()
preceding_content = content[:start_pos]
line_number = preceding_content.count("\n") + 1
start_char = start_pos - preceding_content.rfind("\n") - 1
nonmigrated_files[file].append((line_number, start_char))

if all(len(issues) == 0 for issues in nonmigrated_files.values()):
print("Structured logging style check passed.", file=sys.stderr)
sys.exit(0)
else:
for file_path, issues in nonmigrated_files.items():
for line_number, start_char in issues:
print(f"[error] {file_path}:{line_number}:{start_char}", file=sys.stderr)
print(
"[error]\tPlease use the Structured Logging Framework for logging messages "
'with variables. For example: log"...${{MDC(TASK_ID, taskId)}}..."'
"\n\tRefer to the guidelines in the file `shims/LoggingShims.scala`.",
file=sys.stderr,
)

sys.exit(-1)


if __name__ == "__main__":
main()

0 comments on commit c60437b

Please sign in to comment.