Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d94509

Browse files
authored
Merge branch 'master' into update-context-api
2 parents 674658a + ed25287 commit 9d94509

File tree

83 files changed

+1309
-361
lines changed

Some content is hidden

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

83 files changed

+1309
-361
lines changed

README.md

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ pip install -e ./ext/opentelemetry-ext-{integration}
5151

5252
```python
5353
from opentelemetry import trace
54-
from opentelemetry.sdk.trace import TracerSource
54+
from opentelemetry.sdk.trace import TracerProvider
5555
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
5656
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
5757

58-
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
59-
trace.tracer_source().add_span_processor(
58+
trace.set_preferred_tracer_provider_implementation(lambda T: TracerProvider())
59+
trace.tracer_provider().add_span_processor(
6060
SimpleExportSpanProcessor(ConsoleSpanExporter())
6161
)
62-
tracer = trace.tracer_source().get_tracer(__name__)
62+
tracer = trace.get_tracer(__name__)
6363
with tracer.start_as_current_span('foo'):
6464
with tracer.start_as_current_span('bar'):
6565
with tracer.start_as_current_span('baz'):
@@ -171,20 +171,46 @@ includes:
171171
- Flask Integration
172172
- PyMongo Integration
173173

174+
The v0.4 alpha release includes:
175+
176+
- Metrics MinMaxSumCount Aggregator
177+
- Context API
178+
- Full Metrics SDK Pipeline
179+
- Metrics STDOUT Exporter
180+
- Dbapi2 Integration
181+
- MySQL Integration
182+
- Psycopg2 Integration
183+
- Zipkin Exporter
184+
- Prometheus Metrics Exporter
185+
- New Examples and Improvements to Existing Examples
186+
187+
Thank you to the following individuals for contributing to this release:
188+
189+
* Alex Boten
190+
* Chris Kleinknecht
191+
* Christian Neumüller
192+
* Daniel González
193+
* Diego Hurtado
194+
* Golovin Pavel
195+
* Hector Hernandez
196+
* Jake Malachowski
197+
* Joshua H Lang
198+
* Leighton Chen
199+
* Mauricio Vásquez
200+
* Yusuke Tsutsumi
201+
174202
See the [project
175203
milestones](https://github.com/open-telemetry/opentelemetry-python/milestones)
176204
for details on upcoming releases. The dates and features described here are
177205
estimates, and subject to change.
178206

179207
Future releases targets include:
180208

181-
| Component | Version | Target Date |
182-
| ----------------------------------- | ---------- | ----------------- |
183-
| Zipkin Trace Exporter | Alpha v0.4 | February 21 2020 |
184-
| W3C Correlation Context Propagation | Alpha v0.4 | February 21 2020 |
185-
| Support for Tags/Baggage | Alpha v0.4 | February 21 2020 |
186-
| Metrics Aggregation | Alpha v0.4 | February 21 2020 |
187-
| gRPC Integrations | Alpha v0.4 | February 21 2020 |
188-
| Prometheus Metrics Exporter | Alpha v0.4 | February 21 2020 |
189-
| OpenCensus Bridge | Alpha v0.4 | February 21 2020 |
190-
| Metrics SDK (Complete) | Alpha v0.4 | February 21 2020 |
209+
| Component | Version | Target Date |
210+
| ----------------------------------- | ---------- | ------------ |
211+
| W3C Correlation Context Propagation | Beta v1 | March 16 2020|
212+
| Support for Tags/Baggage | Beta v1 | March 16 2020|
213+
| gRPC Integrations | Beta v1 | March 16 2020|
214+
| OpenTelemetry Collector Exporter | Beta v1 | March 16 2020|
215+
| OpenCensus Bridge | Beta v1 | March 16 2020|
216+
| Metrics SDK (Complete) | Beta v1 | March 16 2020|

examples/basic_tracer/tracer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import os
1818

1919
from opentelemetry import trace
20-
from opentelemetry.sdk.trace import TracerSource
20+
from opentelemetry.sdk.trace import TracerProvider
2121
from opentelemetry.sdk.trace.export import (
2222
BatchExportSpanProcessor,
2323
ConsoleSpanExporter,
@@ -36,17 +36,17 @@
3636

3737
# The preferred tracer implementation must be set, as the opentelemetry-api
3838
# defines the interface with a no-op implementation.
39-
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
39+
trace.set_preferred_tracer_provider_implementation(lambda T: TracerProvider())
4040

4141
# We tell OpenTelemetry who it is that is creating spans. In this case, we have
4242
# no real name (no setup.py), so we make one up. If we had a version, we would
4343
# also specify it here.
44-
tracer = trace.tracer_source().get_tracer(__name__)
44+
tracer = trace.get_tracer(__name__)
4545

4646
# SpanExporter receives the spans and send them to the target location.
4747
span_processor = BatchExportSpanProcessor(exporter)
4848

49-
trace.tracer_source().add_span_processor(span_processor)
49+
trace.tracer_provider().add_span_processor(span_processor)
5050
with tracer.start_as_current_span("foo"):
5151
with tracer.start_as_current_span("bar"):
5252
with tracer.start_as_current_span("baz"):

examples/http/server.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from opentelemetry import trace
2323
from opentelemetry.ext import http_requests
2424
from opentelemetry.ext.wsgi import OpenTelemetryMiddleware
25-
from opentelemetry.sdk.trace import TracerSource
25+
from opentelemetry.sdk.trace import TracerProvider
2626
from opentelemetry.sdk.trace.export import (
2727
BatchExportSpanProcessor,
2828
ConsoleSpanExporter,
@@ -41,17 +41,17 @@
4141

4242
# The preferred tracer implementation must be set, as the opentelemetry-api
4343
# defines the interface with a no-op implementation.
44-
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
45-
tracer = trace.tracer_source().get_tracer(__name__)
44+
trace.set_preferred_tracer_provider_implementation(lambda T: TracerProvider())
45+
tracer = trace.get_tracer(__name__)
4646

4747
# SpanExporter receives the spans and send them to the target location.
4848
span_processor = BatchExportSpanProcessor(exporter)
49-
trace.tracer_source().add_span_processor(span_processor)
49+
trace.tracer_provider().add_span_processor(span_processor)
5050

5151
# Integrations are the glue that binds the OpenTelemetry API and the
5252
# frameworks and libraries that are used together, automatically creating
5353
# Spans and propagating context as appropriate.
54-
http_requests.enable(trace.tracer_source())
54+
http_requests.enable(trace.tracer_provider())
5555
app = flask.Flask(__name__)
5656
app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app)
5757

examples/http/tracer_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from opentelemetry import trace
2222
from opentelemetry.ext import http_requests
23-
from opentelemetry.sdk.trace import TracerSource
23+
from opentelemetry.sdk.trace import TracerProvider
2424
from opentelemetry.sdk.trace.export import (
2525
BatchExportSpanProcessor,
2626
ConsoleSpanExporter,
@@ -39,15 +39,15 @@
3939

4040
# The preferred tracer implementation must be set, as the opentelemetry-api
4141
# defines the interface with a no-op implementation.
42-
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
43-
tracer_source = trace.tracer_source()
42+
trace.set_preferred_tracer_provider_implementation(lambda T: TracerProvider())
43+
tracer_provider = trace.tracer_provider()
4444

4545
# SpanExporter receives the spans and send them to the target location.
4646
span_processor = BatchExportSpanProcessor(exporter)
47-
tracer_source.add_span_processor(span_processor)
47+
tracer_provider.add_span_processor(span_processor)
4848

4949
# Integrations are the glue that binds the OpenTelemetry API and the
5050
# frameworks and libraries that are used together, automatically creating
5151
# Spans and propagating context as appropriate.
52-
http_requests.enable(tracer_source)
52+
http_requests.enable(tracer_provider)
5353
response = requests.get(url="http://127.0.0.1:5000/")
Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019, OpenTelemetry Authors
1+
# Copyright 2020, OpenTelemetry Authors
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -16,29 +16,34 @@
1616
This module serves as an example for a simple application using metrics
1717
Examples show how to recording affects the collection of metrics to be exported
1818
"""
19-
import time
19+
20+
from prometheus_client import start_http_server
2021

2122
from opentelemetry import metrics
23+
from opentelemetry.ext.prometheus import PrometheusMetricsExporter
2224
from opentelemetry.sdk.metrics import Counter, Meter
23-
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
24-
from opentelemetry.sdk.metrics.export.batcher import UngroupedBatcher
2525
from opentelemetry.sdk.metrics.export.controller import PushController
2626

27-
# Batcher used to collect all created metrics from meter ready for exporting
28-
# Pass in false for non-stateful batcher. Indicates the batcher computes
29-
# checkpoints which describe the updates of a single collection period (deltas)
30-
batcher = UngroupedBatcher(False)
27+
# Start Prometheus client
28+
start_http_server(port=8000, addr="localhost")
29+
3130
# Meter is responsible for creating and recording metrics
32-
metrics.set_preferred_meter_implementation(lambda _: Meter(batcher))
31+
metrics.set_preferred_meter_implementation(lambda _: Meter())
3332
meter = metrics.meter()
34-
# exporter to export metrics to the console
35-
exporter = ConsoleMetricsExporter()
33+
# exporter to export metrics to Prometheus
34+
prefix = "MyAppPrefix"
35+
exporter = PrometheusMetricsExporter(prefix)
3636
# controller collects metrics created from meter and exports it via the
3737
# exporter every interval
3838
controller = PushController(meter, exporter, 5)
3939

4040
counter = meter.create_metric(
41-
"requests", "number of requests", 1, int, Counter, ("environment",)
41+
"requests",
42+
"number of requests",
43+
"requests",
44+
int,
45+
Counter,
46+
("environment",),
4247
)
4348

4449
# Labelsets are used to identify key-values that are associated with a specific
@@ -47,11 +52,4 @@
4752
label_set = meter.get_label_set({"environment": "staging"})
4853

4954
counter.add(25, label_set)
50-
# We sleep for 5 seconds, exported value should be 25
51-
time.sleep(5)
52-
53-
counter.add(50, label_set)
54-
# exported value should be 50 due to non-stateful batcher
55-
time.sleep(20)
56-
57-
# Following exported values would be 0
55+
input("Press any key to exit...")

examples/metrics/simple_example.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Copyright 2019, 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+
"""
16+
This module serves as an example for a simple application using metrics
17+
It shows:
18+
- How to configure a meter passing a sateful or stateless.
19+
- How to configure an exporter and how to create a controller.
20+
- How to create some metrics intruments and how to capture data with them.
21+
"""
22+
import sys
23+
import time
24+
25+
from opentelemetry import metrics
26+
from opentelemetry.sdk.metrics import Counter, Measure, Meter
27+
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
28+
from opentelemetry.sdk.metrics.export.batcher import UngroupedBatcher
29+
from opentelemetry.sdk.metrics.export.controller import PushController
30+
31+
batcher_mode = "stateful"
32+
33+
34+
def usage(argv):
35+
print("usage:")
36+
print("{} [mode]".format(argv[0]))
37+
print("mode: stateful (default) or stateless")
38+
39+
40+
if len(sys.argv) >= 2:
41+
batcher_mode = sys.argv[1]
42+
if batcher_mode not in ("stateful", "stateless"):
43+
print("bad mode specified.")
44+
usage(sys.argv)
45+
sys.exit(1)
46+
47+
# Batcher used to collect all created metrics from meter ready for exporting
48+
# Pass in True/False to indicate whether the batcher is stateful.
49+
# True indicates the batcher computes checkpoints from over the process
50+
# lifetime.
51+
# False indicates the batcher computes checkpoints which describe the updates
52+
# of a single collection period (deltas)
53+
batcher = UngroupedBatcher(batcher_mode == "stateful")
54+
55+
# If a batcher is not provided, a default batcher is used
56+
# Meter is responsible for creating and recording metrics
57+
metrics.set_preferred_meter_implementation(lambda _: Meter(batcher))
58+
meter = metrics.meter()
59+
60+
# Exporter to export metrics to the console
61+
exporter = ConsoleMetricsExporter()
62+
63+
# A PushController collects metrics created from meter and exports it via the
64+
# exporter every interval
65+
controller = PushController(meter, exporter, 5)
66+
67+
# Metric instruments allow to capture measurements
68+
requests_counter = meter.create_metric(
69+
"requests", "number of requests", 1, int, Counter, ("environment",)
70+
)
71+
72+
clicks_counter = meter.create_metric(
73+
"clicks", "number of clicks", 1, int, Counter, ("environment",)
74+
)
75+
76+
requests_size = meter.create_metric(
77+
"requests_size", "size of requests", 1, int, Measure, ("environment",)
78+
)
79+
80+
# Labelsets are used to identify key-values that are associated with a specific
81+
# metric that you want to record. These are useful for pre-aggregation and can
82+
# be used to store custom dimensions pertaining to a metric
83+
staging_label_set = meter.get_label_set({"environment": "staging"})
84+
testing_label_set = meter.get_label_set({"environment": "testing"})
85+
86+
# Update the metric instruments using the direct calling convention
87+
requests_size.record(100, staging_label_set)
88+
requests_counter.add(25, staging_label_set)
89+
# Sleep for 5 seconds, exported value should be 25
90+
time.sleep(5)
91+
92+
requests_size.record(5000, staging_label_set)
93+
requests_counter.add(50, staging_label_set)
94+
# Exported value should be 75
95+
time.sleep(5)
96+
97+
requests_size.record(2, testing_label_set)
98+
requests_counter.add(35, testing_label_set)
99+
# There should be two exported values 75 and 35, one for each labelset
100+
time.sleep(5)
101+
102+
clicks_counter.add(5, staging_label_set)
103+
# There should be three exported values, labelsets can be reused for different
104+
# metrics but will be recorded seperately, 75, 35 and 5
105+
106+
time.sleep(5)

0 commit comments

Comments
 (0)