问题
I have successfully setup prometheus and grafana on my kubernetes dev cluster (following this: https://itnext.io/kubernetes-monitoring-with-prometheus-in-15-minutes-8e54d1de2e13).
Added this to Startup.cs for my sample .net core app:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
var counter = Metrics.CreateCounter("PathCounter", "Counts requests to endpoints", new CounterConfiguration
{
LabelNames = new[] { "method", "endpoint" }
});
app.Use((context, next) =>
{
counter.WithLabels(context.Request.Method, context.Request.Path).Inc();
return next();
});
app.UseMetricServer();
Should I specify anything for app.UseMetricServer(HERE?);
I've applied this yaml to add my app to be scraped:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: buygroup
labels:
app: buygroup
release: prom
spec:
namespaceSelector:
any: true
selector:
matchLabels:
app: buygroup
endpoints:
- port: web
interval: 10s
I don't see anything collected in Targets under: http://localhost:9090/targets
Installed .net dashboard but shows no results:
What do I have to do to have results scraped from my app "buygroup"?
Service yaml:
apiVersion: v1
kind: Service
metadata:
name: buygroup
labels:
name: buygroup
spec:
type: NodePort
selector:
app.kubernetes.io/instance: buygroup
app.kubernetes.io/name: buygroup
ports:
- name: http
port: 80
nodePort: 30601
targetPort: http
Service Monitor:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: monitoring-buygroup
namespace: monitoring
labels:
app: buygroup
spec:
selector:
matchLabels:
# Target app service
app: buygroup
endpoints:
- interval: 15s
path: /metrics
port: http
namespaceSelector:
matchNames:
- buygroup-namespace
回答1:
I figured it out:
ServiceMonitor:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: prometheus-operator-buygroup
namespace: monitoring
labels:
app: prometheus-operator-buygroup
release: prometheus-operator
spec:
selector:
matchLabels:
# Target app service
app.kubernetes.io/instance: buygroup
app.kubernetes.io/name: buygroup
endpoints:
- interval: 15s
path: /metrics
port: http
namespaceSelector:
any: true
Service:
apiVersion: v1
kind: Service
metadata:
name: prometheus-operator-buygroup
labels:
app: buygroup
app.kubernetes.io/instance: buygroup
app.kubernetes.io/name: buygroup
spec:
type: ClusterIP
ports:
- name: http
protocol: TCP
port: 80
targetPort: http
selector:
app: buygroup
app.kubernetes.io/instance: buygroup
app.kubernetes.io/name: buygroup
来源:https://stackoverflow.com/questions/60780448/setup-prometheus-operator-to-monitor-net-core-app