Skip to content

Commit

Permalink
feat(ci): enable tracing during e2e tests
Browse files Browse the repository at this point in the history
Note: we change the pydantic settings source for tracing options to
ensure that environment variables have higher one.

Signed-off-by: Fatih Acar <fatih@opsmill.com>
  • Loading branch information
fatih-acar committed Mar 31, 2024
1 parent 1b6a299 commit 2abd296
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 35 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,16 @@ jobs:
run: echo "INFRAHUB_DB_BACKUP_PORT=$(shuf -n 1 -i 10000-30000)" >> $GITHUB_ENV
- name: Select vmagent port
run: echo "VMAGENT_PORT=$(shuf -n 1 -i 10000-30000)" >> $GITHUB_ENV

- name: Enable tracing
run: echo "INFRAHUB_TRACE_ENABLE=true" >> $GITHUB_ENV
- name: Set tracing configuration
run: echo "INFRAHUB_TRACE_INSECURE=false" >> $GITHUB_ENV
- name: Set tracing configuration
run: echo "INFRAHUB_TRACE_EXPORTER_ENDPOINT=${{ secrets.TRACING_ENDPOINT }}" >> $GITHUB_ENV
- name: Set tracing configuration
run: echo "OTEL_RESOURCE_ATTRIBUTES=github.run_id=${GITHUB_RUN_ID}" >> $GITHUB_ENV

- name: "Store start time"
run: echo TEST_START_TIME=$(date +%s)000 >> $GITHUB_ENV

Expand Down
2 changes: 1 addition & 1 deletion backend/infrahub/cli/git_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def _start(debug: bool, port: int) -> None:
service="infrahub-git-agent",
version=__version__,
exporter_type=config.SETTINGS.trace.exporter_type,
exporter_endpoint=config.SETTINGS.trace.trace_endpoint,
exporter_endpoint=config.SETTINGS.trace.exporter_endpoint,
exporter_protocol=config.SETTINGS.trace.exporter_protocol,
)

Expand Down
42 changes: 12 additions & 30 deletions backend/infrahub/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Optional
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type

import toml
from infrahub_sdk import generate_uuid
from pydantic import AliasChoices, Field, ValidationError
from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource, SettingsConfigDict

from infrahub.database.constants import DatabaseType
from infrahub.exceptions import InitializationError
Expand Down Expand Up @@ -279,35 +279,17 @@ class TraceSettings(BaseSettings):
default=TraceTransportProtocol.GRPC, description="Protocol to be used for exporting traces"
)
exporter_endpoint: Optional[str] = Field(default=None, description="OTLP endpoint for exporting traces")
exporter_port: Optional[int] = Field(
default=None, ge=1, le=65535, description="Specified if running on a non default port (4317)"
)

@property
def service_port(self) -> int:
if self.exporter_protocol == TraceTransportProtocol.GRPC:
default_port = 4317
elif self.exporter_protocol == TraceTransportProtocol.HTTP_PROTOBUF:
default_port = 4318
else:
default_port = 4317

return self.exporter_port or default_port

@property
def trace_endpoint(self) -> Optional[str]:
if not self.exporter_endpoint:
return None
if self.insecure:
scheme = "http://"
else:
scheme = "https://"
endpoint = str(self.exporter_endpoint) + ":" + str(self.service_port)

if self.exporter_protocol == TraceTransportProtocol.HTTP_PROTOBUF:
endpoint += "/v1/traces"

return scheme + endpoint
@classmethod
def settings_customise_sources(
cls,
settings_cls: Type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> Tuple[PydanticBaseSettingsSource, ...]:
return env_settings, init_settings, dotenv_settings, file_secret_settings


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion backend/infrahub/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def app_initialization(application: FastAPI) -> None:
service="infrahub-server",
version=__version__,
exporter_type=config.SETTINGS.trace.exporter_type,
exporter_endpoint=config.SETTINGS.trace.trace_endpoint,
exporter_endpoint=config.SETTINGS.trace.exporter_endpoint,
exporter_protocol=config.SETTINGS.trace.exporter_protocol,
)

Expand Down
14 changes: 13 additions & 1 deletion backend/infrahub/trace.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
OTLPSpanExporter as GRPCSpanExporter,
Expand Down Expand Up @@ -31,8 +33,18 @@ def create_tracer_provider(
else:
raise ValueError("Exporter type unsupported by Infrahub")

extra_attributes = dict()
if os.getenv("OTEL_RESOURCE_ATTRIBUTES"):
extra_attributes = dict(attr.split("=") for attr in os.getenv("OTEL_RESOURCE_ATTRIBUTES").split(","))

# Resource can be required for some backends, e.g. Jaeger
resource = Resource(attributes={"service.name": service, "service.version": version})
resource = Resource(
attributes={
"service.name": service,
"service.version": version,
**extra_attributes,
}
)
span_processor = BatchSpanProcessor(exporter)
tracer_provider = TracerProvider(resource=resource)
tracer_provider.add_span_processor(span_processor)
Expand Down
8 changes: 8 additions & 0 deletions development/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ services:
- "INFRAHUB_SECURITY_INITIAL_ADMIN_TOKEN=06438eb2-8019-4776-878c-0941b1f1d1ec"
- "INFRAHUB_SECURITY_SECRET_KEY=327f747f-efac-42be-9e73-999f08f86b92"
- "INFRAHUB_ALLOW_ANONYMOUS_ACCESS=true"
- "INFRAHUB_TRACE_ENABLE=${INFRAHUB_TRACE_ENABLE:-false}"
- "INFRAHUB_TRACE_INSECURE=${INFRAHUB_TRACE_INSECURE:-true}"
- "INFRAHUB_TRACE_EXPORTER_ENDPOINT=${INFRAHUB_TRACE_EXPORTER_ENDPOINT:-http://jaeger:4317}"
- "OTEL_RESOURCE_ATTRIBUTES=${OTEL_RESOURCE_ATTRIBUTES:-}"
- "INFRAHUB_DB_TYPE=${INFRAHUB_DB_TYPE}"
volumes:
- "storage_data:/opt/infrahub/storage"
Expand Down Expand Up @@ -60,6 +64,10 @@ services:
- "INFRAHUB_LOG_LEVEL=DEBUG"
- "INFRAHUB_SDK_API_TOKEN=06438eb2-8019-4776-878c-0941b1f1d1ec"
- "INFRAHUB_SDK_TIMEOUT=20"
- "INFRAHUB_TRACE_ENABLE=${INFRAHUB_TRACE_ENABLE:-false}"
- "INFRAHUB_TRACE_INSECURE=${INFRAHUB_TRACE_INSECURE:-true}"
- "INFRAHUB_TRACE_EXPORTER_ENDPOINT=${INFRAHUB_TRACE_EXPORTER_ENDPOINT:-http://jaeger:4317}"
- "OTEL_RESOURCE_ATTRIBUTES=${OTEL_RESOURCE_ATTRIBUTES:-}"
- "INFRAHUB_DB_TYPE=${INFRAHUB_DB_TYPE}"
volumes:
- "git_data:/opt/infrahub/git"
Expand Down
3 changes: 1 addition & 2 deletions development/infrahub.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ enable = false
insecure = "True"
exporter_type = "otlp"
exporter_protocol = "grpc"
exporter_endpoint = "jaeger"
exporter_port = 4317
exporter_endpoint = "http://jaeger:4317"


# [experimental_features]
Expand Down

0 comments on commit 2abd296

Please sign in to comment.