Skip to content

Fix cpython.gc metrics to use semantic convention attribute names#4

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-opentelemetry-violations
Draft

Fix cpython.gc metrics to use semantic convention attribute names#4
Copilot wants to merge 2 commits intomainfrom
copilot/fix-opentelemetry-violations

Conversation

Copy link
Copy Markdown

Copilot AI commented Jan 7, 2026

Description

The cpython.gc metrics (collections, collected_objects, uncollectable_objects) were using non-namespaced attribute name "generation" with string values, violating semantic conventions which require "cpython.gc.generation" with integer values.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes

  • Import semantic convention constant: Added CPYTHON_GC_GENERATION from opentelemetry.semconv._incubating.attributes.cpython_attributes
  • Updated attribute references in callbacks: Changed hardcoded "generation" to CPYTHON_GC_GENERATION constant in:
    • _get_runtime_gc_collections()
    • _get_runtime_gc_collected_objects()
    • _get_runtime_gc_uncollectable_objects()
  • Fixed attribute values: Changed from str(index) to index for proper integer typing (0, 1, 2)
  • Updated tests: Modified expectations to match new attribute name and integer values

Before:

self._runtime_gc_collections_labels["generation"] = str(index)  # Wrong: no namespace, string value

After:

self._runtime_gc_collections_labels[CPYTHON_GC_GENERATION] = index  # Correct: "cpython.gc.generation", int value

How Has This Been Tested?

  • All 41 existing system metrics tests pass
  • Specific gc metrics tests verify correct attribute name and integer values

Does This PR Require a Core Repo Change?

  • 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
Original prompt

Please look at the following report and open a pr to fix the violations listed there in opentelemetry-instrumentation-system-metrics package that is inside the instrumentation dir. On that instrumentation the name of the attributes are taken from the opentelemetry-semantic-conventions package, so you should grep https://github.com/open-telemetry/opentelemetry-python/tree/main/opentelemetry-semantic-conventions/ to get the variable name referenced in the opentelemetry-instrumentation-system-metrics package.

Here's the report:

Metric cpython.gc.collections counter, {collection}
- [improvement] Metric 'cpython.gc.collections' is not stable; stability = development.
Data point 125
- [violation] Required attribute 'cpython.gc.generation' is not present.
generation = 0
- [violation] Attribute 'generation' does not exist in the registry.
- [improvement] Attribute name 'generation' must include a namespace (e.g. '{namespace}.{attribute_key}')
Data point 11
- [violation] Required attribute 'cpython.gc.generation' is not present.
generation = 1
- [violation] Attribute 'generation' does not exist in the registry.
- [improvement] Attribute name 'generation' must include a namespace (e.g. '{namespace}.{attribute_key}')
Data point 1
- [violation] Required attribute 'cpython.gc.generation' is not present.
generation = 2
- [violation] Attribute 'generation' does not exist in the registry.
- [improvement] Attribute name 'generation' must include a namespace (e.g. '{namespace}.{attribute_key}')

Metric cpython.gc.collected_objects counter, {object}
- [improvement] Metric 'cpython.gc.collected_objects' is not stable; stability = development.
Data point 601
- [violation] Required attribute 'cpython.gc.generation' is not present.
generation = 0
- [violation] Attribute 'generation' does not exist in the registry.
- [improvement] Attribute name 'generation' must include a namespace (e.g. '{namespace}.{attribute_key}')
Data point 78
- [violation] Required attribute 'cpython.gc.generation' is not present.
generation = 1
- [violation] Attribute 'generation' does not exist in the registry.
- [improvement] Attribute name 'generation' must include a namespace (e.g. '{namespace}.{attribute_key}')
Data point 20
- [violation] Required attribute 'cpython.gc.generation' is not present.
generation = 2
- [violation] Attribute 'generation' does not exist in the registry.
- [improvement] Attribute name 'generation' must include a namespace (e.g. '{namespace}.{attribute_key}')

Metric cpython.gc.uncollectable_objects counter, {object}
- [improvement] Metric 'cpython.gc.uncollectable_objects' is not stable; stability = development.
Data point 0
- [violation] Required attribute 'cpython.gc.generation' is not present.
generation = 0
- [violation] Attribute 'generation' does not exist in the registry.
- [improvement] Attribute name 'generation' must include a namespace (e.g. '{namespace}.{attribute_key}')
Data point 0
- [violation] Required attribute 'cpython.gc.generation' is not present.
generation = 1
- [violation] Attribute 'generation' does not exist in the registry.
- [improvement] Attribute name 'generation' must include a namespace (e.g. '{namespace}.{attribute_key}')
Data point 0
- [violation] Required attribute 'cpython.gc.generation' is not present.
generation = 2
- [violation] Attribute 'generation' does not exist in the registry.
- [improvement] Attribute name 'generation' must include a namespace (e.g. '{namespace}.{attribute_key}')


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

- Import CPYTHON_GC_GENERATION from semantic conventions
- Replace "generation" with CPYTHON_GC_GENERATION constant
- Change generation values from strings to integers (0, 1, 2)
- Update tests to use correct attribute name "cpython.gc.generation"

Co-authored-by: xrmx <12932+xrmx@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix violations in opentelemetry-instrumentation-system-metrics Fix cpython.gc metrics to use semantic convention attribute names Jan 7, 2026
Copilot AI requested a review from xrmx January 7, 2026 11:23
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.

2 participants