Skip to content

Commit

Permalink
generate for semconv 1.20.0
Browse files Browse the repository at this point in the history
Rakefile reorganized into file and directory tasks where possible.
  • Loading branch information
robbkidd committed Jan 16, 2024
1 parent 270a993 commit 3001d5a
Show file tree
Hide file tree
Showing 8 changed files with 1,260 additions and 35 deletions.
89 changes: 62 additions & 27 deletions semantic_conventions/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ require 'yard'
require 'rubocop/rake_task'
require 'tmpdir'

SPEC_VERSION = '1.10.0'

RuboCop::RakeTask.new

Rake::TestTask.new :test do |t|
Expand All @@ -31,34 +29,71 @@ else
task default: %i[generate test rubocop yard]
end

task :generate do
cwd = Dir.pwd
SPEC_VERSION = '1.20.0'
PREV_SPEC_VERSION = Dir.children('./lib/opentelemetry/semantic_conventions/')
.select { |dir| dir != SPEC_VERSION && File.directory?("./lib/opentelemetry/semantic_conventions/#{dir}") }
.map { |dir| Gem::Version.new(dir) }
.max

GENERATOR_VERSION = '0.22.0'
semconvrepodir = './tmpsemconvrepo'
gen_output_dir = "./lib/opentelemetry/semantic_conventions/#{SPEC_VERSION}"

Dir.mktmpdir('opentelemetry-specification', Dir.pwd) do |tmpdir|
`git clone https://github.com/open-telemetry/opentelemetry-specification.git #{tmpdir}`
Dir.chdir(tmpdir) do
`git fetch`
`git checkout "v#{SPEC_VERSION}"`
end

%w[trace resource].each do |kind|
cmd = %W[
docker run --rm
-v "#{tmpdir}/semantic_conventions/#{kind}":/source
-v "#{cwd}/templates":/templates
-v "#{cwd}/lib":/output
otel/semconvgen:0.11.1
-f /source code
--template /templates/semantic_conventions.j2
--output /output/opentelemetry/semantic_conventions/#{kind}.rb
-Dmodule=#{kind[0].upcase}#{kind[1..]}
]

puts "Running: #{cmd.join(' ')}"
`#{cmd.join(' ')}`
end
task generate: [:update_gem_version, :update_includes, "#{gen_output_dir}/trace.rb", "#{gen_output_dir}/resource.rb"]

directory semconvrepodir do
`git clone --depth=1 --branch v#{SPEC_VERSION} https://github.com/open-telemetry/opentelemetry-specification.git #{semconvrepodir}`
end

task check_out_semconv_version: [semconvrepodir] do
Dir.chdir(semconvrepodir) do
`git fetch`
`git checkout "v#{SPEC_VERSION}"`
end
end

directory gen_output_dir

file "#{gen_output_dir}/trace.rb" => [:check_out_semconv_version, gen_output_dir] do
semconvgen(semconvrepodir, kind: 'trace', only: 'span,event,attribute_group,scope')
end

file "#{gen_output_dir}/resource.rb" => [:check_out_semconv_version, gen_output_dir] do
semconvgen(semconvrepodir, kind: 'resource', only: 'resource')
end

def semconvgen(semconvrepo, kind:, only:)
cwd = Dir.pwd
cmd = %W[
docker run --rm
-v "#{semconvrepo}/semantic_conventions/":/source
-v "#{cwd}/templates":/templates
-v "#{cwd}/lib":/output
otel/semconvgen:#{GENERATOR_VERSION}
--only #{only}
--yaml-root /source code
--template /templates/semantic_conventions.j2
--output /output/opentelemetry/semantic_conventions/#{SPEC_VERSION}/#{kind}.rb
-Dkind=#{kind}
-Dmodule=#{kind[0].upcase}#{kind[1..]}
-Dspec_version=#{SPEC_VERSION}
-Dprev_spec_version=#{PREV_SPEC_VERSION}
]

puts "Running: #{cmd.join(' ')}"
`#{cmd.join(' ')}`
end

task :update_includes do
`sed -i.bak "s/::SemanticConventions_.*::/::SemanticConventions_#{SPEC_VERSION.tr(".", "_")}::/g" lib/opentelemetry/semantic_conventions/trace.rb`
`sed -i.bak "s/::SemanticConventions_.*::/::SemanticConventions_#{SPEC_VERSION.tr(".", "_")}::/g" lib/opentelemetry/semantic_conventions/resource.rb`
`sed -i.bak "s/#{PREV_SPEC_VERSION.to_s.gsub(/\./, "\\.")}/#{SPEC_VERSION}/" lib/opentelemetry/semantic_conventions.rb`
`rm lib/opentelemetry/semantic_conventions/trace.rb.bak`
`rm lib/opentelemetry/semantic_conventions/resource.rb.bak`
`rm lib/opentelemetry/semantic_conventions.rb.bak`
end

task :update_gem_version do
`sed -i.bak "s/VERSION = '.*'/VERSION = '#{SPEC_VERSION}'/g" lib/opentelemetry/semantic_conventions/version.rb`
`rm lib/opentelemetry/semantic_conventions/version.rb.bak`
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module SemanticConventions
end
end

require_relative 'semantic_conventions/1.10.0/trace'
require_relative 'semantic_conventions/1.20.0/trace'
require_relative 'semantic_conventions/trace'
require_relative 'semantic_conventions/1.10.0/resource'
require_relative 'semantic_conventions/1.20.0/resource'
require_relative 'semantic_conventions/resource'

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
module OpenTelemetry
module SemanticConventions
module Resource
include OpenTelemetry::SemanticConventions_1_10_0::Resource
def self.const_missing(const_name)
attribute_name = OpenTelemetry::SemanticConventions_1_20_0::Resource.const_get(const_name)
super(const_name) unless attribute_name

const_set(const_name, attribute_name)
attribute_name
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
module OpenTelemetry
module SemanticConventions
module Trace
include OpenTelemetry::SemanticConventions_1_10_0::Trace
def self.const_missing(const_name)
attribute_name = OpenTelemetry::SemanticConventions_1_20_0::Trace.const_get(const_name)
super(const_name) unless attribute_name

const_set(const_name, attribute_name)
attribute_name
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

module OpenTelemetry
module SemanticConventions
VERSION = '1.10.0'
VERSION = '1.20.0'
end
end
22 changes: 19 additions & 3 deletions semantic_conventions/templates/semantic_conventions.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,31 @@
#
# SPDX-License-Identifier: Apache-2.0

require_relative '../{{prev_spec_version}}/{{kind}}'

module OpenTelemetry
module SemanticConventions
module SemanticConventions_{{spec_version | replace(".", "_")}} # rubocop:disable Naming/ClassAndModuleCamelCase
# https://github.com/open-telemetry/opentelemetry-specification/blob/v{{spec_version}}/specification/
module {{module}}
{%- for attribute in attributes | unique(attribute="fqn") %}
{%- if (prev_spec_version | string()) != "" %}
def self.const_missing(const_name)
attribute_name = OpenTelemetry::SemanticConventions_{{prev_spec_version | replace(".", "_")}}::Trace.const_get(const_name)
super(const_name) unless attribute_name

warn "#{const_name} is deprecated."
const_set(const_name, attribute_name)
attribute_name
end
{# blank line #}
{%- endif %}
{%- for attribute in attributes | unique(attribute="fqn") | sort(attribute="fqn") %}
# {{ attribute.brief | to_doc_brief | regex_replace(pattern="\n", replace="\n # ") }}
{%- if attribute.note %}
#
# @note {{ attribute.note | to_doc_brief | regex_replace(pattern="\n", replace="\n # ") }}
{%- endif %}
{%- if attribute.deprecated %}
{%- if (attribute.stability | string()) == "StabilityLevel.DEPRECATED" %}
#
# @deprecated {{ attribute.deprecated | to_doc_brief | regex_replace(pattern="\n", replace="\n # ") }}
{%- endif %}
{{ attribute.fqn | to_const_name }} = '{{ attribute.fqn }}'
Expand Down

0 comments on commit 3001d5a

Please sign in to comment.