template.j2 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. {%- macro keyval_method(type) -%}
  2. {%- if type == "string" -%}
  3. String
  4. {%- elif type == "string[]" -%}
  5. StringSlice
  6. {%- elif type == "int" -%}
  7. Int
  8. {%- elif type == "int[]" -%}
  9. IntSlice
  10. {%- elif type == "double" -%}
  11. Float64
  12. {%- elif type == "double[]" -%}
  13. Float64Slice
  14. {%- elif type == "boolean" -%}
  15. Bool
  16. {%- elif type == "boolean[]" -%}
  17. BoolSlice
  18. {%- endif -%}
  19. {%- endmacro -%}
  20. {%- macro to_go_attr_type(type, val) -%}
  21. {{keyval_method(type)}}({% if type == "string" %}"{{val}}"{% else %}{{val}}{% endif %})
  22. {%- endmacro -%}
  23. {%- macro to_go_name(fqn) -%}
  24. {{fqn | replace(".", " ") | replace("_", " ") | title | replace(" ", "")}}
  25. {%- endmacro -%}
  26. {%- macro it_reps(brief) -%}
  27. It represents {% if brief[:2] == "A " or brief[:3] == "An " or brief[:4] == "The " -%}
  28. {{ brief[0]|lower }}{{ brief[1:] }}
  29. {%- else -%}
  30. the {{ brief[0]|lower }}{{ brief[1:] }}
  31. {%- endif -%}
  32. {%- endmacro -%}
  33. {%- macro keydoc(attr) -%}
  34. {{ to_go_name(attr.fqn) }}Key is the attribute Key conforming to the "{{ attr.fqn }}" semantic conventions. {{ it_reps(attr.brief) }}
  35. {%- endmacro -%}
  36. {%- macro keydetails(attr) -%}
  37. {%- if attr.attr_type is string %}
  38. Type: {{ attr.attr_type }}
  39. {%- else %}
  40. Type: Enum
  41. {%- endif %}
  42. {%- if attr.requirement_level == RequirementLevel.REQUIRED %}
  43. RequirementLevel: Required
  44. {%- elif attr.requirement_level == RequirementLevel.CONDITIONALLY_REQUIRED %}
  45. RequirementLevel: ConditionallyRequired
  46. {%- if attr.requirement_level_msg != "" %} ({{ attr.requirement_level_msg }}){%- endif %}
  47. {%- elif attr.requirement_level == RequirementLevel.RECOMMENDED %}
  48. RequirementLevel: Recommended
  49. {%- if attr.requirement_level_msg != "" %} ({{ attr.requirement_level_msg }}){%- endif %}
  50. {%- else %}
  51. RequirementLevel: Optional
  52. {%- endif %}
  53. {{ attr.stability | replace("Level.", ": ") | capitalize }}
  54. {%- if attr.deprecated != None %}
  55. Deprecated: {{ attr.deprecated }}
  56. {%- endif %}
  57. {%- if attr.examples is iterable %}
  58. Examples: {{ attr.examples | pprint | trim("[]") }}
  59. {%- endif %}
  60. {%- if attr.note %}
  61. Note: {{ attr.note }}
  62. {%- endif %}
  63. {%- endmacro -%}
  64. {%- macro fndoc(attr) -%}
  65. // {{ to_go_name(attr.fqn) }} returns an attribute KeyValue conforming to the "{{ attr.fqn }}" semantic conventions. {{ it_reps(attr.brief) }}
  66. {%- endmacro -%}
  67. {%- macro to_go_func(type, name) -%}
  68. {%- if type == "string" -%}
  69. func {{name}}(val string) attribute.KeyValue {
  70. {%- elif type == "string[]" -%}
  71. func {{name}}(val ...string) attribute.KeyValue {
  72. {%- elif type == "int" -%}
  73. func {{name}}(val int) attribute.KeyValue {
  74. {%- elif type == "int[]" -%}
  75. func {{name}}(val ...int) attribute.KeyValue {
  76. {%- elif type == "double" -%}
  77. func {{name}}(val float64) attribute.KeyValue {
  78. {%- elif type == "double[]" -%}
  79. func {{name}}(val ...float64) attribute.KeyValue {
  80. {%- elif type == "boolean" -%}
  81. func {{name}}(val bool) attribute.KeyValue {
  82. {%- elif type == "boolean[]" -%}
  83. func {{name}}(val ...bool) attribute.KeyValue {
  84. {%- endif -%}
  85. return {{name}}Key.{{keyval_method(type)}}(val)
  86. }
  87. {%- endmacro -%}
  88. {%- macro sentence_case(text) -%}
  89. {{ text[0]|upper}}{{text[1:] }}
  90. {%- endmacro -%}
  91. // Copyright The OpenTelemetry Authors
  92. //
  93. // Licensed under the Apache License, Version 2.0 (the "License");
  94. // you may not use this file except in compliance with the License.
  95. // You may obtain a copy of the License at
  96. //
  97. // http://www.apache.org/licenses/LICENSE-2.0
  98. //
  99. // Unless required by applicable law or agreed to in writing, software
  100. // distributed under the License is distributed on an "AS IS" BASIS,
  101. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  102. // See the License for the specific language governing permissions and
  103. // limitations under the License.
  104. // Code generated from semantic convention specification. DO NOT EDIT.
  105. package semconv // import [[IMPORTPATH]]
  106. import "go.opentelemetry.io/otel/attribute"
  107. {% for semconv in semconvs -%}
  108. {%- if semconvs[semconv].attributes | rejectattr("ref") | selectattr("is_local") | sort(attribute=fqn) | length > 0 -%}
  109. // {{ sentence_case(semconvs[semconv].brief | replace("This document defines ", "")) | wordwrap(76, break_long_words=false, break_on_hyphens=false, wrapstring="\n// ") }}
  110. const (
  111. {%- for attr in semconvs[semconv].attributes if attr.is_local and not attr.ref %}
  112. // {{ keydoc(attr) | wordwrap(72, break_long_words=false, break_on_hyphens=false, wrapstring="\n\t// ") }}
  113. // {{ keydetails(attr) | wordwrap(72, break_long_words=false, break_on_hyphens=false, wrapstring="\n\t// ") }}
  114. {{to_go_name(attr.fqn)}}Key = attribute.Key("{{attr.fqn}}")
  115. {% endfor -%}
  116. )
  117. {%- for attr in semconvs[semconv].attributes if attr.is_local and not attr.ref -%}
  118. {%- if attr.attr_type is not string %}
  119. var (
  120. {%- for val in attr.attr_type.members %}
  121. // {{ val.brief | to_doc_brief }}
  122. {{to_go_name("{}.{}".format(attr.fqn, val.member_id))}} = {{to_go_name(attr.fqn)}}Key.{{to_go_attr_type(attr.attr_type.enum_type, val.value)}}
  123. {%- endfor %}
  124. )
  125. {%- endif -%}
  126. {%- endfor %}
  127. {%- for attr in semconvs[semconv].attributes if attr.is_local and not attr.ref -%}
  128. {%- if attr.attr_type is string %}
  129. {{ fndoc(attr) | wordwrap(76, break_long_words=false, break_on_hyphens=false, wrapstring="\n// ") }}
  130. {{to_go_func(attr.attr_type, to_go_name(attr.fqn))}}
  131. {%- endif -%}
  132. {%- endfor %}
  133. {% endif %}
  134. {% endfor -%}