Skip to content

Commit

Permalink
fixed review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Wert <[email protected]>
  • Loading branch information
AlexanderWert committed Jul 31, 2023
1 parent f96d489 commit 007d33e
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
validate_values,
)

TEMPLATE_TYPE_RE = re.compile("template\\[([a-z\\[\\]]+)\\]")
TEMPLATE_PREFIX = "template["
TEMPLATE_SUFFIX = "]"


class RequirementLevel(Enum):
Expand Down Expand Up @@ -77,12 +78,8 @@ def inherit_attribute(self):
return replace(self, inherited=True)

@property
def core_type(self):
return (
AttributeType.get_core_template_type(self.attr_type)
if AttributeType.is_template_type(self.attr_type)
else self.attr_type
)
def instantiated_type(self):
return AttributeType.get_instantiated_type(self.attr_type)

@property
def is_local(self):
Expand Down Expand Up @@ -382,17 +379,25 @@ def is_simple_type(attr_type: str):
def is_template_type(attr_type: str):
if not isinstance(attr_type, str):
return False
matchObj = TEMPLATE_TYPE_RE.fullmatch(attr_type)
if matchObj is not None:
return AttributeType.is_simple_type(matchObj.group(1))
return False

return (
attr_type.startswith(TEMPLATE_PREFIX)
and attr_type.endswith(TEMPLATE_SUFFIX)
and AttributeType.is_simple_type(
attr_type[len(TEMPLATE_PREFIX) : len(attr_type) - len(TEMPLATE_SUFFIX)]
)
)

@staticmethod
def get_core_template_type(attr_type: str):
matchObj = TEMPLATE_TYPE_RE.fullmatch(attr_type)
if matchObj is not None:
return matchObj.group(1)
raise ValidationError(0, 0, f"Cannot retrieve core type from type: {attr_type}")
def get_instantiated_type(attr_type: str):
if AttributeType.is_template_type(attr_type):
return attr_type[
len(TEMPLATE_PREFIX) : len(attr_type) - len(TEMPLATE_SUFFIX)
]
elif AttributeType.is_simple_type(attr_type):
return attr_type
else:
return "enum"

@staticmethod
def type_mapper(attr_type: str):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import typing
from dataclasses import dataclass, field
from enum import Enum
from typing import Dict, Tuple, Union
from typing import Dict, Optional, Tuple, Union

from ruamel.yaml import YAML

Expand Down Expand Up @@ -112,34 +112,26 @@ class BaseSemanticConvention(ValidatableYamlNode):

@property
def attributes(self):
if not hasattr(self, "attrs_by_name"):
return []

return list(
filter(
lambda attr: not AttributeType.is_template_type(attr.attr_type),
list(self.attrs_by_name.values()),
)
)
return self._get_attributes(False)

@property
def attribute_templates(self):
if not hasattr(self, "attrs_by_name"):
return []

return list(
filter(
lambda attr: AttributeType.is_template_type(attr.attr_type),
list(self.attrs_by_name.values()),
)
)
return self._get_attributes(True)

@property
def attributes_and_templates(self):
return self._get_attributes(None)

def _get_attributes(self, templates: Optional[bool]):
if not hasattr(self, "attrs_by_name"):
return []

return list(self.attrs_by_name.values())
return [
attr
for attr in self.attrs_by_name.values()
if templates is None
or templates == AttributeType.is_template_type(attr.attr_type)
]

def __init__(self, group):
super().__init__(group)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ def to_markdown_attr(
attr_type = (
"enum"
if isinstance(attribute.attr_type, EnumAttributeType)
else attribute.attr_type
else AttributeType.get_instantiated_type(attribute.attr_type)
)
if AttributeType.is_template_type(attr_type):
attr_type = AttributeType.get_core_template_type(attr_type)
description = ""
if attribute.deprecated and self.options.enable_deprecated:
if "deprecated" in attribute.deprecated.lower():
Expand Down Expand Up @@ -331,33 +329,33 @@ def to_markdown_enum(self, output: io.StringIO):
output.write("\n")

def render_fqn_for_attribute(self, attribute):
diff = self.get_attr_reference_diff(attribute.fqn)
rel_path = self.get_attr_reference_relative_path(attribute.fqn)
name = attribute.fqn
if AttributeType.is_template_type(attribute.attr_type):
name = f"{attribute.fqn}.<key>"

if diff is not None:
return f"[`{name}`]({diff})"
if rel_path is not None:
return f"[`{name}`]({rel_path})"
return f"`{name}`"

def render_attribute_id(self, attribute_id):
"""
Method to render in markdown an attribute id. If the id points to an attribute in another rendered table, a
markdown link is introduced.
"""
diff = self.get_attr_reference_diff(attribute_id)
if diff is not None:
return f"[`{attribute_id}`]({diff})"
rel_path = self.get_attr_reference_relative_path(attribute_id)
if rel_path is not None:
return f"[`{attribute_id}`]({rel_path})"
return f"`{attribute_id}`"

def get_attr_reference_diff(self, attribute_id):
def get_attr_reference_relative_path(self, attribute_id):
md_file = self.filename_for_attr_fqn.get(attribute_id)
if md_file:
path = PurePath(self.render_ctx.current_md)
if path.as_posix() != PurePath(md_file).as_posix():
diff = PurePath(os.path.relpath(md_file, start=path.parent)).as_posix()
if diff != ".":
return diff
rel_path = PurePath(os.path.relpath(md_file, start=path.parent)).as_posix()
if rel_path != ".":
return rel_path
return None

def to_markdown_constraint(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ class AttributesTemplate {
* this is the description of the second attribute template. It's a number.
*/
public static final AttributeKey<Long> ATTRIBUTE_TEMPLATE_TWO = longKey("attribute_template_two");

/**
* this is the description of the third attribute template. It's a boolean.
*/
public static final AttributeKey<Boolean> ATTRIBUTE_THREE = booleanKey("attribute_three");
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ class AttributesTemplate {
{%- if (attribute_template.stability | string()) == "StabilityLevel.DEPRECATED" %}
@Deprecated
{%- endif %}
public static final AttributeKey<{{upFirst(to_java_return_type(attribute_template.core_type | string))}}> {{attribute_template.fqn | to_const_name}} = {{to_java_key_type(attribute_template.core_type | string)}}("{{attribute_template.fqn}}");
{%- endfor %}
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}}");
{%- endfor %}
{%- for attribute in attributes if attribute.is_local and not attribute.ref %}

/**
* {{attribute.brief | render_markdown(code="{{@code {0}}}", paragraph="{0}")}}
{%- if attribute.note %}
*
* <p>Notes:
<ul> {{attribute.note | render_markdown(code="{{@code {0}}}", paragraph="<li>{0}</li>", list="{0}")}} </ul>
{%- endif %}
{%- if (attribute.stability | string()) == "StabilityLevel.DEPRECATED" %}
*
* @deprecated {{attribute.brief | to_doc_brief}}.
{%- endif %}
*/
{%- if (attribute.stability | string()) == "StabilityLevel.DEPRECATED" %}
@Deprecated
{%- endif %}
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}}");
{%- endfor %}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ groups:
type: boolean
brief: >
this is the description of
the second attribute template. It's a boolean.
the third attribute template. It's a boolean.
12 changes: 6 additions & 6 deletions semantic-conventions/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ simple_type ::= "string"
| "double[]"
| "boolean[]"
template_type ::= "template[" simple_type "]"
template_type ::= "template[" simple_type "]" # As a single string
enum ::= [allow_custom_values] members
Expand Down Expand Up @@ -229,7 +229,7 @@ An attribute is defined by:
* `"int[]"`: Array of integer attributes.
* `"double[]"`: Array of double attributes.
* `"boolean[]"`: Array of booleans attributes.
* _template type as string literal:_ `"template[<PRIMITIVE_OR_ARRAY_TYPE>]"` (See later)
* _template type as string literal:_ `"template[<PRIMITIVE_OR_ARRAY_TYPE>]"` (See [below](#template-type))

See the [specification of Attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/common/README.md#attribute) for the definition of the value types.
- `ref`, optional string, reference an existing attribute, see [below](#ref).
Expand Down Expand Up @@ -343,11 +343,11 @@ array of booleans, a template type or an enumeration.

##### Template type

A template type attribute represents a _key-value set_ of attributes with a common attribute name prefix. The syntax for defining template type attributes is the following:
A template type attribute represents a _dictionary_ of attributes with a common attribute name prefix. The syntax for defining template type attributes is the following:

`type: template[<PRIMITIVE_OR_ARRAY_TYPE>]`

The `<PRIMITIVE_OR_ARRAY_TYPE>` is one of the above-mentioned primitive or array types (_not_ an enumeration) and specifies the type of the `value` in the key-value pairs.
The `<PRIMITIVE_OR_ARRAY_TYPE>` is one of the above-mentioned primitive or array types (_not_ an enum) and specifies the type of the `value` in the dictionary.

The following is an example for defining a template type attribute and it's resolution:

Expand All @@ -360,13 +360,13 @@ groups:
- id: http.request.header
type: template[string[]]
brief: >
HTTP request headers, `<key>` being the normalized HTTP Header name (lowercase, with `-` characters replaced by `_`), the value being the header values.
HTTP request headers, the key being the normalized HTTP header name (lowercase, with `-` characters replaced by `_`), the value being the header values.
examples: ['http.request.header.content_type=["application/json"]', 'http.request.header.x_forwarded_for=["1.2.3.4", "1.2.3.5"]']
note: |
...
```
In this example the definition will be resolved into a key-value set of attributes `http.request.header.<key>` where `<key>` will be replaced by the actual HTTP header name, and the value of the attributes is of type `string[]` that carries the HTTP header value.
In this example the definition will be resolved into a dictionary of attributes `http.request.header.<key>` where `<key>` will be replaced by the actual HTTP header name, and the value of the attributes is of type `string[]` that carries the HTTP header value.

##### Enumeration

Expand Down

0 comments on commit 007d33e

Please sign in to comment.