Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
How to return plain text from flask endpoint? Needed by Prometheus

I need to setup a /metrics endpoint so that Prometheus can consume statistics about an endpoint. How do I go about doing this?

I have this in my Flask app:

@app.route('/metrics')
def metrics():
    def generateMetrics():
        metrics = ""
        ... some string builder logic
        return metrics

    response = make_response(generateMetrics(), 200)
    response.mimetype = "text/plain"
    return response

Is this the best way? What is the difference between returning a String (just returning metrics here) and returning plain text? Why do I need the mimetype?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Is this the best way?

There are several ways to set the MIME type, better explained and discussed in this StackOverflow question. Your way works fine and gets the job done.

What is the difference between returning a String and returning plain text?

If you return a string Flask will automatically handle some of the Response logic for you. This means using some default values. If you set up two different endpoints you'll see that the difference turns out to be that your code returns the following header:

Content-Type:"text/plain; charset=utf-8"

While just returning a string (and default MIME type) would return the following header:

Content-Type:"text/html; charset=utf-8"

Why do I need the mimetype?

You might say that it is technically more correct, given that your response is simply plain text, and not HTML. However, a more forcing reason for needing it would be that a third party system you are using (Prometheus) relies on or cares about the contents of the "Content-Type" header. If they do, then you must set it for them to accept it.

Example code

For the Content-Type header demonstration I used the following example Python code:

from flask import Flask, make_response
app = Flask(__name__)


def generateMetrics():
    return "hello world"


@app.route('/metrics')
def metrics():
    response = make_response(generateMetrics(), 200)
    response.mimetype = "text/plain"
    return response


@app.route('/metrics2')
def metrics2():
    return generateMetrics()

I then viewed the returned body and headers using Postman.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda