diff --git a/amqp/consul/consul_config.py b/amqp/consul/consul_config.py new file mode 100644 index 0000000..968e829 --- /dev/null +++ b/amqp/consul/consul_config.py @@ -0,0 +1,51 @@ +from flask import Flask, request, jsonify +import consul +from opentelemetry import trace +from opentelemetry.instrumentation.flask import FlaskInstrumentor +from opentelemetry.instrumentation.requests import RequestsInstrumentor +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchSpanProcessor +from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter + +# Initialize Flask app +app = Flask(__name__) + +# Initialize Consul client +consul_client = consul.Consul(host='127.0.0.1', port=8500) + +# OpenTelemetry configuration +trace.set_tracer_provider(TracerProvider()) +tracer = trace.get_tracer(__name__) + +# Configure OTLP exporter (e.g., for exporting to Jaeger or Prometheus via OpenTelemetry Collector) +otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True) +span_processor = BatchSpanProcessor(otlp_exporter) +trace.get_tracer_provider().add_span_processor(span_processor) + +# Instrument Flask and Requests +FlaskInstrumentor().instrument_app(app) +RequestsInstrumentor().instrument() + +# Endpoint to set configuration properties in Consul +@app.route('/config', methods=['POST']) +def set_config(): + key = request.json.get('key') + value = request.json.get('value') + + if not key or not value: + return jsonify({"error": "Key and value are required"}), 400 + + consul_client.kv.put(key, value) + return jsonify({"message": "Configuration set", "key": key, "value": value}) + +# Endpoint to get configuration properties from Consul +@app.route('/config/', methods=['GET']) +def get_config(key): + with tracer.start_as_current_span("get_config"): # Manual tracing + index, data = consul_client.kv.get(key) + if data and 'Value' in data: + return jsonify({"key": key, "value": data['Value'].decode('utf-8')}) + return jsonify({"error": "Key not found"}), 404 + +if __name__ == '__main__': + app.run(debug=True)