Skip to content

Commit

Permalink
Add vendor script
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jopel committed Nov 22, 2024
1 parent ad49a23 commit c0d6005
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 10 deletions.
54 changes: 54 additions & 0 deletions scripts/vendor_otlp_proto_common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash
#
# Vendor in the python code in
# https://github.com/open-telemetry/opentelemetry-python/tree/main/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common
#
# To use, update REPO_BRANCH_OR_COMMIT variable below to a commit hash or
# tag in opentelemtry-python repo that you want to build off of. Then, just run
# this script to update the proto files. Commit the changes as well as any
# fixes needed in the OTLP exporter.

# Pinned commit/branch/tag for the current version used in opentelemetry-proto python package.
REPO_BRANCH_OR_COMMIT="v1.26.0"

set -e

REPO_DIR=${REPO_DIR:-"/tmp/opentelemetry-python"}
# root of opentelemetry-python repo
repo_root="$(git rev-parse --show-toplevel)"

# Clone the proto repo if it doesn't exist
if [ ! -d "$REPO_DIR" ]; then
git clone https://github.com/open-telemetry/opentelemetry-python.git $REPO_DIR
fi

# Pull in changes and switch to requested branch
(
cd $REPO_DIR
git fetch --all
git checkout $REPO_BRANCH_OR_COMMIT
# pull if REPO_BRANCH_OR_COMMIT is not a detached head
git symbolic-ref -q HEAD && git pull --ff-only || true
)

cd $repo_root/src/snowflake/telemetry/_internal

# Copy the entire file tree from exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/
# to src/snowflake/telemetry/_internal/opentelemetry/
cp -r $REPO_DIR/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter opentelemetry/

# If MacOS need '' after -i
# Detect the OS (macOS or Linux)
if [[ "$OSTYPE" == "darwin"* ]]; then
SED_CMD="sed -i ''"
else
SED_CMD="sed -i"
fi

# Replace all the imports strings in the copied python files
# opentelemetry.exporter to snowflake.telemetry._internal.opentelemetry.exporter
# opentelemetry.proto.*_pb2 to snowflake.telemetry._internal.opentelemetry.proto.*_marshaler

find opentelemetry/exporter -type f -name "*.py" -exec $SED_CMD 's/opentelemetry.exporter/snowflake.telemetry._internal.opentelemetry.exporter/g' {} +
find opentelemetry/exporter -type f -name "*.py" -exec $SED_CMD -i 's/opentelemetry\.proto\(.*\)_pb2/snowflake.telemetry._internal.opentelemetry.proto\1_marshaler/g' {} +

Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

from snowflake.telemetry._internal.opentelemetry.exporter.otlp.proto.common.version import __version__

__all__ = ["__version__"]
__all__ = ["__version__"]
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,15 @@ def _create_exp_backoff_generator(max_value: int = 0) -> Iterator[int]:
from 1 (2^0) and doubles each time (2^1, 2^2, 2^3, ...). If a max_value is specified
and non-zero, the generated values will not exceed this maximum, capping at max_value
instead of growing indefinitely.
Parameters:
- max_value (int, optional): The maximum value to yield. If 0 or not provided, the
sequence grows without bound.
Returns:
Iterator[int]: An iterator that yields the exponential backoff values, either uncapped or
capped at max_value.
Example:
```
gen = _create_exp_backoff_generator(max_value=10)
Expand All @@ -165,8 +168,9 @@ def _create_exp_backoff_generator(max_value: int = 0) -> Iterator[int]:
4
8
10
Note: this functionality used to be handled by the 'backoff' package.
"""
for i in count(0):
out = 2**i
yield min(out, max_value) if max_value else out
yield min(out, max_value) if max_value else out
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ def _encode_resource_logs(batch: Sequence[LogData]) -> List[ResourceLogs]:
)
)

return pb2_resource_logs
return pb2_resource_logs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
ExportMetricsServiceRequest,
)
from snowflake.telemetry._internal.opentelemetry.proto.common.v1.common_marshaler import InstrumentationScope
import snowflake.telemetry._internal.opentelemetry.proto.metrics.v1.metrics_marshaler as pb2
from snowflake.telemetry._internal.opentelemetry.proto.metrics.v1 import metrics_marshaler as pb2
from opentelemetry.sdk.metrics.export import (
MetricsData,
Gauge,
Expand Down Expand Up @@ -335,4 +335,4 @@ def encode_metrics(data: MetricsData) -> ExportMetricsServiceRequest:
)
)
resource_metrics = resource_data
return ExportMetricsServiceRequest(resource_metrics=resource_metrics)
return ExportMetricsServiceRequest(resource_metrics=resource_metrics)
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,4 @@ def _encode_trace_state(trace_state: TraceState) -> Optional[str]:
def _encode_parent_id(context: Optional[SpanContext]) -> Optional[bytes]:
if context:
return _encode_span_id(context.span_id)
return None
return None
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
encode_logs,
)

__all__ = ["encode_logs"]
__all__ = ["encode_logs"]
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
encode_metrics,
)

__all__ = ["encode_metrics"]
__all__ = ["encode_metrics"]
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
encode_spans,
)

__all__ = ["encode_spans"]
__all__ = ["encode_spans"]
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = "1.26.0"
__version__ = "1.26.0"

0 comments on commit c0d6005

Please sign in to comment.