我使用 Application Insights 和 OpenTelemetry (OTLP) 与 Dynatrace SaaS 集成,成功从 Azure API 管理 (APIM) 收集了摄取的跟踪。但是,我无法过滤 Dynatrace 中的跟踪,无论它们是使用 2xx、4xx 还是 5xx 响应代码进行响应。
-> 按 4xx 响应代码过滤不会返回任何带有 4xx 错误响应代码的请求,尽管有一些请求带有 4xx 错误响应代码。
我想过滤 Dynatrace 中收集的跟踪,仅包含导致 4xx 或 5xx 错误代码的请求,以便进一步分析和故障排除。
from flask import Flask, request, jsonify, render_template_string
import requests
import json
import warnings
app = Flask(__name__)
# Updated HTML template as a string
html_template = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Merged Application</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background-color: #f4f4f9; }
.section { margin-bottom: 40px; border: 1px solid #ddd; padding: 20px; border-radius: 5px; }
h2 { color: #333; }
label { font-weight: bold; display: block; margin-top: 10px; }
input[type="text"], textarea { width: 100%; padding: 10px; margin-bottom: 10px; }
button { padding: 10px 15px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer; margin-right: 10px; }
button:hover { background-color: #45a049; }
.preview, .result { background-color: #eaeaea; padding: 10px; margin-top: 10px; white-space: pre-wrap; }
</style>
</head>
<body>
<div class="container">
<div class="section">
<h2>Step 1: Management Zone</h2>
<form id="mgmt_zone_form">
<label for="mgmt_zone">Management Zone:</label>
<input type="text" id="mgmt_zone" name="mgmt_zone">
<button type="button" onclick="previewMgmtZone()">Preview</button>
<button type="button" onclick="executeMgmtZone()">Execute</button>
<h3>Preview:</h3>
<div id="mgmt_zone_preview" class="preview"></div>
<h3>Result:</h3>
<div id="mgmt_zone_result" class="result"></div>
</form>
</div>
<div class="section">
<h2>Step 2: Calculated Metrics</h2>
<form id="calc_metrics_form">
<label for="app_name">Application Name:</label>
<input type="text" id="app_name" name="app_name">
<label for="process_group">Process Group Name:</label>
<input type="text" id="process_group" name="process_group">
<button type="button" onclick="previewCalcMetrics()">Preview</button>
<button type="button" onclick="executeCalcMetrics()">Execute</button>
<h3>Preview:</h3>
<div id="calc_metrics_preview" class="preview"></div>
<h3>Result:</h3>
<div id="calc_metrics_result" class="result"></div>
</form>
</div>
<div class="section">
<h2>Step 3: Dashboard Creation</h2>
<form id="dashboard_form">
<label for="role_id">role id:</label>
<input type="text" id="role_id" name="role_id">
<label for="xyz_app_name">Application Name:</label>
<input type="text" id="xyz_app_name" name="xyz_app_name" readonly>
<label for="module">Module:</label>
<input type="text" id="module" name="module">
<label for="email_id">Email ID:</label>
<input type="text" id="email_id" name="email_id">
<label for="synthetic_tag">Synthetic Tag:</label>
<input type="text" id="synthetic_tag" name="synthetic_tag">
<label for="hostname">Hostname:</label>
<input type="text" id="hostname" name="hostname">
<label for="xyz_process_group">Process Group Name:</label>
<input type="text" id="xyz_process_group" name="xyz_process_group" readonly>
<button type="button" onclick="previewDashboard()">Preview</button>
<button type="button" onclick="executeDashboard()">Execute</button>
<h3>Preview:</h3>
<div id="dashboard_preview" class="preview"></div>
<h3>Result:</h3>
<div id="dashboard_result" class="result"></div>
</form>
</div>
</div>
<script>
function previewMgmtZone() {
let mgmt_zone = document.getElementById("mgmt_zone").value;
fetch('/preview_mgmt_zone', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'mgmt_zone=' + encodeURIComponent(mgmt_zone)
})
.then(response => response.json())
.then(data => {
document.getElementById('mgmt_zone_preview').textContent = data.preview_script;
});
}
function executeMgmtZone() {
let mgmt_zone = document.getElementById("mgmt_zone").value;
fetch('/execute_mgmt_zone', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'mgmt_zone=' + encodeURIComponent(mgmt_zone)
})
.then(response => response.json())
.then(data => {
document.getElementById('mgmt_zone_result').textContent = JSON.stringify(data.response, null, 2);
});
}
function previewCalcMetrics() {
let app_name = document.getElementById("app_name").value;
let process_group = document.getElementById("process_group").value;
fetch('/preview_calc_metrics', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'app_name=' + encodeURIComponent(app_name) + '&process_group=' + encodeURIComponent(process_group)
})
.then(response => response.json())
.then(data => {
document.getElementById('calc_metrics_preview').textContent = data.script;
// Update Step 3 fields
document.getElementById('xyz_app_name').value = app_name;
document.getElementById('xyz_process_group').value = process_group;
});
}
function executeCalcMetrics() {
let app_name = document.getElementById("app_name").value;
let process_group = document.getElementById("process_group").value;
fetch('/execute_calc_metrics', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'app_name=' + encodeURIComponent(app_name) + '&process_group=' + encodeURIComponent(process_group)
})
.then(response => response.json())
.then(data => {
document.getElementById('calc_metrics_result').textContent = JSON.stringify(data.output, null, 2);
// Update Step 3 fields
document.getElementById('xyz_app_name').value = app_name;
document.getElementById('xyz_process_group').value = process_group;
});
}
function previewDashboard() {
let formData = new FormData(document.getElementById('dashboard_form'));
fetch('/preview_dashboard', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
document.getElementById('dashboard_preview').textContent = data.script;
});
}
function executeDashboard() {
let formData = new FormData(document.getElementById('dashboard_form'));
fetch('/execute_dashboard', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
document.getElementById('dashboard_result').textContent = JSON.stringify(data.response, null, 2);
});
}
</script>
</body>
</html>
'''
@app.route('/')
def index():
return render_template_string(html_template)
# ABC Script routes
@app.route('/preview_mgmt_zone', methods=['POST'])
def preview_mgmt_zone():
mgmt_zone = request.form['mgmt_zone']
script = f'''
import requests
url = "http://127.0.0.1:5000/dummy-api/mgmt_zone?mzName={mgmt_zone}&pageSize=500"
headers = {{'Authorization': 'Bearer your_token_here'}}
response = requests.get(url, headers=headers, verify=False)
print(response.text)
'''
return jsonify({'preview_script': script})
@app.route('/execute_mgmt_zone', methods=['POST'])
def execute_mgmt_zone():
mgmt_zone = request.form['mgmt_zone']
url = f"http://127.0.0.1:5000/dummy-api/mgmt_zone?mzName={mgmt_zone}&pageSize=500"
headers = {'Authorization': 'Bearer your_token_here'}
response = requests.get(url, headers=headers, verify=False)
return jsonify({'response': response.json()})
@app.route('/preview_calc_metrics', methods=['POST'])
def preview_calc_metrics():
app_name = request.form['app_name']
process_group = request.form['process_group']
script = f'''
import requests
import json
import warnings
warnings.filterwarnings('ignore', message='Unverified HTTPS request')
url = "http://127.0.0.1:5000/dummy-api/calc_metrics"
headers = {{
'Authorization': 'Bearer your_token_here',
'Content-Type': 'application/json'
}}
payloads = [
json.dumps({{
"name": "{app_name}-statuscode",
"version": "1.0",
"description": "This is the first test.",
"metrics": [
{{
"enabled": True,
"metric": "REQUEST_COUNT",
"conditions": [
{{
"attribute": "PROCESS_GROUP.NAME",
"comparisonInfo": {{
"type": "FAST_STRING",
"comparison": "CONTAINS",
"value": "{process_group}",
"caseSensitive": False
}}
}}
]
}}
]
}}),
json.dumps({{
"name": "{app_name}-3xx",
"version": "1.0",
"description": "This is the second test.",
"metrics": [
{{
"enabled": True,
"metric": "ERROR_COUNT",
"conditions": [
{{
"attribute": "PROCESS_GROUP.NAME",
"comparisonInfo": {{
"type": "FAST_STRING",
"comparison": "CONTAINS",
"value": "{process_group}",
"caseSensitive": False
}}
}}
]
}}
]
}})
]
for payload in payloads:
response = requests.post(url, headers=headers, data=payload, verify=False)
print(response.status_code, response.text)
'''
return jsonify({'script': script})
@app.route('/execute_calc_metrics', methods=['POST'])
def execute_calc_metrics():
app_name = request.form['app_name']
process_group = request.form['process_group']
url = "http://127.0.0.1:5000/dummy-api/calc_metrics"
headers = {'Authorization': 'Bearer your_token_here', 'Content-Type': 'application/json'}
payloads = [
{
"name": f"{app_name}-statuscode",
"version": "1.0",
"description": "This is the first test.",
"metrics": [
{
"enabled": True,
"metric": "REQUEST_COUNT",
"conditions": [
{
"attribute": "PROCESS_GROUP.NAME",
"comparisonInfo": {
"type": "FAST_STRING",
"comparison": "CONTAINS",
"value": process_group,
"caseSensitive": False
}
}
]
}
]
},
{
"name": f"{app_name}-3xx",
"version": "1.0",
"description": "This is the second test.",
"metrics": [
{
"enabled": True,
"metric": "ERROR_COUNT",
"conditions": [
{
"attribute": "PROCESS_GROUP.NAME",
"comparisonInfo": {
"type": "FAST_STRING",
"comparison": "CONTAINS",
"value": process_group,
"caseSensitive": False
}
}
]
}
]
}
]
results = []
for payload in payloads:
response = requests.post(url, headers=headers, json=payload, verify=False)
results.append(f"{response.status_code} {response.text}")
return jsonify({'output': results})
# XYZ Script routes
@app.route('/preview_dashboard', methods=['POST'])
def preview_dashboard():
data = request.form
script = f'''
import requests
import json
url = "http://127.0.0.1:5000/dummy-api/dashboards"
payload = json.dumps({{
"metadata": {{
"configurationVersions": [7],
"clusterVersion": "1234"
}},
"dashboardMetadata": {{
"name": "AAA - {data['role_id']} - {data['xyz_app_name']} - {data['module']} - Prod",
"shared": True,
"owner": "{data['email_id']}",
"dashboardFilter": {{
"managementZone": {{
"id": "",
"name": "{data['synthetic_tag']}"
}}
}},
"popularity": 1,
"tilesNameSize": "{data['hostname']}",
"hasConsistentColors": "{data['xyz_process_group']}"
}}
}})
headers = {{
'Authorization': 'Bearer your_token_here',
'Content-Type': 'application/json'
}}
response = requests.post(url, headers=headers, data=payload, verify=False)
print(response.status_code, response.text)
'''
return jsonify({'script': script})
@app.route('/execute_dashboard', methods=['POST'])
def execute_dashboard():
data = request.form
url = "http://127.0.0.1:5000/dummy-api/dashboards"
payload = {
"metadata": {
"configurationVersions": [7],
"clusterVersion": "1234"
},
"dashboardMetadata": {
"name": f"AAA - {data['role_id']} - {data['xyz_app_name']} - {data['module']} - Prod",
"shared": True,
"owner": data['email_id'],
"dashboardFilter": {
"managementZone": {
"id": "",
"name": data['synthetic_tag']
}
},
"popularity": 1,
"tilesNameSize": data['hostname'],
"hasConsistentColors": data['xyz_process_group']
}
}
headers = {
'Authorization': 'Bearer your_token_here',
'Content-Type': 'application/json'
}
response = requests.post(url, headers=headers, json=payload, verify=False)
return jsonify({'response': response.json()})
if __name__ == '__main__':
app.run(port=5000, debug=True)