|
| 1 | +# Copyright The 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 | +OpenTelemetry Python - OpAMP client |
| 17 | +----------------------------------- |
| 18 | +
|
| 19 | +This package provides a bunch of classes that can be used by OpenTelemetry distributions implementors |
| 20 | +to implement remote config support via the OpAMP protocol. |
| 21 | +
|
| 22 | +The client implements the following capabilities: |
| 23 | +
|
| 24 | +* ReportsStatus |
| 25 | +* ReportsHeartbeat |
| 26 | +* AcceptsRemoteConfig |
| 27 | +* ReportsRemoteConfig |
| 28 | +
|
| 29 | +These capabilities are enough to get a remote config from an opamp server, parse it, apply it and ack it. |
| 30 | +
|
| 31 | +Since OpAMP APIs, config options or environment variables are not standardizes the distros are required |
| 32 | +to provide code doing so. |
| 33 | +OTel Python distros would need to provide their own message handler callback that implements the actual |
| 34 | +change of whatever configuration their backends sends. |
| 35 | +
|
| 36 | +Please note that the API is not finalized yet and so the name is called ``_opamp`` with the underscore. |
| 37 | +
|
| 38 | +Usage |
| 39 | +----- |
| 40 | +
|
| 41 | +.. code-block:: python |
| 42 | +
|
| 43 | + import os |
| 44 | +
|
| 45 | + from opentelemetry._opamp import messages |
| 46 | + from opentelemetry._opamp.agent import OpAMPAgent |
| 47 | + from opentelemetry._opamp.client import OpAMPClient |
| 48 | + from opentelemetry._opamp.proto import opamp_pb2 as opamp_pb2 |
| 49 | + from opentelemetry.sdk._configuration import _OTelSDKConfigurator |
| 50 | + from opentelemetry.sdk.resources import OTELResourceDetector |
| 51 | +
|
| 52 | +
|
| 53 | + def opamp_handler(agent: OpAMPAgent, client: OpAMPClient, message: opamp_pb2.ServerToAgent): |
| 54 | + for config_filename, config in messages._decode_remote_config(message.remote_config): |
| 55 | + print("do something") |
| 56 | +
|
| 57 | +
|
| 58 | + class MyOpenTelemetryConfigurator(_OTelSDKConfigurator): |
| 59 | + def _configure(self, **kwargs): |
| 60 | + super()._configure(**kwargs) |
| 61 | +
|
| 62 | + enable_opamp = False |
| 63 | + endpoint = os.environ.get("OTEL_PYTHON_OPAMP_ENDPOINT") |
| 64 | + if endpoint: |
| 65 | + # this is not great but we don't have the calculated resource attributes around |
| 66 | + # see https://github.com/open-telemetry/opentelemetry-python/pull/4646 for creating |
| 67 | + # an entry point distros can implement |
| 68 | + resource = OTELResourceDetector().detect() |
| 69 | + agent_identifying_attributes = { |
| 70 | + "service.name": resource.attributes.get("service.name"), |
| 71 | + } |
| 72 | + opamp_client = OpAMPClient( |
| 73 | + endpoint=endpoint, |
| 74 | + agent_identifying_attributes=agent_identifying_attributes, |
| 75 | + ) |
| 76 | + opamp_agent = OpAMPAgent( |
| 77 | + interval=30, |
| 78 | + message_handler=opamp_handler, |
| 79 | + client=opamp_client, |
| 80 | + ) |
| 81 | + opamp_agent.start() |
| 82 | +
|
| 83 | +API |
| 84 | +--- |
| 85 | +""" |
| 86 | + |
| 87 | +from opentelemetry._opamp.agent import OpAMPAgent |
| 88 | +from opentelemetry._opamp.client import OpAMPClient |
| 89 | + |
| 90 | +__all__ = ["OpAMPAgent", "OpAMPClient"] |
0 commit comments