Skip to content

config: generate model code from json schema#4879

Open
codeboten wants to merge 28 commits intoopen-telemetry:mainfrom
codeboten:codeboten/generate-config-model-from-schema
Open

config: generate model code from json schema#4879
codeboten wants to merge 28 commits intoopen-telemetry:mainfrom
codeboten:codeboten/generate-config-model-from-schema

Conversation

@codeboten
Copy link
Contributor

Description

Proposing that the first step towards implementing OpenTelemetry Configuration is to produce the model code from the json schema. I did a quick search for tools available to do this and came across datamodel-codegen which seems to do what i expected.

Will open following pull requests (in draft) to use this model code, i just want to keep these as clearly separated as possible to make reviewing them easier.

Related to #3631

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

This PR is just code generation, no tests have been added just yet

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

Proposing that the first step towards implementing OpenTelemetry Configuration is to
produce the model code from the json schema. I did a quick search for tools available
to do this and came across datamodel-codegen which seems to do what i expected.

Will open following pull requests (in draft) to use this model code, i just want
to keep these as clearly separated as possible to make reviewing them easier.

Signed-off-by: alex boten <223565+codeboten@users.noreply.github.com>
pyproject.toml Outdated
formatters = ["ruff-format", "ruff-check"]
use-standard-collections = true
use-schema-description = true
use-title-as-name = true
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
use-title-as-name = true
use-title-as-name = true
use-union-operator = true

Does this make a difference? It should be the default but the pipe is not used for optionals

Copy link
Member

Choose a reason for hiding this comment

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

I tried this but Python 3.9 doesn't support | and with use-union-operator set we'd generate something like

AlwaysOffSampler: TypeAlias = dict[str, Any] | None

with it disabled, we generate which works in 3.9

AlwaysOffSampler: TypeAlias = Optional[dict[str, Any]]

I also updated the target-python-version to 3.10 as datamodel-codegen's validation wants 3.10+.

Copy link
Contributor

Choose a reason for hiding this comment

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

We have from __future__ import annotations for being able to use the | on older pythons.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @xrmx - I've set use-union-operator to true in aef08e0

@MikeGoldsmith
Copy link
Member

MikeGoldsmith commented Feb 4, 2026

I'm helping pick this up from @codeboten - I've opened the following PR to their branch that fixes the codegeneration tooling and also sets the use-union-operator option @xrmx suggested:

@codeboten codeboten marked this pull request as ready for review February 4, 2026 15:42
@codeboten codeboten requested a review from a team as a code owner February 4, 2026 15:42
@MikeGoldsmith
Copy link
Member

I've been reviewing the CI failures and I don't think they are related to the changes in this PR.

  1. Misc 0 - typecheck
  • lots of errors even when running against main
  • no errors from files we added, eg sdk/src/opentelemetry/sdk/_configuration/models.py
  1. opentelemetry-sdk pypy-3.9 Ubuntu
  1. contrib_0 / exporter-richconsole
  • separate contrib package

If you think any of the changes are related, let me know. Thanks.

cc @open-telemetry/python-approvers @open-telemetry/python-maintainers

… into codeboten/generate-config-model-from-schema
datamodel-codegen section was inserted between [tool.pyright] and its
include/exclude config, causing pyright to check entire repo (599 files)
instead of just included paths. Moved datamodel-codegen section after
pyright config.
@MikeGoldsmith
Copy link
Member

I found the problem with the CI type checks, I had the new tool definition under the wrong section. It's fixed now.

cc @open-telemetry/python-approvers

@MikeGoldsmith
Copy link
Member

MikeGoldsmith commented Feb 11, 2026

PS the check-links error seems to be transient. The most recent failure is referencing (#3038) which is a valid link. I don't have the ability to re-run, could someone do that for me?

@xrmx xrmx moved this to Ready for review in @xrmx's Python PR digest Feb 12, 2026
MikeGoldsmith and others added 2 commits February 13, 2026 10:11
Pyright doesn't recognize that pipe operators are valid with
from __future__ import annotations on Python 3.9.
Testing if dev dependency updates in uv.lock are causing
contrib test failures.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@MikeGoldsmith
Copy link
Member

Hmm, the contrib CI failures are related to the uv.lock updates. Looking into this now.

@MikeGoldsmith
Copy link
Member

Okay, fixed - I think maybe something for weird after merging main into this branch. Reverting and regenerating the uv.lock seems to have fixed it 🎉

@pmcollins
Copy link
Member

Might be nice to add a test that just does a from opentelemetry.sdk._configuration import models to make sure the generated code imports successfully by the python versions configured in CI.

@MikeGoldsmith
Copy link
Member

Thanks @pmcollins - I've added the test, and it shows that the use-union-operator datamodel gen option doesn't work for python 3.9. We may need to disable that option while we still support that python version.

cc @xrmx what do you think?

@MikeGoldsmith
Copy link
Member

I've updated to set use-union-operator to false now. The post-processing script still updates the TypeAlias as before. This should now be compatible with python 3.9 and I've left a comment to say we can re-enable it once we drop support for Python 3.9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready for review

Development

Successfully merging this pull request may close these issues.

4 participants

Comments