Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for metric code generation #209

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions semantic-conventions/src/tests/data/jinja/metrics/expected.java
Original file line number Diff line number Diff line change
@@ -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";

}
Original file line number Diff line number Diff line change
@@ -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 %}
}
23 changes: 23 additions & 0 deletions semantic-conventions/src/tests/data/jinja/units/expected.java
Original file line number Diff line number Diff line change
@@ -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}";

}
38 changes: 38 additions & 0 deletions semantic-conventions/src/tests/data/yaml/metrics/metrics.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


def test_build_units(open_test_file):
with open_test_file(os.path.join("yaml", "metrics", "units.yaml")) as yaml_file:
with open_test_file(os.path.join("yaml", "units", "units.yaml")) as yaml_file:
conventions = parse_semantic_convention_groups(yaml_file)

assert len(conventions) == 1
Expand Down Expand Up @@ -40,7 +40,7 @@ def test_build_units(open_test_file):

def test_build_units_bad(open_test_file):
with pytest.raises(ValidationError) as excinfo, open_test_file(
os.path.join("yaml", "metrics", "units_bad_with_attributes.yaml")
os.path.join("yaml", "units", "units_bad_with_attributes.yaml")
) as yaml_file:
parse_semantic_convention_groups(yaml_file)
assert "attributes" in str(excinfo.value)
29 changes: 22 additions & 7 deletions semantic-conventions/src/tests/semconv/templating/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@

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", "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()
Expand All @@ -24,21 +41,19 @@ def test_codegen_units(test_file_path, read_test_file):
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)

output = io.StringIO()
renderer.render(semconv, template_path, output, None)
result = output.getvalue()

expected = read_test_file(
"jinja", "metrics", "expected_trim_whitespace_enabled.java"
)
expected = read_test_file("jinja", "units", "expected_trim_whitespace_enabled.java")

assert result == expected

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,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/")
Expand Down
Loading