Skip to content

Commit

Permalink
add meter provider configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Boten <aboten@lightstep.com>
  • Loading branch information
Alex Boten committed Feb 21, 2023
1 parent e123d14 commit bacb1d4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
18 changes: 10 additions & 8 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ sdk:
#
# Environment variable: OTEL_EXPORTER_JAEGER_AGENT_HOST
agent_port: 6832
console:
# List of span processors. Each span processor has a type and args used to configure it.
span_processors:
# Add a batch span processor.
Expand Down Expand Up @@ -143,13 +144,13 @@ sdk:
# Environment variable: OTEL_TRACES_EXPORTER
exporter: console
# Add a batch span processor configured with zipkin exporter. For full description of options see sdk.tracer_provider.span_processors[0].
- type: batch
args:
exporter: zipkin
# Add a batch span processor configured with jaeger exporter. For full description of options see sdk.tracer_provider.span_processors[0].
- type: batch
args:
exporter: jaeger
# - type: batch
# args:
# exporter: zipkin
# # Add a batch span processor configured with jaeger exporter. For full description of options see sdk.tracer_provider.span_processors[0].
# - type: batch
# args:
# exporter: jaeger
# Configure the span limits. See also sdk.attribute_limits.
span_limits:
# Set the max span attribute value size. Overrides sdk.attribute_limits.attribute_value_length_limit.
Expand Down Expand Up @@ -275,6 +276,7 @@ sdk:
#
# Environment variable: OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION
default_histogram_aggregation: exponential_bucket_histogram
console:
# List of metric readers. Each metric reader has a type and args used to configure it.
metric_readers:
# Add a periodic metric reader.
Expand All @@ -293,7 +295,7 @@ sdk:
# Sets the exporter. Exporter must refer to a key in sdk.meter_provider.exporters.
#
# Environment variable: OTEL_METRICS_EXPORTER
exporter: otlp
exporter: console
# Add a prometheus metric reader. Some languages SDKs may implement this as a metric exporter.
#
# Environment variable: OTEL_METRICS_EXPORTER=prometheus
Expand Down
5 changes: 1 addition & 4 deletions json_schema/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,12 @@
},
"LoggerProviderExporters": {
"type": "object",
"additionalProperties": false,
"additionalProperties": true,
"properties": {
"otlp": {
"$ref": "#/definitions/Otlp"
}
},
"required": [
"otlp"
],
"title": "LoggerProviderExporters"
},
"Otlp": {
Expand Down
38 changes: 29 additions & 9 deletions prototypes/python/otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from jsonschema import validate, validators
from jsonschema.exceptions import ValidationError

from opentelemetry.metrics import set_meter_provider
from opentelemetry.trace import set_tracer_provider
from opentelemetry.sdk.trace.export import SpanExporter
from opentelemetry.sdk.metrics.export import MetricExporter


# borromed from opentelemetry/sdk/_configuration
Expand Down Expand Up @@ -44,35 +46,53 @@ def _get_exporter(self, signal: str, name: str):
raise Exception(f"exporter {name} not specified for {signal} signal")

exporter = _import_config_component(name, f"opentelemetry_{signal}_exporter")
if issubclass(exporter, SpanExporter):
return exporter(**self._config.get("sdk").get(signal).get("exporters").get(name))
if signal == "metrics":
cls_type = MetricExporter
elif signal == "traces":
cls_type = SpanExporter
elif signal == "logs":
cls_type = SpanExporter
if issubclass(exporter, cls_type):
if self._config.get("sdk").get(signal).get("exporters").get(name) is not None:
return exporter(**self._config.get("sdk").get(signal).get("exporters").get(name))
return exporter()
raise RuntimeError(f"{name} is not a {signal} exporter")

def set_tracer_provider(self):
def configure_tracing(self, cfg):
if cfg is None:
return
from opentelemetry.sdk.trace import TracerProvider
provider = TracerProvider(resource=self._resource())
from opentelemetry.sdk.trace.export import BatchSpanProcessor

for processor in self._config.get("sdk").get("traces").get("span_processors"):
for processor in cfg.get("span_processors"):
logging.debug("adding span processor %s", processor)
try:
# TODO: pass in exporter arguments
processor = BatchSpanProcessor(self._get_exporter("traces", processor.get("args").get("exporter")))
provider.add_span_processor(processor)
except ModuleNotFoundError as exc:
logging.error("module not found", exc)
set_tracer_provider(provider)

def set_meter_provider(self):
pass
def configure_metrics(self, cfg):
if cfg is None:
return
from opentelemetry.sdk.metrics import MeterProvider
readers = []
for reader in cfg.get("metric_readers"):
if reader.get("type") == "periodic":
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
readers.append(PeriodicExportingMetricReader(self._get_exporter("metrics", reader.get("args").get("exporter"))))
provider = MeterProvider(resource=self._resource(), metric_readers=readers)
set_meter_provider(provider)

def apply(self):
logging.debug("applying configuration %s", self._config)
if self._config is None or self._config.get("sdk").get("disabled"):
logging.debug("sdk disabled, nothing to apply")
return
self.set_tracer_provider()
self.set_meter_provider()
self.configure_tracing(self._config.get("sdk").get("traces"))
self.configure_metrics(self._config.get("sdk").get("metrics"))


NoOpConfig = Config()
Expand Down
12 changes: 10 additions & 2 deletions prototypes/python/prototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys

from opentelemetry.trace import get_tracer
from opentelemetry.metrics import get_meter

import otel

Expand All @@ -13,10 +14,17 @@ def main():
otel.configure(otel.parse_and_validate_from_config_file(sys.argv[1], sys.argv[2]))

tracer = get_tracer("config-prototype")
meter = get_meter("config-prototype")

counter = meter.create_counter("work", unit="1")

with tracer.start_as_current_span("operation-a"):
with tracer.start_as_current_span("operation-b"):
with tracer.start_as_current_span("operation-c"):
logging.debug("you should see traces after this line")
logging.debug("you should see telemetry after this line")
counter.add(1)



main()
if __name__ == "__main__":
main()

0 comments on commit bacb1d4

Please sign in to comment.