Skip to content

Commit

Permalink
Add test for metric code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova committed Sep 28, 2023
1 parent b982d95 commit 6a0b479
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 20 deletions.
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";

}
14 changes: 14 additions & 0 deletions semantic-conventions/src/tests/data/jinja/metrics/metrics_template
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
28 changes: 22 additions & 6 deletions semantic-conventions/src/tests/semconv/templating/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

0 comments on commit 6a0b479

Please sign in to comment.