Skip to content
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

Return empty receivers list if tracing backend isn't connected #338

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/grafana_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,15 @@ def _requested_tracing_protocols(self) -> Set[ReceiverProtocol]:
)

def _update_tracing_provider(self):
# If the "upstream" tracing is not ready, we don't want to publish the receivers.
# Otherwise, charms that integrate over `tracing` would start sending traces to an endpoint that isn't open.
requested_tracing_protocols = (
mmkay marked this conversation as resolved.
Show resolved Hide resolved
self._requested_tracing_protocols if self._tracing.is_ready() else []
)
self._tracing_provider.publish_receivers(
tuple(
(protocol, self._get_tracing_receiver_url(protocol))
for protocol in self._requested_tracing_protocols
for protocol in requested_tracing_protocols
)
)

Expand Down Expand Up @@ -880,6 +885,12 @@ def _tracing_receivers(self) -> Dict[str, Union[Any, List[Any]]]:
Returns:
a dict with the receivers config.
"""
if not self._tracing.is_ready():
logger.warning(
"Tracing backend is not connected yet: grafana-agent cannot ingest traces."
)
return {}

receivers_set = self._requested_tracing_protocols

if not receivers_set:
Expand Down
35 changes: 33 additions & 2 deletions tests/scenario/test_tracing_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,37 @@ def base_state():


def test_tracing_relation(ctx, base_state):
# GIVEN a tracing relation over the tracing-provider endpoint
tracing_provider = Relation(
"tracing-provider",
remote_app_data=TracingRequirerAppData(receivers=["otlp_http", "otlp_grpc"]).dump(),
)
tracing = Relation(
"tracing",
remote_app_data=TracingProviderAppData(
receivers=[
Receiver(protocol={"name": "otlp_grpc", "type": "grpc"}, url="http:foo.com:1111")
]
).dump(),
)

state = dataclasses.replace(base_state, relations=[tracing, tracing_provider])
# WHEN we process any setup event for the relation
state_out = ctx.run(ctx.on.relation_changed(tracing_provider), state)

agent = state_out.get_container("agent")

# THEN the agent has started
assert agent.services["agent"].is_running()
# AND the grafana agent config has a traces config section
fs = agent.get_filesystem(ctx)
gagent_config = fs.joinpath(*CONFIG_PATH.strip("/").split("/"))
assert gagent_config.exists()
yml = yaml.safe_load(gagent_config.read_text())
assert yml["traces"]["configs"][0], yml.get("traces", "<no traces config>")


def test_tracing_provider_without_tracing(ctx, base_state):
# GIVEN a tracing relation over the tracing-provider endpoint
tracing = Relation(
"tracing-provider",
Expand All @@ -53,12 +84,12 @@ def test_tracing_relation(ctx, base_state):

# THEN the agent has started
assert agent.services["agent"].is_running()
# AND the grafana agent config has a traces config section
# AND the grafana agent config has an empty traces config section
fs = agent.get_filesystem(ctx)
gagent_config = fs.joinpath(*CONFIG_PATH.strip("/").split("/"))
assert gagent_config.exists()
yml = yaml.safe_load(gagent_config.read_text())
assert yml["traces"]["configs"][0], yml.get("traces", "<no traces config>")
assert yml["traces"] == {}


def test_tracing_relations_in_and_out(ctx, base_state):
Expand Down
Loading