-
Notifications
You must be signed in to change notification settings - Fork 16
Add OpAMP agent #320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add OpAMP agent #320
Changes from all commits
Commits
Show all changes
62 commits
Select commit
Hold shift + click to select a range
b581880
Skeleton for opamp implementation
xrmx 009c12b
Add protobuf messages and tooling
xrmx 475a829
Backport uuidv7 implementation from Python 3.14
xrmx 7f82545
Empty requests transport implementation for opamp
xrmx 6f902e4
WIP more client work
xrmx e113373
Wip requests transports
xrmx d205904
WIP transport
xrmx aee0fd4
WIP manual patch of generated proto :(
xrmx e8910ee
WIP messages
xrmx 11e4de9
Use uuid-utils for uuidv7
xrmx 199f681
WIP something more useful
xrmx 1560b39
Hook opamp agent into distro
xrmx 9d61290
Fix agent attributes
xrmx 50b9a67
More fixes
xrmx ab5987f
Handle both text/json and application/json
xrmx a902315
Add import patching of generated protobuf files to the script
xrmx 4e26404
Add jitter in exponential backoff to scheduler
xrmx d094956
Handle logging level config option
xrmx 5fec639
Move opamp handler to config module
xrmx de4f7e3
Add pyright and fix typing
xrmx fd8d1f3
Move opamp client to a path that may resemble upstream
xrmx 1b0b740
Register atexit callback in the OpAMPAgent start method
xrmx 36494da
Make agent identifying attributes mandatory
xrmx 7d818cb
Encode also numbers and bools, cleanup AnyType aliases to match upstream
xrmx 63f2063
Add missing type annotations
xrmx 688a04d
Use absolute import
xrmx b4b94b7
Cleanup agent interface
xrmx bae5d61
Fix case of OpAMP in user agent
xrmx 5b6c092
Register atexit right after starting threads
xrmx ecb49c5
Move endpoint env var to the distro side and drop interval one
xrmx e0cefd0
Make Job class private
xrmx 4580827
Stop reporting ReportsRemoteConfig agent capability
xrmx a7c166c
Get and forward client headers in the agent
xrmx 8eacd65
Send AgentDisconnect message to server on exit
xrmx 4f158ba
Make the agent take a client instance
xrmx c70671f
Rename agent handler to message_handler
xrmx 34be645
refresh dev-requirements
xrmx 9f2a5f5
First batch of opamp agent tests
xrmx e709597
Fix wrong indentation
xrmx 53a88a7
Move computation inside _Job helpers so it's easier to test
xrmx 5c54d70
Add typecheck gh workflow job
xrmx b4ee338
More agent tests
xrmx ab0520f
More return types in client
xrmx eb7bf3a
Lower http client timeout to 1 second
xrmx db3c41e
Use heartbeat consistently
xrmx 6b3accb
Add opamp client tests
xrmx c259999
Add requests transport tests
xrmx 071c082
Add e2e tests with registered responses
xrmx d84ef1b
Rename e2e and add tests with server not responding
xrmx 6f6495b
Use an opamp endpoint that would not work locally
xrmx a09f77e
Make vcrpy 3.9+
xrmx 21833a5
Run tests with vcrpy only in 3.10+
xrmx 976d286
Add missing license headers
xrmx 762c9c6
Improve header license script
xrmx 4ce8717
Fix CI
xrmx de11f1b
Add distro config and opamp_handler tests and rework resource detection
xrmx 97ebc27
Automatically add /v1/opamp to opamp endpoint if path not provided
xrmx 0a72044
Move shebang at top
xrmx 6ac1120
Don't send deployment.environment.name if it is not set
xrmx f36e5c3
Always serialize capabilities
xrmx 25721c6
Pass the client to the opamp message handler
xrmx bd5dca2
Fix typecheck
xrmx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Use caution when bumping this version to ensure compatibility with the currently supported protobuf version. | ||
| # Pinning this to the oldest grpcio version that supports protobuf 5 helps avoid RuntimeWarning messages | ||
| # from the generated protobuf code and ensures continued stability for newer grpcio versions. | ||
| grpcio-tools==1.63.2 | ||
| mypy-protobuf~=3.5.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #!/bin/bash | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # Regenerate python code from opamp protos in | ||
| # https://github.com/open-telemetry/opamp-spec | ||
| # | ||
| # To use, update OPAMP_SPEC_REPO_BRANCH_OR_COMMIT variable below to a commit hash or | ||
| # tag in opentelemtry-proto 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. | ||
| # | ||
| # Optional envars: | ||
| # OPAMP_SPEC_REPO_DIR - the path to an existing checkout of the opamp-spec repo | ||
|
|
||
| # Pinned commit/branch/tag for the current version used in the opamp python package. | ||
| OPAMP_SPEC_REPO_BRANCH_OR_COMMIT="v0.12.0" | ||
|
|
||
| set -e | ||
|
|
||
| OPAMP_SPEC_REPO_DIR=${OPAMP_SPEC_REPO_DIR:-"/tmp/opamp-spec"} | ||
| # root of opentelemetry-python repo | ||
| repo_root="$(git rev-parse --show-toplevel)" | ||
| venv_dir="/tmp/opamp_proto_codegen_venv" | ||
| proto_output_dir="$repo_root/src/opentelemetry/_opamp/proto" | ||
|
|
||
| # run on exit even if crash | ||
| cleanup() { | ||
| echo "Deleting $venv_dir" | ||
| rm -rf $venv_dir | ||
| } | ||
| trap cleanup EXIT | ||
|
|
||
| echo "Creating temporary virtualenv at $venv_dir using $(python3 --version)" | ||
| python3 -m venv $venv_dir | ||
| source $venv_dir/bin/activate | ||
| python -m pip install \ | ||
| -c $repo_root/opamp-gen-requirements.txt \ | ||
| grpcio-tools mypy-protobuf | ||
| echo 'python -m grpc_tools.protoc --version' | ||
| python -m grpc_tools.protoc --version | ||
|
|
||
| # Clone the proto repo if it doesn't exist | ||
| if [ ! -d "$OPAMP_SPEC_REPO_DIR" ]; then | ||
| git clone https://github.com/open-telemetry/opamp-spec.git $OPAMP_SPEC_REPO_DIR | ||
| fi | ||
|
|
||
| # Pull in changes and switch to requested branch | ||
| ( | ||
| cd $OPAMP_SPEC_REPO_DIR | ||
| git fetch --all | ||
| git checkout $OPAMP_SPEC_REPO_BRANCH_OR_COMMIT | ||
| # pull if OPAMP_SPEC_BRANCH_OR_COMMIT is not a detached head | ||
| git symbolic-ref -q HEAD && git pull --ff-only || true | ||
| ) | ||
|
|
||
| cd $proto_output_dir | ||
|
|
||
| # clean up old generated code | ||
| find . -regex ".*_pb2.*\.pyi?" -exec rm {} + | ||
|
|
||
| # generate proto code for all protos | ||
| all_protos=$(find $OPAMP_SPEC_REPO_DIR/ -name "*.proto") | ||
| python -m grpc_tools.protoc \ | ||
| -I $OPAMP_SPEC_REPO_DIR/proto \ | ||
| --python_out=. \ | ||
| --mypy_out=. \ | ||
| $all_protos | ||
|
|
||
| sed -i -e 's/import anyvalue_pb2 as anyvalue__pb2/from . import anyvalue_pb2 as anyvalue__pb2/' opamp_pb2.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| # or more contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright | ||
| # ownership. Elasticsearch B.V. licenses this file to you under | ||
| # the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import logging | ||
|
|
||
| from opentelemetry._opamp import messages | ||
| from opentelemetry._opamp.client import OpAMPClient | ||
| from opentelemetry._opamp.proto import opamp_pb2 as opamp_pb2 | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| _LOG_LEVELS_MAP = { | ||
| "trace": 5, | ||
| "debug": logging.DEBUG, | ||
| "info": logging.INFO, | ||
| "warn": logging.WARNING, | ||
| "error": logging.ERROR, | ||
| "fatal": logging.CRITICAL, | ||
| "off": 1000, | ||
| } | ||
|
|
||
|
|
||
| def opamp_handler(client: OpAMPClient, message: opamp_pb2.ServerToAgent): | ||
| if not message.remote_config: | ||
| return | ||
|
|
||
| for config_filename, config in messages._decode_remote_config(message.remote_config): | ||
| # we don't have standardized config values so limit to configs coming from our backend | ||
| if config_filename == "elastic": | ||
| logger.debug("Config %s: %s", config_filename, config) | ||
| # when config option has default value you don't get it so need to handle the default | ||
| config_logging_level = config.get("logging_level") | ||
| if config_logging_level is not None: | ||
| logging_level = _LOG_LEVELS_MAP.get(config_logging_level) # type: ignore[reportArgumentType] | ||
|
trentm marked this conversation as resolved.
|
||
| else: | ||
| logging_level = logging.INFO | ||
|
|
||
| if logging_level is None: | ||
| logger.warning("Logging level not handled: %s", config_logging_level) | ||
| else: | ||
| # update upstream and distro logging levels | ||
| logging.getLogger("opentelemetry").setLevel(logging_level) | ||
| logging.getLogger("elasticotel").setLevel(logging_level) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # opamp | ||
|
|
||
| opamp is an OpAMP protocol implementation. | ||
|
|
||
| Implementation tries to be agnostic to the transport libraries and protocols used but since it's only HTTP for now that | ||
| may be achieved once more transport implementation appears. | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.