# Stripping protocol and optional port from target

**URL:** <https://discuss.prometheus.io/t/stripping-protocol-and-optional-port-from-target/2123>\
**Category:** General Help/Support\
**Created:** [January 26, 2024, 6:57pm UTC](https://discuss.prometheus.io/t/stripping-protocol-and-optional-port-from-target/2123 "2024-01-26T18:57:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![UntouchedWagons](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.prometheus.io/untouchedwagons/32/907_2.png) [@UntouchedWagons](https://discuss.prometheus.io/u/UntouchedWagons)\
**Post date:** [January 26, 2024, 6:57pm UTC](https://discuss.prometheus.io/t/stripping-protocol-and-optional-port-from-target/2123/1 "2024-01-26T18:57:08Z")

</div>

I’ve mostly managed to get scraping with Blackbox to work but I’m having issues normalizing the target FQDNs across my scrape configs. Here’s one of my scrape configs:

```auto
---
apiVersion: monitoring.coreos.com/v1alpha1
kind: ScrapeConfig
metadata:
  name: &app exporter-blackbox
  namespace: monitoring
spec:
  scrapeInterval: 1m
  metricsPath: /probe
  params:
    module: [http_2xx]
  staticConfigs:
    - targets:
      - http://brother_hl-2270dw.internal.untouchedwagons.com
      - http://homeassistant.internal.untouchedwagons.com:8123
      - https://pve-cluster-node-01.internal.untouchedwagons.com:8006
  relabelings:
    - action: replace
      sourceLabels: [__address__]
      targetLabel: __param_target
    - action: replace
      sourceLabels: [__param_target]
      targetLabel: instance
    - action: replace
      targetLabel: __address__
      replacement: exporter-blackbox.monitoring.svc.cluster.local:9115
    - action: replace
      targetLabel: module
      replacement: http_2xx

```

There are other targets of course but as you can see two are http while the third is https, the first has no port specified while the second and third do. My other scrape jobs are similar with other modules and ports. What I want is the FQDN to be the same across all the jobs (IE `pve-cluster-node-01.internal.untouchedwagons.com`). I’ve tried using a regex to strip the protocol and optional port but I get alerts from Prometheus that these scrap jobs have been rejected.

```auto
  relabelings:
    - action: replace
      sourceLabels: [__address__]
      targetLabel: __param_target
    - action: replace
      sourceLabels: [__param_target]
      regex: ([\w\-.]+):?+[\d]* # This does not work
      replacement: '$1' # This does not work
      targetLabel: instance
    - action: replace
      targetLabel: __address__
      replacement: exporter-blackbox.monitoring.svc.cluster.local:9115
    - action: replace
      targetLabel: module
      replacement: ssh_banner

```

---

<div class="post-metadata">

**Author:** ![UntouchedWagons](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.prometheus.io/untouchedwagons/32/907_2.png) [@UntouchedWagons](https://discuss.prometheus.io/u/UntouchedWagons)\
**Post date:** [January 28, 2024, 6:49pm UTC](https://discuss.prometheus.io/t/stripping-protocol-and-optional-port-from-target/2123/2 "2024-01-28T18:49:46Z")

</div>

Well I got it working, I don’t know what the issue was, possibly the regular expression? Anyways this is what I’ve got:

```auto
apiVersion: monitoring.coreos.com/v1alpha1
kind: ScrapeConfig
metadata:
  name: &app exporter-blackbox-tcp
  namespace: monitoring
spec:
  scrapeInterval: 1m
  metricsPath: /probe
  params:
    module: [tcp_connect]
  staticConfigs:
    - targets:
      - idrac-7p57cz1.internal.untouchedwagons.com:443
      - idrac-cjtyp22.internal.untouchedwagons.com:443
      - idrac-czblpd2.internal.untouchedwagons.com:443
      - idrac-jnky182.internal.untouchedwagons.com:443
      - ups.internal.untouchedwagons.com:3493
  relabelings:
    - action: replace
      sourceLabels: [__address__]
      targetLabel: __param_target
    - action: replace
      sourceLabels: [__param_target]
      regex: ([^:]+)(?::\d+)? # The first group matches the host, the second matches the port.
      replacement: '$1'
      targetLabel: instance
    - action: replace
      targetLabel: __address__
      replacement: exporter-blackbox.monitoring.svc.cluster.local:9115
    - action: replace
      targetLabel: module
      replacement: tcp_connect

```

On the targets page the port will still be shown but it will be stripped as expected in the actual records.
