Skip to content

Commit ffeb2d2

Browse files
committed
Merge branch 'main' into add_cred_envvar
2 parents 65f177b + 64de448 commit ffeb2d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2926
-238
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
1313
## Unreleased
1414

15+
- Add experimental composite samplers
16+
([#4714](https://github.com/open-telemetry/opentelemetry-python/pull/4714))
1517
- Add new environment variables to the SDK `OTEL_PYTHON_EXPORTER_OTLP_{METRICS/TRACES/LOGS}_CREDENTIAL_PROVIDER` that can be used to
1618
inject a `requests.Session` or `grpc.ChannelCredentials` object into OTLP exporters created during auto instrumentation [#4689](https://github.com/open-telemetry/opentelemetry-python/pull/4689).
1719
- Filter duplicate logs out of some internal `logger`'s logs on the export logs path that might otherwise endlessly log or cause a recursion depth exceeded issue in cases where logging itself results in an exception.
1820
([#4695](https://github.com/open-telemetry/opentelemetry-python/pull/4695)).
1921
- docs: linked the examples with their github source code location and added Prometheus example
2022
([#4728](https://github.com/open-telemetry/opentelemetry-python/pull/4728))
23+
- Permit to override default HTTP OTLP exporters headers
24+
([#4634](https://github.com/open-telemetry/opentelemetry-python/pull/4634))
25+
- semantic-conventions: Bump to 1.37.0
26+
([#4731](https://github.com/open-telemetry/opentelemetry-python/pull/4731))
2127

2228
## Version 1.36.0/0.57b0 (2025-07-29)
2329

docs/examples/auto-instrumentation/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from sys import argv
15+
import sys
1616

1717
from requests import get
1818

@@ -31,16 +31,16 @@
3131
BatchSpanProcessor(ConsoleSpanExporter())
3232
)
3333

34-
35-
assert len(argv) == 2
34+
# Get parameter from command line argument or use default value "testing"
35+
param_value = sys.argv[1] if len(sys.argv) > 1 else "testing"
3636

3737
with tracer.start_as_current_span("client"):
3838
with tracer.start_as_current_span("client-server"):
3939
headers = {}
4040
inject(headers)
4141
requested = get(
4242
"http://localhost:8082/server_request",
43-
params={"param": argv[1]},
43+
params={"param": param_value},
4444
headers=headers,
4545
)
4646

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 json
16+
import os
17+
import subprocess
18+
import sys
19+
import unittest
20+
21+
22+
class TestMetrics(unittest.TestCase):
23+
def test_metrics(self):
24+
"""Test that metrics example produces expected values"""
25+
# Run the metrics example
26+
test_script = f"{os.path.dirname(os.path.realpath(__file__))}/../metrics_example.py"
27+
28+
result = subprocess.run(
29+
[sys.executable, test_script],
30+
capture_output=True,
31+
text=True,
32+
timeout=10,
33+
check=True,
34+
)
35+
36+
# Script should run successfully
37+
self.assertEqual(result.returncode, 0)
38+
39+
# Parse the JSON output
40+
output_data = json.loads(result.stdout)
41+
42+
# Get the metrics from the JSON structure
43+
metrics = output_data["resource_metrics"][0]["scope_metrics"][0][
44+
"metrics"
45+
]
46+
47+
# Create a lookup dict for easier testing
48+
metrics_by_name = {metric["name"]: metric for metric in metrics}
49+
50+
# Test Counter: should be 1 (called counter.add(1))
51+
counter_value = metrics_by_name["counter"]["data"]["data_points"][0][
52+
"value"
53+
]
54+
self.assertEqual(counter_value, 1, "Counter should have value 1")
55+
56+
# Test UpDownCounter: should be -4 (1 + (-5) = -4)
57+
updown_value = metrics_by_name["updown_counter"]["data"][
58+
"data_points"
59+
][0]["value"]
60+
self.assertEqual(
61+
updown_value, -4, "UpDownCounter should have value -4"
62+
)
63+
64+
# Test Histogram: should have count=1, sum=99.9
65+
histogram_data = metrics_by_name["histogram"]["data"]["data_points"][0]
66+
self.assertEqual(
67+
histogram_data["count"], 1, "Histogram should have count 1"
68+
)
69+
self.assertEqual(
70+
histogram_data["sum"], 99.9, "Histogram should have sum 99.9"
71+
)
72+
73+
# Test Gauge: should be 1 (last value set)
74+
gauge_value = metrics_by_name["gauge"]["data"]["data_points"][0][
75+
"value"
76+
]
77+
self.assertEqual(gauge_value, 1, "Gauge should have value 1")
78+
79+
# Test Observable Counter: should be 1 (from callback)
80+
obs_counter_value = metrics_by_name["observable_counter"]["data"][
81+
"data_points"
82+
][0]["value"]
83+
self.assertEqual(
84+
obs_counter_value, 1, "Observable counter should have value 1"
85+
)
86+
87+
# Test Observable UpDownCounter: should be -10 (from callback)
88+
obs_updown_value = metrics_by_name["observable_updown_counter"][
89+
"data"
90+
]["data_points"][0]["value"]
91+
self.assertEqual(
92+
obs_updown_value,
93+
-10,
94+
"Observable updown counter should have value -10",
95+
)
96+
97+
# Test Observable Gauge: should be 9 (from callback)
98+
obs_gauge_value = metrics_by_name["observable_gauge"]["data"][
99+
"data_points"
100+
][0]["value"]
101+
self.assertEqual(
102+
obs_gauge_value, 9, "Observable gauge should have value 9"
103+
)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
# oltpcollector_example.py
16+
import os
17+
import subprocess
18+
import sys
19+
import unittest
20+
21+
22+
class TestOTLPCollector(unittest.TestCase):
23+
def test_otlpcollector(self):
24+
"""Test that OTLP collector example outputs 'Hello world!'"""
25+
dirpath = os.path.dirname(os.path.realpath(__file__))
26+
test_script = f"{dirpath}/../otlpcollector_example.py"
27+
28+
# Run the script with a short timeout since it will retry forever
29+
with subprocess.Popen(
30+
[sys.executable, test_script],
31+
stdout=subprocess.PIPE,
32+
stderr=subprocess.PIPE,
33+
text=True,
34+
) as process:
35+
# Wait 2 seconds then kill it (enough time to print "Hello world!")
36+
try:
37+
stdout, _ = process.communicate(timeout=2)
38+
except subprocess.TimeoutExpired:
39+
process.kill()
40+
stdout, _ = process.communicate()
41+
42+
# Check that it printed the expected message
43+
self.assertIn("Hello world!", stdout)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ def __init__(
126126
)
127127
self._session.headers.update(self._headers)
128128
self._session.headers.update(_OTLP_HTTP_HEADERS)
129+
# let users override our defaults
130+
self._session.headers.update(self._headers)
129131
if self._compression is not Compression.NoCompression:
130132
self._session.headers.update(
131133
{"Content-Encoding": self._compression.value}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ def __init__(
167167
)
168168
self._session.headers.update(self._headers)
169169
self._session.headers.update(_OTLP_HTTP_HEADERS)
170+
# let users override our defaults
171+
self._session.headers.update(self._headers)
170172
if self._compression is not Compression.NoCompression:
171173
self._session.headers.update(
172174
{"Content-Encoding": self._compression.value}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ def __init__(
123123
)
124124
self._session.headers.update(self._headers)
125125
self._session.headers.update(_OTLP_HTTP_HEADERS)
126+
# let users override our defaults
127+
self._session.headers.update(self._headers)
126128
if self._compression is not Compression.NoCompression:
127129
self._session.headers.update(
128130
{"Content-Encoding": self._compression.value}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
OS_ENV_CERTIFICATE = "os/env/base.crt"
8484
OS_ENV_CLIENT_CERTIFICATE = "os/env/client-cert.pem"
8585
OS_ENV_CLIENT_KEY = "os/env/client-key.pem"
86-
OS_ENV_HEADERS = "envHeader1=val1,envHeader2=val2"
86+
OS_ENV_HEADERS = "envHeader1=val1,envHeader2=val2,User-agent=Overridden"
8787
OS_ENV_TIMEOUT = "30"
8888

8989

@@ -153,7 +153,7 @@ def test_constructor_default(self):
153153
OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY: "metrics/client-key.pem",
154154
OTEL_EXPORTER_OTLP_METRICS_COMPRESSION: Compression.Deflate.value,
155155
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: "https://metrics.endpoint.env",
156-
OTEL_EXPORTER_OTLP_METRICS_HEADERS: "metricsEnv1=val1,metricsEnv2=val2,metricEnv3===val3==",
156+
OTEL_EXPORTER_OTLP_METRICS_HEADERS: "metricsEnv1=val1,metricsEnv2=val2,metricEnv3===val3==,User-agent=metrics-user-agent",
157157
OTEL_EXPORTER_OTLP_METRICS_TIMEOUT: "40",
158158
OTEL_PYTHON_EXPORTER_OTLP_METRICS_CREDENTIAL_PROVIDER: "credential_provider",
159159
},
@@ -184,9 +184,18 @@ def f(_):
184184
"metricsenv1": "val1",
185185
"metricsenv2": "val2",
186186
"metricenv3": "==val3==",
187+
"user-agent": "metrics-user-agent",
187188
},
188189
)
189190
self.assertIsInstance(exporter._session, Session)
191+
self.assertEqual(
192+
exporter._session.headers.get("User-Agent"),
193+
"metrics-user-agent",
194+
)
195+
self.assertEqual(
196+
exporter._session.headers.get("Content-Type"),
197+
"application/x-protobuf",
198+
)
190199

191200
@patch.dict(
192201
"os.environ",
@@ -249,7 +258,12 @@ def test_exporter_env(self):
249258
self.assertEqual(exporter._timeout, int(OS_ENV_TIMEOUT))
250259
self.assertIs(exporter._compression, Compression.Gzip)
251260
self.assertEqual(
252-
exporter._headers, {"envheader1": "val1", "envheader2": "val2"}
261+
exporter._headers,
262+
{
263+
"envheader1": "val1",
264+
"envheader2": "val2",
265+
"user-agent": "Overridden",
266+
},
253267
)
254268

255269
@patch.dict(

exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_log_exporter.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
ENV_CERTIFICATE = "/etc/base.crt"
7474
ENV_CLIENT_CERTIFICATE = "/etc/client-cert.pem"
7575
ENV_CLIENT_KEY = "/etc/client-key.pem"
76-
ENV_HEADERS = "envHeader1=val1,envHeader2=val2"
76+
ENV_HEADERS = "envHeader1=val1,envHeader2=val2,User-agent=Overridden"
7777
ENV_TIMEOUT = "30"
7878

7979

@@ -116,7 +116,7 @@ def test_constructor_default(self):
116116
OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY: "logs/client-key.pem",
117117
OTEL_EXPORTER_OTLP_LOGS_COMPRESSION: Compression.Deflate.value,
118118
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: "https://logs.endpoint.env",
119-
OTEL_EXPORTER_OTLP_LOGS_HEADERS: "logsEnv1=val1,logsEnv2=val2,logsEnv3===val3==",
119+
OTEL_EXPORTER_OTLP_LOGS_HEADERS: "logsEnv1=val1,logsEnv2=val2,logsEnv3===val3==,User-agent=LogsUserAgent",
120120
OTEL_EXPORTER_OTLP_LOGS_TIMEOUT: "40",
121121
OTEL_PYTHON_EXPORTER_OTLP_LOGS_CREDENTIAL_PROVIDER: "credential_provider",
122122
},
@@ -147,10 +147,19 @@ def f(_):
147147
"logsenv1": "val1",
148148
"logsenv2": "val2",
149149
"logsenv3": "==val3==",
150+
"user-agent": "LogsUserAgent",
150151
},
151152
)
152153
self.assertIs(exporter._session, credential)
153154
self.assertIsInstance(exporter._session, requests.Session)
155+
self.assertEqual(
156+
exporter._session.headers.get("User-Agent"),
157+
"LogsUserAgent",
158+
)
159+
self.assertEqual(
160+
exporter._session.headers.get("Content-Type"),
161+
"application/x-protobuf",
162+
)
154163

155164
@patch.dict(
156165
"os.environ",
@@ -225,7 +234,12 @@ def test_exporter_env(self):
225234
self.assertEqual(exporter._timeout, int(ENV_TIMEOUT))
226235
self.assertIs(exporter._compression, Compression.Gzip)
227236
self.assertEqual(
228-
exporter._headers, {"envheader1": "val1", "envheader2": "val2"}
237+
exporter._headers,
238+
{
239+
"envheader1": "val1",
240+
"envheader2": "val2",
241+
"user-agent": "Overridden",
242+
},
229243
)
230244
self.assertIsInstance(exporter._session, requests.Session)
231245

exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_span_exporter.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
OS_ENV_CERTIFICATE = "os/env/base.crt"
5757
OS_ENV_CLIENT_CERTIFICATE = "os/env/client-cert.pem"
5858
OS_ENV_CLIENT_KEY = "os/env/client-key.pem"
59-
OS_ENV_HEADERS = "envHeader1=val1,envHeader2=val2"
59+
OS_ENV_HEADERS = "envHeader1=val1,envHeader2=val2,User-agent=Overridden"
6060
OS_ENV_TIMEOUT = "30"
6161
BASIC_SPAN = _Span(
6262
"abc",
@@ -110,7 +110,7 @@ def test_constructor_default(self):
110110
OTEL_EXPORTER_OTLP_TRACES_CLIENT_KEY: "traces/client-key.pem",
111111
OTEL_EXPORTER_OTLP_TRACES_COMPRESSION: Compression.Deflate.value,
112112
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://traces.endpoint.env",
113-
OTEL_EXPORTER_OTLP_TRACES_HEADERS: "tracesEnv1=val1,tracesEnv2=val2,traceEnv3===val3==",
113+
OTEL_EXPORTER_OTLP_TRACES_HEADERS: "tracesEnv1=val1,tracesEnv2=val2,traceEnv3===val3==,User-agent=TraceUserAgent",
114114
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT: "40",
115115
OTEL_PYTHON_EXPORTER_OTLP_TRACES_CREDENTIAL_PROVIDER: "credential_provider",
116116
},
@@ -141,10 +141,19 @@ def f(_):
141141
"tracesenv1": "val1",
142142
"tracesenv2": "val2",
143143
"traceenv3": "==val3==",
144+
"user-agent": "TraceUserAgent",
144145
},
145146
)
146147
self.assertIs(exporter._session, credential)
147148
self.assertIsInstance(exporter._session, requests.Session)
149+
self.assertEqual(
150+
exporter._session.headers.get("Content-Type"),
151+
"application/x-protobuf",
152+
)
153+
self.assertEqual(
154+
exporter._session.headers.get("User-Agent"),
155+
"TraceUserAgent",
156+
)
148157

149158
@patch.dict(
150159
"os.environ",
@@ -207,7 +216,12 @@ def test_exporter_env(self):
207216
self.assertEqual(exporter._timeout, int(OS_ENV_TIMEOUT))
208217
self.assertIs(exporter._compression, Compression.Gzip)
209218
self.assertEqual(
210-
exporter._headers, {"envheader1": "val1", "envheader2": "val2"}
219+
exporter._headers,
220+
{
221+
"envheader1": "val1",
222+
"envheader2": "val2",
223+
"user-agent": "Overridden",
224+
},
211225
)
212226

213227
@patch.dict(

0 commit comments

Comments
 (0)