<p>I am successfully configure my service monitor to monitor the API that provide metrics that is running in the kubernetes pod. However, I also would like to add external service to my service monitor target. This external service is arangoDB oasis exporter metrics (<a href="https://www.youtube.com/watch?v=c8i7K4HUPF4&amp;t=554s" rel="nofollow noreferrer">https://www.youtube.com/watch?v=c8i7K4HUPF4&amp;t=554s</a>). And this service is not running in kubernetes container. Here are my configuration files concerned:</p>
<ol>
<li><code>/helm/charts/prometheus-xxx/templates/service_monitor.tpl</code></li>
</ol>
<pre><code>---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template &quot;jobs-manager-servicemonitor.fullname&quot; . }}
  # Change this to the namespace the jobs-manager-servicemonitor instance is running in
  namespace: {{ .Values.serviceMonitor.namespace }}
  labels:
    serviceapp: {{ template &quot;jobs-manager-servicemonitor.name&quot; . }}
    release: &quot;{{ .Release.Name }}&quot;
spec:
  selector:
    matchLabels:
      # Targets jobs-manager service
      app.kubernetes.io/instance: {{ .Values.instance.name }}
  endpoints:
    - port: {{ .Values.service.metricsPort.name }}
      interval: {{ .Values.serviceMonitor.interval }}
      {{- if .Values.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }}
      {{- end }}
  namespaceSelector:
    matchNames:
    - {{ .Values.Namespace }}
</code></pre>
<ol start="2">
<li><code>/helm/charts/prometheus-xxx/Chart.yaml</code></li>
</ol>
<pre><code>apiVersion: v1
appVersion: &quot;1.0.0&quot;
description: Prometheus Service monitor, customized
name: jobs-manager-servicemonitor
version: 1.0.1
</code></pre>
<ol start="3">
<li><code>/helm/charts/prometheus-xxx/templates/_helpers.tpl</code></li>
</ol>
<pre><code>{{/*
Expand the name of the chart.
*/}}
{{- define &quot;jobs-manager-servicemonitor.name&quot; -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix &quot;-&quot; }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define &quot;jobs-manager-servicemonitor.fullname&quot; -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix &quot;-&quot; -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix &quot;-&quot; -}}
{{- else -}}
{{- printf &quot;%s-%s&quot; .Release.Name $name | trunc 63 | trimSuffix &quot;-&quot; -}}
{{- end -}}
{{- end -}}
{{- end -}}
</code></pre>
<ol start="4">
<li><code>/helm/charts/prometheus-xxx/values.yaml</code></li>
</ol>
<pre><code>serviceMonitor:
  enabled: false
  namespace: prometheus
  interval: 10s
  scrapeTimeout: 10s

service:
  metricsPort:
    name: http

instance:
  name: jobs-manager
  
Namespace: test1
</code></pre>
<p>Do you have any suggestion on how to add the external service that is not running in kubernetes pod into the target of service monitor? Thank you very much in advance.</p>
<p><strong>----------UPDATE POST----------</strong></p>
<p>Here are my new config files in the charts template <code>arangodb-servicemonitor</code>:</p>
<ol>
<li><code>/helm/charts/arangodb-servicemonitor/templates/service.yaml</code></li>
</ol>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: arangodb
  namespace: prometheus
  labels:
    app: arangodb
    release: prometheus
spec:
  type: ClusterIP
  externalName: xxxxx.arangodb.cloud:xxxx
  ports:
    - name: metrics
      port: 9000
      targetPort: 9000
      protocol: TCP
    - bearer_token: [ARANGODB_TOKEN]
    type: ExternalName
</code></pre>
<ol start="2">
<li><code>/helm/charts/arangodb-servicemonitor/templates/endpoints.yaml</code></li>
</ol>
<pre><code>kind: Endpoints
apiVersion: v1
metadata:
  name: arangodb
  labels:
    app: arangodb
subsets:
- addresses:
  - ip: xxxxx.arangodb.cloud:xxxx
  ports: 
  - name: metrics
    port: 9000
    protocol: TCP
</code></pre>
<ol start="3">
<li><code>/helm/charts/arangodb-servicemonitor/templates/service_monitor.tpl</code></li>
</ol>
<pre><code>---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: arangodb-servicemonitor
  # Change this to the namespace the arangodb-servicemonitor instance is running in
  namespace: prometheus
  labels:
    serviceapp: arangodb-servicemonitor
    release: prometheus
spec:
  selector:
    # Targets arangodb service
    app: arangodb
  endpoints:
    # TO DO: use an array (List) of endpoints to monitor many endpoints
    - port: metrics
      interval: 30s
  namespaceSelector:
    matchNames:
    # TO DO: use an array (List) of endpoints to monitor many endpoints
    - default
</code></pre>
<p>But when I do <code>terraform apply</code>, I get this error message regarding the <code>bearer_token</code> that I added:</p>
<p><a href="https://i.stack.imgur.com/6PggK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6PggK.png" alt="makeapplyERR" /></a></p>
<p>For info, I need to add this token to connect to ArangoDB external service:</p>
<p><a href="https://i.stack.imgur.com/XuX3A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XuX3A.png" alt="connectArangoDB" /></a></p>

<p>Create the <strong>Kubernetes</strong> service and use the Prometheus <strong>ServiceMonitor</strong> same way.</p>
<p>create the K8s service</p>
<pre><code>kind: Service
apiVersion: v1
metadata:
 name: arangoDB
spec:
 type: ClusterIP
 ports:
 - name: metrics
   port: 9000
   targetPort: 9000
</code></pre>
<p>create the <strong>Endpoint</strong> config of that Db in this prometheus should be able to access the IP of your Database</p>
<pre><code>kind: Endpoints
apiVersion: v1
metadata:
 name: arangoDB
subsets:
 - addresses:
     - ip: IP of Database
   ports: 
     - name: metrics
       port: 9000 
</code></pre>
<p>so nice service monitor will check the Kubernetes service and Kubernetes service will be pointing to the Endpoint and database and getting metrics from external service.</p>
<p>check endpoint service creation details at.
: <a href="https://kubernetes.io/docs/concepts/services-networking/service/#services-without-selectors" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/service/#services-without-selectors</a></p>
