Skip to content

Commit 974d6d0

Browse files
author
Liudmila Molkova
authored
Make stability and deprecation independent properties (#244)
1 parent f8b934b commit 974d6d0

16 files changed

Lines changed: 120 additions & 168 deletions

File tree

semantic-conventions/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Please update the changelog as part of any significant pull request.
44

55
## Unreleased
66

7+
- BREAKING: Make stability and deprecation independent properties.
8+
([#244](https://github.com/open-telemetry/build-tools/pull/244))
9+
710
## v0.23.0
811

912
- Rephrase and relax sampling-relevant description
@@ -17,7 +20,7 @@ Please update the changelog as part of any significant pull request.
1720
([#205](https://github.com/open-telemetry/build-tools/pull/205))
1821
- Fix referencing template attributes
1922
([#206](https://github.com/open-telemetry/build-tools/pull/206))
20-
23+
2124
## v0.21.0
2225

2326
- Render template-type attributes from yaml files

semantic-conventions/src/opentelemetry/semconv/model/semantic_attribute.py

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class RequirementLevel(Enum):
4141
class StabilityLevel(Enum):
4242
STABLE = 1
4343
EXPERIMENTAL = 2
44-
DEPRECATED = 3
4544

4645

4746
@dataclass
@@ -82,9 +81,7 @@ def is_enum(self):
8281
return isinstance(self.attr_type, EnumAttributeType)
8382

8483
@staticmethod
85-
def parse(
86-
prefix, semconv_stability, yaml_attributes
87-
) -> "Dict[str, SemanticAttribute]":
84+
def parse(prefix, yaml_attributes) -> "Dict[str, SemanticAttribute]":
8885
"""This method parses the yaml representation for semantic attributes
8986
creating the respective SemanticAttribute objects.
9087
"""
@@ -179,21 +176,13 @@ def parse(
179176
raise ValidationError.from_yaml_pos(position, msg)
180177

181178
tag = attribute.get("tag", "").strip()
182-
stability, deprecated = SemanticAttribute.parse_stability_deprecated(
183-
attribute.get("stability"), attribute.get("deprecated"), position_data
179+
stability = SemanticAttribute.parse_stability(
180+
attribute.get("stability"), position_data
184181
)
185-
if (
186-
semconv_stability == StabilityLevel.DEPRECATED
187-
and stability is not StabilityLevel.DEPRECATED
188-
):
189-
position = (
190-
position_data["stability"]
191-
if "stability" in position_data
192-
else position_data["deprecated"]
193-
)
194-
msg = f"Semantic convention stability set to deprecated but attribute '{attr_id}' is {stability}"
195-
raise ValidationError.from_yaml_pos(position, msg)
196-
stability = stability or semconv_stability or StabilityLevel.EXPERIMENTAL
182+
deprecated = SemanticAttribute.parse_deprecated(
183+
attribute.get("deprecated"), position_data
184+
)
185+
197186
sampling_relevant = (
198187
AttributeType.to_bool("sampling_relevant", attribute)
199188
if attribute.get("sampling_relevant")
@@ -291,44 +280,31 @@ def parse_attribute(attribute):
291280
return attr_type, str(brief), examples
292281

293282
@staticmethod
294-
def parse_stability_deprecated(stability, deprecated, position_data):
295-
if deprecated is not None and stability is None:
296-
stability = "deprecated"
297-
if deprecated is not None:
298-
if stability is not None and stability != "deprecated":
299-
position = position_data["deprecated"]
300-
msg = f"There is a deprecation message but the stability is set to '{stability}'"
301-
raise ValidationError.from_yaml_pos(position, msg)
302-
if AttributeType.get_type(deprecated) != "string" or deprecated == "":
303-
position = position_data["deprecated"]
304-
msg = (
305-
"Deprecated field expects a string that specifies why the attribute is deprecated and/or what"
306-
" to use instead! "
307-
)
308-
raise ValidationError.from_yaml_pos(position, msg)
309-
deprecated = deprecated.strip()
310-
if stability is not None:
311-
stability = SemanticAttribute.check_stability(
312-
stability,
313-
position_data["stability"]
314-
if "stability" in position_data
315-
else position_data["deprecated"],
316-
)
317-
return stability, deprecated
318-
319-
@staticmethod
320-
def check_stability(stability_value, position):
283+
def parse_stability(stability, position_data):
284+
if stability is None:
285+
return StabilityLevel.EXPERIMENTAL
321286

322287
stability_value_map = {
323-
"deprecated": StabilityLevel.DEPRECATED,
324288
"experimental": StabilityLevel.EXPERIMENTAL,
325289
"stable": StabilityLevel.STABLE,
326290
}
327-
val = stability_value_map.get(stability_value)
291+
val = stability_value_map.get(stability)
328292
if val is not None:
329293
return val
330-
msg = f"Value '{stability_value}' is not allowed as a stability marker"
331-
raise ValidationError.from_yaml_pos(position, msg)
294+
msg = f"Value '{stability}' is not allowed as a stability marker"
295+
raise ValidationError.from_yaml_pos(position_data["stability"], msg)
296+
297+
@staticmethod
298+
def parse_deprecated(deprecated, position_data):
299+
if deprecated is not None:
300+
if AttributeType.get_type(deprecated) != "string" or deprecated == "":
301+
msg = (
302+
"Deprecated field expects a string that specifies why the attribute is deprecated and/or what"
303+
" to use instead! "
304+
)
305+
raise ValidationError.from_yaml_pos(position_data["deprecated"], msg)
306+
return deprecated.strip()
307+
return None
332308

333309
def equivalent_to(self, other: "SemanticAttribute"):
334310
if self.attr_id is not None:

semantic-conventions/src/opentelemetry/semconv/model/semantic_convention.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,18 @@ def __init__(self, group):
140140
self.semconv_id = self.id
141141
self.note = group.get("note", "").strip()
142142
self.prefix = group.get("prefix", "").strip()
143-
stability = group.get("stability")
144-
deprecated = group.get("deprecated")
145143
position_data = group.lc.data
146-
self.stability, self.deprecated = SemanticAttribute.parse_stability_deprecated(
147-
stability, deprecated, position_data
144+
self.stability = SemanticAttribute.parse_stability(
145+
group.get("stability"), position_data
146+
)
147+
self.deprecated = SemanticAttribute.parse_deprecated(
148+
group.get("deprecated"), position_data
148149
)
149150
self.extends = group.get("extends", "").strip()
150151
self.events = group.get("events", ())
151152
self.constraints = parse_constraints(group.get("constraints", ()))
152153
self.attrs_by_name = SemanticAttribute.parse(
153-
self.prefix, self.stability, group.get("attributes")
154+
self.prefix, group.get("attributes")
154155
)
155156

156157
def contains_attribute(self, attr: "SemanticAttribute"):

semantic-conventions/src/opentelemetry/semconv/templating/code.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222

2323
from opentelemetry.semconv.model.semantic_attribute import (
2424
RequirementLevel,
25+
SemanticAttribute,
2526
TextWithLinks,
2627
)
27-
from opentelemetry.semconv.model.semantic_convention import SemanticConventionSet
28+
from opentelemetry.semconv.model.semantic_convention import (
29+
BaseSemanticConvention,
30+
SemanticConventionSet,
31+
)
2832
from opentelemetry.semconv.model.utils import ID_RE
2933

3034

@@ -156,6 +160,10 @@ def to_camelcase(name: str, first_upper=False) -> str:
156160
return first + "".join(word.capitalize() for word in rest)
157161

158162

163+
def is_deprecated(obj: typing.Union[SemanticAttribute, BaseSemanticConvention]) -> bool:
164+
return obj.deprecated is not None
165+
166+
159167
class CodeRenderer:
160168
pattern = f"{{{ID_RE.pattern}}}"
161169

@@ -209,6 +217,8 @@ def setup_environment(env: Environment, trim_whitespace: bool):
209217
env.filters["to_html_links"] = to_html_links
210218
env.filters["regex_replace"] = regex_replace
211219
env.filters["render_markdown"] = render_markdown
220+
env.filters["is_deprecated"] = is_deprecated
221+
env.tests["is_deprecated"] = is_deprecated
212222
env.trim_blocks = trim_whitespace
213223
env.lstrip_blocks = trim_whitespace
214224

semantic-conventions/src/opentelemetry/semconv/templating/markdown/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,19 @@ def to_markdown_attr(
119119
if "deprecated" in attribute.deprecated.lower():
120120
description = f"**{attribute.deprecated}**<br>"
121121
else:
122-
deprecated_msg = self.options.md_snippet_by_stability_level[
123-
StabilityLevel.DEPRECATED
124-
].format(attribute.deprecated)
122+
deprecated_msg = self.options.deprecated_md_snippet().format(
123+
attribute.deprecated
124+
)
125125
description = f"{deprecated_msg}<br>"
126126
elif (
127127
attribute.stability == StabilityLevel.STABLE and self.options.enable_stable
128128
):
129-
description = f"{self.options.md_snippet_by_stability_level[StabilityLevel.STABLE]}<br>"
129+
description = f"{self.options.stable_md_snippet()}<br>"
130130
elif (
131131
attribute.stability == StabilityLevel.EXPERIMENTAL
132132
and self.options.enable_experimental
133133
):
134-
description = f"{self.options.md_snippet_by_stability_level[StabilityLevel.EXPERIMENTAL]}<br>"
134+
description = f"{self.options.experimental_md_snippet()}<br>"
135135
description += attribute.brief
136136
if attribute.note:
137137
self.render_ctx.add_note(attribute.note)

semantic-conventions/src/opentelemetry/semconv/templating/markdown/options.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,9 @@
1515
from dataclasses import dataclass, field
1616
from typing import List
1717

18-
from opentelemetry.semconv.model.semantic_attribute import StabilityLevel
19-
2018

2119
@dataclass()
2220
class MarkdownOptions:
23-
24-
_badge_map = {
25-
StabilityLevel.DEPRECATED: "![Deprecated](https://img.shields.io/badge/-deprecated-red)",
26-
StabilityLevel.EXPERIMENTAL: "![Experimental](https://img.shields.io/badge/-experimental-blue)",
27-
StabilityLevel.STABLE: "![Stable](https://img.shields.io/badge/-stable-lightgreen)",
28-
}
29-
30-
_label_map = {
31-
StabilityLevel.DEPRECATED: "**Deprecated: {}**",
32-
StabilityLevel.EXPERIMENTAL: "**Experimental**",
33-
StabilityLevel.STABLE: "**Stable**",
34-
}
35-
3621
check_only: bool = False
3722
enable_stable: bool = False
3823
enable_experimental: bool = False
@@ -41,8 +26,17 @@ class MarkdownOptions:
4126
break_count: int = 50
4227
exclude_files: List[str] = field(default_factory=list)
4328

44-
@property
45-
def md_snippet_by_stability_level(self):
29+
def stable_md_snippet(self):
30+
if self.use_badge:
31+
return "![Stable](https://img.shields.io/badge/-stable-lightgreen)"
32+
return "**Stable**"
33+
34+
def experimental_md_snippet(self):
35+
if self.use_badge:
36+
return "![Experimental](https://img.shields.io/badge/-experimental-blue)"
37+
return "**Experimental**"
38+
39+
def deprecated_md_snippet(self):
4640
if self.use_badge:
47-
return self._badge_map
48-
return self._label_map
41+
return "![Deprecated](https://img.shields.io/badge/-deprecated-red)"
42+
return "**Deprecated: {}**"

semantic-conventions/src/tests/data/jinja/attribute_templates/template

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ class AttributesTemplate {
4949
* <p>Notes:
5050
<ul> {{attribute_template.note | render_markdown(code="{{@code {0}}}", paragraph="<li>{0}</li>", list="{0}")}} </ul>
5151
{%- endif %}
52-
{%- if (attribute_template.stability | string()) == "StabilityLevel.DEPRECATED" %}
52+
{%- if attribute_template | is_deprecated %}
5353
*
5454
* @deprecated {{attribute_template.brief | to_doc_brief}}.
5555
{%- endif %}
5656
*/
57-
{%- if (attribute_template.stability | string()) == "StabilityLevel.DEPRECATED" %}
57+
{%- if attribute_template | is_deprecated %}
5858
@Deprecated
5959
{%- endif %}
6060
public static final AttributeKey<{{upFirst(to_java_return_type(attribute_template.instantiated_type | string))}}> {{attribute_template.fqn | to_const_name}} = {{to_java_key_type(attribute_template.instantiated_type | string)}}("{{attribute_template.fqn}}");
@@ -68,12 +68,12 @@ class AttributesTemplate {
6868
* <p>Notes:
6969
<ul> {{attribute.note | render_markdown(code="{{@code {0}}}", paragraph="<li>{0}</li>", list="{0}")}} </ul>
7070
{%- endif %}
71-
{%- if (attribute.stability | string()) == "StabilityLevel.DEPRECATED" %}
71+
{%- if attribute | is_deprecated %}
7272
*
7373
* @deprecated {{attribute.brief | to_doc_brief}}.
7474
{%- endif %}
7575
*/
76-
{%- if (attribute.stability | string()) == "StabilityLevel.DEPRECATED" %}
76+
{%- if attribute | is_deprecated %}
7777
@Deprecated
7878
{%- endif %}
7979
public static final AttributeKey<{{upFirst(to_java_return_type(attribute.instantiated_type | string))}}> {{attribute.fqn | to_const_name}} = {{to_java_key_type(attribute.instantiated_type | string)}}("{{attribute.fqn}}");

semantic-conventions/src/tests/data/markdown/stability/badges_expected.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
| Attribute | Type | Description | Examples | Requirement Level |
55
|---|---|---|---|---|
66
| [`test.def_stability`](labels_expected.md) | boolean | | | Required |
7-
| [`test.deprecated_attr`](labels_expected.md) | boolean | | | Required |
7+
| [`test.deprecated_attr`](labels_expected.md) | boolean | ![Deprecated](https://img.shields.io/badge/-deprecated-red)<br> | | Required |
88
| [`test.exp_attr`](labels_expected.md) | boolean | | | Required |
99
| [`test.stable_attr`](labels_expected.md) | boolean | ![Stable](https://img.shields.io/badge/-stable-lightgreen)<br> | | Required |
1010
<!-- endsemconv -->

semantic-conventions/src/tests/data/markdown/stability/labels_expected.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
| Attribute | Type | Description | Examples | Requirement Level |
55
|---|---|---|---|---|
66
| [`test.def_stability`](labels_expected.md) | boolean | | | Required |
7-
| [`test.deprecated_attr`](labels_expected.md) | boolean | | | Required |
7+
| [`test.deprecated_attr`](labels_expected.md) | boolean | **Deprecated: Removed.**<br> | | Required |
88
| [`test.exp_attr`](labels_expected.md) | boolean | | | Required |
99
| [`test.stable_attr`](labels_expected.md) | boolean | | | Required |
1010
<!-- endsemconv -->

semantic-conventions/src/tests/data/markdown/stability/stability.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ groups:
1717
- id: deprecated_attr
1818
type: boolean
1919
requirement_level: required
20-
stability: deprecated
20+
stability: stable
21+
deprecated: "Removed."
2122
brief: ""
2223
- id: def_stability
2324
type: boolean

0 commit comments

Comments
 (0)