Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to unexpose /metrics to public internet #21

Open
jasonlimantoro opened this issue Oct 31, 2021 · 1 comment
Open

How to unexpose /metrics to public internet #21

jasonlimantoro opened this issue Oct 31, 2021 · 1 comment

Comments

@jasonlimantoro
Copy link

jasonlimantoro commented Oct 31, 2021

How does one hide the /metrics from the public internet? And once it's hidden, what's the usual practice for Grafana/Grafana Cloud to scrape this hidden/protected endpoint?

I'm asking because I figured out that exposing /metrics to the world is unacceptable (unless I'm missing something obvious).

@porkloin
Copy link

porkloin commented Feb 5, 2022

@jasonlimantoro I know it's been a while since you commented, but for posterity's sake I'll include an answer (not maintainer, so take this with a grain of salt).

As far as I can tell, the instance of express() that you pass to createPrometheusExporterPlugin({ yourExpressInstance }) doesn't have to be the same express serving your Apollo Server. I initially thought there would be some configuration allowing me to decide which port to expose, but ended up just instantiating a new Express server specifically for serving prometheus metrics, bound it to a non-internet facing port, and then configured that port's access according to the restrictions I had. Beyond that I can't specify how you'd configure your particular stack.

A basic example might look like this:

const metricsApp = express();
metricsApp.listen('6666',  () => {
  console.log('Apollo Prometheus Exporter server running on :6666');
}
const prometheusExporterPlugin = createPrometheusExporterPlugin({ metricsApp });

const server = new ApolloServer({ 
  typeDefs,
  resolvers,
  plugins: [prometheusExporterPlugin], 
});
const apolloApp = express();
server.applyMiddleware({ apolloApp });
apolloApp.listen('4000', () => {
  console.log('Apollo Express running at :4000');
});

In this case, localhost:4000/graphql would serve your apollo server, and localhost:6666/metrics would serve your prometheus metrics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants