Skip to content

Commit

Permalink
Merge branch 'master' into add-contrib-repo-tests-workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Nov 18, 2020
2 parents a783036 + d556b90 commit 13fe59d
Show file tree
Hide file tree
Showing 18 changed files with 201 additions and 142 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ on:
- 'instrumentation/**'
- 'opentelemetry-python/opentelemetry-api/src/opentelemetry/**'
- 'opentelemetry-python/opentelemetry-sdk/src/opentelemetry/sdk/**'
env:
CONTRIB_REPO_SHA: master

jobs:
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Checkout Contrib Repo @ SHA - ${{ env.CONTRIB_REPO_SHA }}
uses: actions/checkout@v2
with:
repository: open-telemetry/opentelemetry-python-contrib
ref: ${{ env.CONTRIB_REPO_SHA }}
path: opentelemetry-python-contrib
- name: Set up Python py38
uses: actions/setup-python@v2
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import opentelemetry.exporter.opencensus.util as utils
from opentelemetry.sdk.metrics import Counter, Metric
from opentelemetry.sdk.metrics.export import (
MetricRecord,
ExportRecord,
MetricsExporter,
MetricsExportResult,
)
Expand Down Expand Up @@ -79,11 +79,11 @@ def __init__(
self.exporter_start_timestamp.GetCurrentTime()

def export(
self, metric_records: Sequence[MetricRecord]
self, export_records: Sequence[ExportRecord]
) -> MetricsExportResult:
try:
responses = self.client.Export(
self.generate_metrics_requests(metric_records)
self.generate_metrics_requests(export_records)
)

# Read response
Expand All @@ -99,7 +99,7 @@ def shutdown(self) -> None:
pass

def generate_metrics_requests(
self, metrics: Sequence[MetricRecord]
self, metrics: Sequence[ExportRecord]
) -> metrics_service_pb2.ExportMetricsServiceRequest:
collector_metrics = translate_to_collector(
metrics, self.exporter_start_timestamp
Expand All @@ -112,15 +112,15 @@ def generate_metrics_requests(

# pylint: disable=too-many-branches
def translate_to_collector(
metric_records: Sequence[MetricRecord],
export_records: Sequence[ExportRecord],
exporter_start_timestamp: Timestamp,
) -> Sequence[metrics_pb2.Metric]:
collector_metrics = []
for metric_record in metric_records:
for export_record in export_records:

label_values = []
label_keys = []
for label_tuple in metric_record.labels:
for label_tuple in export_record.labels:
label_keys.append(metrics_pb2.LabelKey(key=label_tuple[0]))
label_values.append(
metrics_pb2.LabelValue(
Expand All @@ -130,30 +130,30 @@ def translate_to_collector(
)

metric_descriptor = metrics_pb2.MetricDescriptor(
name=metric_record.instrument.name,
description=metric_record.instrument.description,
unit=metric_record.instrument.unit,
type=get_collector_metric_type(metric_record.instrument),
name=export_record.instrument.name,
description=export_record.instrument.description,
unit=export_record.instrument.unit,
type=get_collector_metric_type(export_record.instrument),
label_keys=label_keys,
)

# If cumulative and stateful, explicitly set the start_timestamp to
# exporter start time.
if metric_record.instrument.meter.processor.stateful:
if export_record.instrument.meter.processor.stateful:
start_timestamp = exporter_start_timestamp
else:
start_timestamp = None

timeseries = metrics_pb2.TimeSeries(
label_values=label_values,
points=[get_collector_point(metric_record)],
points=[get_collector_point(export_record)],
start_timestamp=start_timestamp,
)
collector_metrics.append(
metrics_pb2.Metric(
metric_descriptor=metric_descriptor,
timeseries=[timeseries],
resource=get_resource(metric_record),
resource=get_resource(export_record),
)
)
return collector_metrics
Expand All @@ -169,29 +169,29 @@ def get_collector_metric_type(metric: Metric) -> metrics_pb2.MetricDescriptor:
return metrics_pb2.MetricDescriptor.UNSPECIFIED


def get_collector_point(metric_record: MetricRecord) -> metrics_pb2.Point:
def get_collector_point(export_record: ExportRecord) -> metrics_pb2.Point:
# TODO: horrible hack to get original list of keys to then get the bound
# instrument
point = metrics_pb2.Point(
timestamp=utils.proto_timestamp_from_time_ns(
metric_record.aggregator.last_update_timestamp
export_record.aggregator.last_update_timestamp
)
)
if metric_record.instrument.value_type == int:
point.int64_value = metric_record.aggregator.checkpoint
elif metric_record.instrument.value_type == float:
point.double_value = metric_record.aggregator.checkpoint
if export_record.instrument.value_type == int:
point.int64_value = export_record.aggregator.checkpoint
elif export_record.instrument.value_type == float:
point.double_value = export_record.aggregator.checkpoint
else:
raise TypeError(
"Unsupported metric type: {}".format(
metric_record.instrument.value_type
export_record.instrument.value_type
)
)
return point


def get_resource(metric_record: MetricRecord) -> resource_pb2.Resource:
resource_attributes = metric_record.resource.attributes
def get_resource(export_record: ExportRecord) -> resource_pb2.Resource:
resource_attributes = export_record.resource.attributes
return resource_pb2.Resource(
type=infer_oc_resource_type(resource_attributes),
labels={k: str(v) for k, v in resource_attributes.items()},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
get_dict_as_key,
)
from opentelemetry.sdk.metrics.export import (
MetricRecord,
ExportRecord,
MetricsExportResult,
aggregate,
)
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_get_collector_point(self):
"testName", "testDescription", "unit", float,
)
result = metrics_exporter.get_collector_point(
MetricRecord(
ExportRecord(
int_counter,
self._key_labels,
aggregator,
Expand All @@ -113,7 +113,7 @@ def test_get_collector_point(self):
aggregator.update(123.5)
aggregator.take_checkpoint()
result = metrics_exporter.get_collector_point(
MetricRecord(
ExportRecord(
float_counter,
self._key_labels,
aggregator,
Expand All @@ -124,7 +124,7 @@ def test_get_collector_point(self):
self.assertRaises(
TypeError,
metrics_exporter.get_collector_point(
MetricRecord(
ExportRecord(
valuerecorder,
self._key_labels,
aggregator,
Expand All @@ -144,7 +144,7 @@ def test_export(self):
test_metric = self._meter.create_counter(
"testname", "testdesc", "unit", int, self._labels.keys(),
)
record = MetricRecord(
record = ExportRecord(
test_metric,
self._key_labels,
aggregate.SumAggregator(),
Expand Down Expand Up @@ -173,7 +173,7 @@ def test_translate_to_collector(self):
aggregator = aggregate.SumAggregator()
aggregator.update(123)
aggregator.take_checkpoint()
record = MetricRecord(
record = ExportRecord(
test_metric,
self._key_labels,
aggregator,
Expand Down
Loading

0 comments on commit 13fe59d

Please sign in to comment.