Skip to content

Commit 5bf0048

Browse files
committed
http metrics test
1 parent ee00bbb commit 5bf0048

File tree

3 files changed

+185
-1
lines changed

3 files changed

+185
-1
lines changed

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/metric_exporter/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
AggregationTemporality,
5959
Gauge,
6060
Histogram as HistogramType,
61-
Metric,
6261
MetricExporter,
6362
MetricExportResult,
6463
MetricsData,

exporter/opentelemetry-exporter-otlp-proto-http/tests/metrics/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
from unittest.mock import patch
17+
18+
import requests
19+
20+
from opentelemetry.exporter.otlp.proto.http import Compression
21+
from opentelemetry.exporter.otlp.proto.http.metric_exporter import (
22+
DEFAULT_COMPRESSION,
23+
DEFAULT_ENDPOINT,
24+
DEFAULT_TIMEOUT,
25+
DEFAULT_METRICS_EXPORT_PATH,
26+
OTLPMetricExporter,
27+
)
28+
from opentelemetry.sdk.environment_variables import (
29+
OTEL_EXPORTER_OTLP_CERTIFICATE,
30+
OTEL_EXPORTER_OTLP_COMPRESSION,
31+
OTEL_EXPORTER_OTLP_ENDPOINT,
32+
OTEL_EXPORTER_OTLP_HEADERS,
33+
OTEL_EXPORTER_OTLP_TIMEOUT,
34+
OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE,
35+
OTEL_EXPORTER_OTLP_METRICS_COMPRESSION,
36+
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
37+
OTEL_EXPORTER_OTLP_METRICS_HEADERS,
38+
OTEL_EXPORTER_OTLP_METRICS_TIMEOUT,
39+
)
40+
41+
OS_ENV_ENDPOINT = "os.env.base"
42+
OS_ENV_CERTIFICATE = "os/env/base.crt"
43+
OS_ENV_HEADERS = "envHeader1=val1,envHeader2=val2"
44+
OS_ENV_TIMEOUT = "30"
45+
46+
47+
# pylint: disable=protected-access
48+
class TestOTLPMetricExporter(unittest.TestCase):
49+
def test_constructor_default(self):
50+
51+
exporter = OTLPMetricExporter()
52+
53+
self.assertEqual(
54+
exporter._endpoint, DEFAULT_ENDPOINT + DEFAULT_METRICS_EXPORT_PATH
55+
)
56+
self.assertEqual(exporter._certificate_file, True)
57+
self.assertEqual(exporter._timeout, DEFAULT_TIMEOUT)
58+
self.assertIs(exporter._compression, DEFAULT_COMPRESSION)
59+
self.assertEqual(exporter._headers, {})
60+
self.assertIsInstance(exporter._session, requests.Session)
61+
62+
@patch.dict(
63+
"os.environ",
64+
{
65+
OTEL_EXPORTER_OTLP_CERTIFICATE: OS_ENV_CERTIFICATE,
66+
OTEL_EXPORTER_OTLP_COMPRESSION: Compression.Gzip.value,
67+
OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT,
68+
OTEL_EXPORTER_OTLP_HEADERS: OS_ENV_HEADERS,
69+
OTEL_EXPORTER_OTLP_TIMEOUT: OS_ENV_TIMEOUT,
70+
OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE: "metrics/certificate.env",
71+
OTEL_EXPORTER_OTLP_METRICS_COMPRESSION: Compression.Deflate.value,
72+
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: "https://metrics.endpoint.env",
73+
OTEL_EXPORTER_OTLP_METRICS_HEADERS: "metricsEnv1=val1,metricsEnv2=val2,metricEnv3===val3==",
74+
OTEL_EXPORTER_OTLP_METRICS_TIMEOUT: "40",
75+
},
76+
)
77+
def test_exporter_metrics_env_take_priority(self):
78+
exporter = OTLPMetricExporter()
79+
80+
self.assertEqual(exporter._endpoint, "https://metrics.endpoint.env")
81+
self.assertEqual(exporter._certificate_file, "metrics/certificate.env")
82+
self.assertEqual(exporter._timeout, 40)
83+
self.assertIs(exporter._compression, Compression.Deflate)
84+
self.assertEqual(
85+
exporter._headers,
86+
{
87+
"metricsenv1": "val1",
88+
"metricsenv2": "val2",
89+
"metricenv3": "==val3==",
90+
},
91+
)
92+
self.assertIsInstance(exporter._session, requests.Session)
93+
94+
@patch.dict(
95+
"os.environ",
96+
{
97+
OTEL_EXPORTER_OTLP_CERTIFICATE: OS_ENV_CERTIFICATE,
98+
OTEL_EXPORTER_OTLP_COMPRESSION: Compression.Gzip.value,
99+
OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT,
100+
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: "https://metrics.endpoint.env",
101+
OTEL_EXPORTER_OTLP_HEADERS: OS_ENV_HEADERS,
102+
OTEL_EXPORTER_OTLP_TIMEOUT: OS_ENV_TIMEOUT,
103+
},
104+
)
105+
def test_exporter_constructor_take_priority(self):
106+
exporter = OTLPMetricExporter(
107+
endpoint="example.com/1234",
108+
certificate_file="path/to/service.crt",
109+
headers={"testHeader1": "value1", "testHeader2": "value2"},
110+
timeout=20,
111+
compression=Compression.NoCompression,
112+
session=requests.Session(),
113+
)
114+
115+
self.assertEqual(exporter._endpoint, "example.com/1234")
116+
self.assertEqual(exporter._certificate_file, "path/to/service.crt")
117+
self.assertEqual(exporter._timeout, 20)
118+
self.assertIs(exporter._compression, Compression.NoCompression)
119+
self.assertEqual(
120+
exporter._headers,
121+
{"testHeader1": "value1", "testHeader2": "value2"},
122+
)
123+
self.assertIsInstance(exporter._session, requests.Session)
124+
125+
@patch.dict(
126+
"os.environ",
127+
{
128+
OTEL_EXPORTER_OTLP_CERTIFICATE: OS_ENV_CERTIFICATE,
129+
OTEL_EXPORTER_OTLP_COMPRESSION: Compression.Gzip.value,
130+
OTEL_EXPORTER_OTLP_HEADERS: OS_ENV_HEADERS,
131+
OTEL_EXPORTER_OTLP_TIMEOUT: OS_ENV_TIMEOUT,
132+
},
133+
)
134+
def test_exporter_env(self):
135+
136+
exporter = OTLPMetricExporter()
137+
138+
self.assertEqual(exporter._certificate_file, OS_ENV_CERTIFICATE)
139+
self.assertEqual(exporter._timeout, int(OS_ENV_TIMEOUT))
140+
self.assertIs(exporter._compression, Compression.Gzip)
141+
self.assertEqual(
142+
exporter._headers, {"envheader1": "val1", "envheader2": "val2"}
143+
)
144+
145+
@patch.dict(
146+
"os.environ",
147+
{OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT},
148+
)
149+
def test_exporter_env_endpoint_without_slash(self):
150+
151+
exporter = OTLPMetricExporter()
152+
153+
self.assertEqual(
154+
exporter._endpoint,
155+
OS_ENV_ENDPOINT + f"/{DEFAULT_METRICS_EXPORT_PATH}",
156+
)
157+
158+
@patch.dict(
159+
"os.environ",
160+
{OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT + "/"},
161+
)
162+
def test_exporter_env_endpoint_with_slash(self):
163+
164+
exporter = OTLPMetricExporter()
165+
166+
self.assertEqual(
167+
exporter._endpoint,
168+
OS_ENV_ENDPOINT + f"/{DEFAULT_METRICS_EXPORT_PATH}",
169+
)
170+
171+
@patch.dict(
172+
"os.environ",
173+
{
174+
OTEL_EXPORTER_OTLP_HEADERS: "envHeader1=val1,envHeader2=val2,missingValue"
175+
},
176+
)
177+
def test_headers_parse_from_env(self):
178+
179+
with self.assertLogs(level="WARNING") as cm:
180+
_ = OTLPMetricExporter()
181+
182+
self.assertEqual(
183+
cm.records[0].message,
184+
"Header doesn't match the format: missingValue.",
185+
)

0 commit comments

Comments
 (0)