AWS Builder Center

Migrating Legacy Systems to Modern Observability: Using AI to Accelerate Instrumentation

A practical guide to using AI to analyze legacy codebases and unstructured logs, turning a months-long manual instrumentation project into a guided, incremental migration.

Google cloud certified professional machine learning engineer

Why This Topic

AWS Builder Center content that performs well shares a consistent shape: a real implementation experience, a step by step build, an explained architecture decision, and lessons pulled from an actual pain point, not abstract theory. Most observability content on the platform assumes a greenfield or already-modern service, add OpenTelemetry, wire up a collector, build a dashboard. Almost none addresses the far more common reality inside large organizations: a legacy monolith, a decade old codebase, unstructured print-statement logging, and no tracing at all, where the real blocker to observability isn't tooling knowledge, it's the sheer manual effort of figuring out what to instrument and where.
This is where AI genuinely changes the economics of a migration project. Using an LLM to analyze legacy code structure and existing log statements turns a manual, error prone discovery phase into a guided, reviewable one. This sits at the intersection of your niche, Observability Engineering and DevOps Automation with Python, applied to the unglamorous but extremely common problem of modernizing systems that were never built with observability in mind.



The Problem: Legacy Systems Resist Observability by Default

Modern observability tooling assumes structured telemetry: named spans, typed log fields, consistent service boundaries. Legacy systems typically have none of this:
  • Logging is unstructured,  print()  statements or free text log lines with no consistent format, no request correlation, no severity levels applied consistently
  • Code structure doesn't map cleanly to service boundaries, a single monolith may contain what should logically be a dozen instrumentable subsystems, with no clear seams
  • Nobody currently on the team fully understands which code paths matter most, institutional knowledge has eroded over years of turnover
The manual approach, an engineer reading through hundreds of thousands of lines of legacy code deciding where to add instrumentation, is exactly the kind of large scale pattern recognition and triage task where AI assistance meaningfully changes the timeline, from a multi quarter project to a multi week one, without skipping the human review step that legacy code absolutely still requires.



The Architecture: AI-Assisted Discovery, Human-Reviewed Implementation

[ Legacy Codebase ] [ Legacy Unstructured Logs ]
| |
(static analysis + (log pattern extraction)
LLM code review)
| |
v v
[ Candidate instrumentation [ Inferred log schema:
points, ranked by call fields, severity, likely
frequency and complexity ] correlation IDs ]
| |
+------------------+-----------------+
|
v
[ Human review: engineer approves/edits
the AI-suggested instrumentation plan ]
|
v
[ Incremental rollout: OTel SDK added to
approved code paths, one module at a time ]
|
v
[ Structured logging + tracing bridges
legacy and modern telemetry in one backend ]
The critical design principle here: AI accelerates discovery and drafting, it does not autonomously modify legacy production code. Every suggestion passes through a human review step before anything ships, this isn't optional given how much legacy code carries undocumented behavior that a static or AI-based analysis alone cannot fully account for.



Step 1: Using AI to Analyze Legacy Code for Instrumentation Candidates

Rather than manually reading through an entire legacy codebase, an LLM can review function-level code and flag where instrumentation would provide the most value, ranked by complexity and apparent criticality:
python
import boto3
import json
import ast

bedrock = boto3.client("bedrock-runtime", region_name="us-east-1")

def extract_functions_from_file(filepath):
"""Parses a Python file and extracts function definitions with source."""
with open(filepath) as f:
source = f.read()
tree = ast.parse(source)
functions = []
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
func_source = ast.get_source_segment(source, node)
functions.append({"name": node.name, "source": func_source, "file": filepath})
return functions

def analyze_instrumentation_candidate(function_info):
prompt = f"""Review this legacy function and assess it as a candidate for
OpenTelemetry instrumentation (tracing spans, key attributes, structured logging).

Function name: {function_info['name']}
File: {function_info['file']}
Source:
{function_info['source'][:3000]}

Respond in this exact format:
PRIORITY: [high/medium/low]
REASON: [one sentence on why, e.g. external I/O, error handling, business critical path]
SUGGESTED_SPAN_NAME: [a short, descriptive span name]
SUGGESTED_ATTRIBUTES: [comma-separated list of 2-4 attributes worth capturing]"""

response = bedrock.invoke_model(
modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
body=json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 250,
"messages": [{"role": "user", "content": prompt}],
}),
)
result = json.loads(response["body"].read())
return result["content"][0]["text"]
Running this across a legacy codebase, filtered first to functions above a reasonable size or cyclomatic complexity threshold to avoid wasting review time on trivial helpers, produces a ranked, reviewable list instead of a blank page. A senior engineer's job shifts from "find the important code paths" to "confirm or correct what's already been drafted," a meaningfully faster starting point.



Step 2: Inferring Structure from Unstructured Legacy Logs

Legacy logs are often free text with no consistent schema. Before wiring up structured logging, it helps to understand what fields already exist implicitly in the log text:
python
import re
from collections import Counter

def sample_log_patterns(log_file_path, sample_size=500):
"""Pulls a representative sample of distinct-looking log lines
for schema inference, avoiding near-duplicate noise."""
seen_patterns = set()
samples = []
with open(log_file_path) as f:
for line in f:
# Normalize numbers and UUIDs to detect structurally similar lines
normalized = re.sub(r"\d+", "N", line)
normalized = re.sub(r"[0-9a-f-]{36}", "UUID", normalized)
if normalized not in seen_patterns:
seen_patterns.add(normalized)
samples.append(line.strip())
if len(samples) >= sample_size:
break
return samples

def infer_log_schema(log_samples):
joined_samples = "\n".join(log_samples[:40])
prompt = f"""These are sample lines from a legacy application's unstructured logs.

{joined_samples}

Infer a structured logging schema that would capture the same information.
List: field names, likely data types, and which fields look like they could
serve as a correlation ID (request ID, session ID, transaction ID, etc.)."""

response = bedrock.invoke_model(
modelId="anthropic.claude-3-5-sonnet-20241022-v2:0",
body=json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 400,
"messages": [{"role": "user", "content": prompt}],
}),
)
result = json.loads(response["body"].read())
return result["content"][0]["text"]
The normalization step before sampling matters more than it looks. Without it, near identical log lines differing only by a timestamp or ID dominate the sample, and the LLM ends up analyzing 40 near duplicates instead of 40 genuinely distinct patterns.



Step 3: Bridging Legacy and Modern Telemetry Incrementally

Once instrumentation candidates and a log schema are identified, the rollout itself follows a strangler-fig style pattern, adding OpenTelemetry incrementally around legacy code without a rewrite:
python
from opentelemetry import trace
import functools

tracer = trace.get_tracer("legacy-bridge")

def instrument_legacy_function(span_name, **static_attributes):
"""A decorator that wraps a legacy function with a trace span,
without requiring any changes to the function's internal logic."""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
with tracer.start_as_current_span(span_name) as span:
for key, value in static_attributes.items():
span.set_attribute(key, value)
try:
result = func(*args, **kwargs)
span.set_attribute("legacy.result", "success")
return result
except Exception as e:
span.set_attribute("legacy.result", "failure")
span.record_exception(e)
raise
return wrapper
return decorator

# Applied to a legacy function with zero internal modification:
@instrument_legacy_function("process-order-legacy", system="order-processing", legacy=True)
def process_order(order_id):
# untouched legacy business logic
pass
This decorator pattern is deliberately non-invasive, it wraps AI-identified high-value functions with tracing without requiring a rewrite of legacy internals, which is exactly the kind of low-risk, incremental change that legacy systems can actually tolerate.



Real World Scenario: What This Looks Like in Practice

A ten year old order processing monolith with roughly 200,000 lines of code and no tracing needs observability before a planned migration to microservices. Manually reading the codebase to find the twenty or so functions worth instrumenting first would typically take a senior engineer two to three weeks of dedicated reading. Running the AI-assisted analysis across the codebase produces a ranked candidate list in under a day, the engineer spends roughly three days reviewing and correcting the AI's priority calls rather than starting from zero, and the first ten instrumented functions, wrapped with the non-invasive decorator pattern, ship within the first week, covering the order processing system's most business-critical paths well before the planned migration deadline.



Common Pitfalls

  • Letting AI suggestions ship without human review. Legacy code frequently has undocumented side effects, error handling significance, or historical workarounds that static or AI-based analysis alone cannot fully surface. Review is not optional here.
  • Instrumenting everything at once. Start with the highest-priority candidates and roll out incrementally, a big bang instrumentation change on a legacy system is a reliability risk in itself.
  • Skipping the log normalization step. Feeding raw, unnormalized log samples to schema inference wastes the exercise on near-duplicates instead of genuinely distinct patterns.
  • Assuming AI understands undocumented business logic. The model can identify structural complexity and I/O boundaries well, it cannot know why a legacy workaround exists. Treat its output as a well-informed starting draft, not a final answer.
  • Neglecting the bridge between old and new telemetry formats. If legacy structured logs and new OTel spans don't share a common correlation ID strategy, you end up with two disconnected observability systems instead of one unified one.



Closing Thought

Migrating legacy systems to modern observability has always been more of a discovery problem than a tooling problem, the OTel SDK itself is well documented, the real cost has always been the manual effort of figuring out what to instrument in code nobody fully remembers anymore. Using AI to analyze code structure and infer log schemas doesn't remove the need for careful, incremental, human-reviewed rollout, but it collapses the discovery phase from weeks of manual reading into a reviewable draft in hours. For organizations sitting on large legacy estates, that shift alone is often what makes an observability migration project finally feel achievable instead of perpetually postponed.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article