Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cbang/simplify entry point2 #93

Draft
wants to merge 85 commits into
base: main
Choose a base branch
from
Draft

Conversation

mrbangbricks
Copy link

@mrbangbricks mrbangbricks commented Feb 4, 2025

Description

This PR introduces a new start_trace() decorator which simplifies the setup of logging setup inside entry point files of business applications: e.g. in measurements or wholesale. Further, a LoggingSettings() pydantic settings class is used to handle the automatic parsing of cli parameters and environment variables.

An implementation of this functionality is avaialble in this PR in the measurements repo: Energinet-DataHub/opengeh-measurements#180

Files changed:

  • .docker/requirements.txt:
    • temporary change to point to own branch
  • .github/workflows/create-release-tag.yml
    • Introducing a new version 4.2.0, as we are introducing a breaking change in logging_configuration method ’s parameters
  • source/telemetry/release-notes/release-notes.md
    • Description of upgrade of version
  • source/telemetry/telemetry_logging/decorators.py
    • Adition of start_trace decorator, which does the following:
      • Checks if an initial span name is provided from initial_span_name parameter. Uses this name if provided, and if not, the name of the function implmenting the decorator is used
      • Checks if logging has been configured in the Telemetery logging module. Raises NotImplementedError if not
      • Starts a tracer using the provided tracer name and logs an initial trace to application insights
      • Calls the function that is implementing the decorator in a try / except block, recording exceptions in Application Insights if they occour
  • source/telemetry/telemetry_logging/logging_configuration.py
    • Implements a LoggingSettings class based on Pydantic BaseSettings which parses cli parameters and env variables
    • Changes to configure_logging:
      • Now just accepts a LoggingSettings object for configuration of logging
      • Automaticcaly adds orchestration_instance_id as an extra, as it is required for the Telemetry logs to show up in Application Insights
  • source/telemetry/tests/conftest.py
    • Implements relative path to point to telemetery_folder_path, using Path Library
  • Test files updated to accomodate the new LoggingSettings class and configure_logging() method:
    • source/telemetry/tests/integration_test/test_integration_azure_monitor.py
    • source/telemetry/tests/telemetry_logging/test_decorators.py
    • source/telemetry/tests/telemetry_logging/test_logging_configuration.py
    • source/telemetry/tests/telemetry_logging/test_span_recording.py

chba added 30 commits January 10, 2025 12:54
… explicitly inherited from AppInterface using method resolution order
Copy link

github-actions bot commented Feb 11, 2025

Coverage

Coverage report for 'geh_common'
FileStmtsMissCoverMissing
src/geh_common
   __init__.py00100% 
src/geh_common/domain
   __init__.py00100% 
src/geh_common/domain/types
   __init__.py14140%1–13, 15
   charge_quality.py660%1, 4–8
   charge_resolution.py880%1, 4–10
   charge_type.py550%1, 4–7
   charge_unit.py660%1, 4–8
   metering_point_resolution.py440%1, 4–6
   metering_point_sub_type.py550%1, 4–7
   metering_point_type.py18180%1, 4–20
   net_settlement_group.py550%1, 4–7
   orchestration_type.py440%1, 4–6
   product.py330%1, 4–5
   quantity_quality.py660%1, 4–8
   quantity_unit.py330%1, 4–5
   settlement_method.py440%1, 4–6
src/geh_common/migrations
   __init__.py40100% 
   container.py110100% 
   migration_pipeline.py60100% 
src/geh_common/migrations/constants
   __init__.py00100% 
   migrations_constants.py30100% 
src/geh_common/migrations/infrastructure
   __init__.py00100% 
   apply_migration_scripts.py22386%24–26
   sql_file_executor.py37391%29–31
   uncommitted_migration_scripts.py34197%60
src/geh_common/migrations/models
   __init__.py00100% 
   configuration.py9633%8–13
   spark_sql_migrations_configuration.py150100% 
   table_version.py4250%3–4
src/geh_common/migrations/schemas
   __init__.py00100% 
   migrations_schema.py30100% 
src/geh_common/migrations/utility
   __init__.py00100% 
   delta_table_helper.py341070%36–37, 41–42, 45, 48, 51–52, 58, 66
src/geh_common/parsing
   __init__.py20100% 
   pydantic_settings_parsing.py30100% 
src/geh_common/pyspark
   __init__.py00100% 
   data_frame_wrapper.py220100% 
   read_csv.py330100% 
   read_csv_test.py130100% 
   transformations.py311164%32, 47, 49–50, 52, 54–55, 57, 61, 65, 70
src/geh_common/telemetry
   __init__.py40100% 
   decorators.py41392%67–69
   logger.py220100% 
   logging_configuration.py530100% 
   span_recording.py50100% 
src/geh_common/testing
   __init__.py00100% 
src/geh_common/testing/container_test
   __init__.py220%1, 3
   databricks_api_client.py36360%1, 3–4, 7–9, 11, 20–25, 27, 37–38, 40, 51, 53–56, 58–60, 62–64, 66–71, 73, 75
src/geh_common/testing/covernator
   TestCases.py47393%22, 56–57
   __init__.py20100% 
src/geh_common/testing/dataframes
   __init__.py50100% 
   assert_dataframes.py1084954%37–39, 42–43, 72–75, 79–81, 83–85, 89–91, 93–95, 99–101, 103–108, 112–114, 116, 119, 127–128, 131–132, 144–145, 149–152, 154–156, 158
   assert_schemas.py63493%126, 159–160, 167
   read_csv.py160100% 
   write_to_delta.py14285%34–35
src/geh_common/testing/delta_lake
   __init__.py20100% 
   delta_lake_operations.py17194%29
src/geh_common/testing/scenario_testing
   TestCases.py31293%20, 60
   __init__.py30100% 
   get_then_names.py13284%25, 28
TOTAL86123173% 

docs/telemetry/release-notes.md Outdated Show resolved Hide resolved
return _LOGGING_CONFIGURED


class LoggingSettings(PydanticParsingSettings):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure this shouldn't just inherit from pydantic.BaseModel?

Copy link
Author

@mrbangbricks mrbangbricks Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as we discussed with Bjarke that we want a generic Pydantic class where we control the most generic settings configurations, that meet our use cases with parameters and environment variables, such as:

cli_parse_args=True,
cli_kebab_case=True,
cli_ignore_unknown_args=True, (parsing will fail otherwise)
cli_implicit_flags=True (We have this some places)

extras: dict[str, Any] = {},
force_configuration: bool = False,
logging_settings: LoggingSettings,
extras: dict[str, Any] | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just default this to an empty dict so you don't have to check for None later?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are thinking about the extras dict right? I agree that it should just default to None

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/geh_common/telemetry/logging_configuration.py Outdated Show resolved Hide resolved
src/geh_common/telemetry/logging_configuration.py Outdated Show resolved Hide resolved
@@ -214,3 +240,105 @@ def test__add_log_records_to_azure_monitor_keeps_correct_count(
query=query,
expected_count=log_count,
)


def test__decorators_integration_test(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you parameterize this so that these can be executed in parallel?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a general delay before the logs show up in application insights, so when the first query succeeds, the remaining three queries find the results instantly - so I don't think it will improve the speed of things to parameterize this :-)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do it for prettification though if you agree?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants