From 6a0b479ee30732be0063a0f834fcc6e13fc2bfdb Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Wed, 27 Sep 2023 20:31:21 -0700 Subject: [PATCH] Add test for metric code generation --- .../tests/data/jinja/metrics/expected.java | 32 +++++++++------- .../tests/data/jinja/metrics/metrics_template | 14 +++++++ .../src/tests/data/jinja/units/expected.java | 23 +++++++++++ .../expected_trim_whitespace_enabled.java | 0 .../jinja/{metrics => units}/units_template | 0 .../units_template_trim_whitespace_enabled | 0 .../src/tests/data/yaml/metrics/metrics.yaml | 38 +++++++++++++++++++ .../data/yaml/{metrics => units}/units.yaml | 0 .../units_bad_with_attributes.yaml | 0 .../src/tests/semconv/templating/test_code.py | 28 +++++++++++--- .../tests/semconv/templating/test_markdown.py | 2 +- 11 files changed, 117 insertions(+), 20 deletions(-) create mode 100644 semantic-conventions/src/tests/data/jinja/metrics/metrics_template create mode 100644 semantic-conventions/src/tests/data/jinja/units/expected.java rename semantic-conventions/src/tests/data/jinja/{metrics => units}/expected_trim_whitespace_enabled.java (100%) rename semantic-conventions/src/tests/data/jinja/{metrics => units}/units_template (100%) rename semantic-conventions/src/tests/data/jinja/{metrics => units}/units_template_trim_whitespace_enabled (100%) create mode 100644 semantic-conventions/src/tests/data/yaml/metrics/metrics.yaml rename semantic-conventions/src/tests/data/yaml/{metrics => units}/units.yaml (100%) rename semantic-conventions/src/tests/data/yaml/{metrics => units}/units_bad_with_attributes.yaml (100%) diff --git a/semantic-conventions/src/tests/data/jinja/metrics/expected.java b/semantic-conventions/src/tests/data/jinja/metrics/expected.java index 46ec3069..04fefa2c 100644 --- a/semantic-conventions/src/tests/data/jinja/metrics/expected.java +++ b/semantic-conventions/src/tests/data/jinja/metrics/expected.java @@ -1,23 +1,29 @@ package io.opentelemetry.instrumentation.api.metric; -class Units { +class Metrics { /** - * Use this unit for Metric Instruments recording values - * representing fraction of a total. - **/ - public static final String PERCENT = "%"; + * Measures the duration of inbound HTTP requests + * + * Instrument: histogram + * Unit: s + */ + public static final String HTTP_SERVER_REQUEST_DURATION = "http.server.request.duration"; /** - * Use this unit for Metric Instruments recording values - * representing time. - **/ - public static final String NANOSECOND = "NS"; + * Measures the number of concurrent HTTP requests that are currently in-flight + * + * Instrument: updowncounter + * Unit: {request} + */ + public static final String HTTP_SERVER_ACTIVE_REQUESTS = "http.server.active_requests"; /** - * Use this unit for Metric Instruments recording values - * representing connections. - **/ - public static final String CONNECTIONS = "{connections}"; + * Measures the size of HTTP request messages + * + * Instrument: histogram + * Unit: By + */ + public static final String HTTP_SERVER_REQUEST_BODY_SIZE = "http.server.request.body.size"; } \ No newline at end of file diff --git a/semantic-conventions/src/tests/data/jinja/metrics/metrics_template b/semantic-conventions/src/tests/data/jinja/metrics/metrics_template new file mode 100644 index 00000000..83532699 --- /dev/null +++ b/semantic-conventions/src/tests/data/jinja/metrics/metrics_template @@ -0,0 +1,14 @@ +package io.opentelemetry.instrumentation.api.metric; + +class Metrics { +{% for id in semconvs %}{%- if semconvs[id].GROUP_TYPE_NAME == 'metric' %}{% set metric = semconvs[id] %} + /** + * {{metric.brief | to_doc_brief}} + * + * Instrument: {{metric.instrument }} + * Unit: {{metric.unit }} + */ + public static final String {{metric.metric_name | to_const_name}} = "{{metric.metric_name}}"; +{# Extra line #} +{%- endif %}{% endfor %} +} diff --git a/semantic-conventions/src/tests/data/jinja/units/expected.java b/semantic-conventions/src/tests/data/jinja/units/expected.java new file mode 100644 index 00000000..46ec3069 --- /dev/null +++ b/semantic-conventions/src/tests/data/jinja/units/expected.java @@ -0,0 +1,23 @@ +package io.opentelemetry.instrumentation.api.metric; + +class Units { + + /** + * Use this unit for Metric Instruments recording values + * representing fraction of a total. + **/ + public static final String PERCENT = "%"; + + /** + * Use this unit for Metric Instruments recording values + * representing time. + **/ + public static final String NANOSECOND = "NS"; + + /** + * Use this unit for Metric Instruments recording values + * representing connections. + **/ + public static final String CONNECTIONS = "{connections}"; + +} \ No newline at end of file diff --git a/semantic-conventions/src/tests/data/jinja/metrics/expected_trim_whitespace_enabled.java b/semantic-conventions/src/tests/data/jinja/units/expected_trim_whitespace_enabled.java similarity index 100% rename from semantic-conventions/src/tests/data/jinja/metrics/expected_trim_whitespace_enabled.java rename to semantic-conventions/src/tests/data/jinja/units/expected_trim_whitespace_enabled.java diff --git a/semantic-conventions/src/tests/data/jinja/metrics/units_template b/semantic-conventions/src/tests/data/jinja/units/units_template similarity index 100% rename from semantic-conventions/src/tests/data/jinja/metrics/units_template rename to semantic-conventions/src/tests/data/jinja/units/units_template diff --git a/semantic-conventions/src/tests/data/jinja/metrics/units_template_trim_whitespace_enabled b/semantic-conventions/src/tests/data/jinja/units/units_template_trim_whitespace_enabled similarity index 100% rename from semantic-conventions/src/tests/data/jinja/metrics/units_template_trim_whitespace_enabled rename to semantic-conventions/src/tests/data/jinja/units/units_template_trim_whitespace_enabled diff --git a/semantic-conventions/src/tests/data/yaml/metrics/metrics.yaml b/semantic-conventions/src/tests/data/yaml/metrics/metrics.yaml new file mode 100644 index 00000000..6a72bc0a --- /dev/null +++ b/semantic-conventions/src/tests/data/yaml/metrics/metrics.yaml @@ -0,0 +1,38 @@ +groups: + - id: metric_attributes.http.server + type: attribute_group + brief: "server attributes." + attributes: + - id: http.request.method + type: string + brief: "The HTTP request method." + examples: 'GET' + - id: http.response.status_code + type: string + brief: "The HTTP response status code." + examples: '200' + + - id: metric.http.server.request.duration + type: metric + metric_name: http.server.request.duration + brief: "Measures the duration of inbound HTTP requests." + instrument: histogram + unit: "s" + extends: metric_attributes.http.server + + - id: metric.http.server.active_requests + type: metric + metric_name: http.server.active_requests + brief: "Measures the number of concurrent HTTP requests that are currently in-flight." + instrument: updowncounter + unit: "{request}" + attributes: + - ref: http.request.method + + - id: metric.http.server.request.body.size + type: metric + metric_name: http.server.request.body.size + brief: "Measures the size of HTTP request messages." + instrument: histogram + unit: "By" + extends: metric_attributes.http.server \ No newline at end of file diff --git a/semantic-conventions/src/tests/data/yaml/metrics/units.yaml b/semantic-conventions/src/tests/data/yaml/units/units.yaml similarity index 100% rename from semantic-conventions/src/tests/data/yaml/metrics/units.yaml rename to semantic-conventions/src/tests/data/yaml/units/units.yaml diff --git a/semantic-conventions/src/tests/data/yaml/metrics/units_bad_with_attributes.yaml b/semantic-conventions/src/tests/data/yaml/units/units_bad_with_attributes.yaml similarity index 100% rename from semantic-conventions/src/tests/data/yaml/metrics/units_bad_with_attributes.yaml rename to semantic-conventions/src/tests/data/yaml/units/units_bad_with_attributes.yaml diff --git a/semantic-conventions/src/tests/semconv/templating/test_code.py b/semantic-conventions/src/tests/semconv/templating/test_code.py index 643505c9..bb2d7234 100644 --- a/semantic-conventions/src/tests/semconv/templating/test_code.py +++ b/semantic-conventions/src/tests/semconv/templating/test_code.py @@ -6,29 +6,45 @@ def test_codegen_units(test_file_path, read_test_file): semconv = SemanticConventionSet(debug=False) - semconv.parse(test_file_path("yaml", "metrics", "units.yaml")) + semconv.parse(test_file_path("yaml", "units", "units.yaml")) semconv.finish() - template_path = test_file_path("jinja", "metrics", "units_template") + template_path = test_file_path("jinja", "units", "units_template") renderer = CodeRenderer({}, trim_whitespace=False) output = io.StringIO() renderer.render(semconv, template_path, output, None) result = output.getvalue() - expected = read_test_file("jinja", "metrics", "expected.java") + expected = read_test_file("jinja", "units", "expected.java") assert result == expected +def test_codegen_metrics(test_file_path, read_test_file): + semconv = SemanticConventionSet(debug=False) + semconv.parse(test_file_path("yaml", "metrics", "metrics.yaml")) + semconv.finish() + + template_path = test_file_path("jinja", "metrics", "metrics_template") + renderer = CodeRenderer({}, trim_whitespace=False) + + output = io.StringIO() + renderer.render(semconv, template_path, output, None) + result = output.getvalue() + + expected = read_test_file("jinja", "metrics", "expected.java") + + assert result == expected + def test_strip_blocks_enabled(test_file_path, read_test_file): """Tests that the Jinja whitespace control params are fed to the Jinja environment""" semconv = SemanticConventionSet(debug=False) - semconv.parse(test_file_path("yaml", "metrics", "units.yaml")) + semconv.parse(test_file_path("yaml", "units", "units.yaml")) semconv.finish() template_path = test_file_path( - "jinja", "metrics", "units_template_trim_whitespace_enabled" + "jinja", "units", "units_template_trim_whitespace_enabled" ) renderer = CodeRenderer({}, trim_whitespace=True) @@ -37,7 +53,7 @@ def test_strip_blocks_enabled(test_file_path, read_test_file): result = output.getvalue() expected = read_test_file( - "jinja", "metrics", "expected_trim_whitespace_enabled.java" + "jinja", "units", "expected_trim_whitespace_enabled.java" ) assert result == expected diff --git a/semantic-conventions/src/tests/semconv/templating/test_markdown.py b/semantic-conventions/src/tests/semconv/templating/test_markdown.py index 540243b4..73d83dda 100644 --- a/semantic-conventions/src/tests/semconv/templating/test_markdown.py +++ b/semantic-conventions/src/tests/semconv/templating/test_markdown.py @@ -115,7 +115,7 @@ def test_wrong_duplicate(self): self.assertIn("already defined", msg) def test_units(self): - self.check("markdown/metrics_unit/", extra_yaml_dirs=["yaml/metrics/"]) + self.check("markdown/metrics_unit/", extra_yaml_dirs=["yaml/units/"]) def test_event(self): self.check("markdown/event/")