Using WrapCollectorWith to un-register metrics registered by a third party lib. newThirdPartyLibFoo illustrates a constructor from a third-party lib that does not expose any way to un-register metrics.
()
| 791 | // newThirdPartyLibFoo illustrates a constructor from a third-party lib that does |
| 792 | // not expose any way to un-register metrics. |
| 793 | func ExampleWrapCollectorWith() { |
| 794 | reg := prometheus.NewRegistry() |
| 795 | |
| 796 | // We want to create two instances of thirdPartyLibFoo, each one wrapped with |
| 797 | // its "instance" label. |
| 798 | firstReg := prometheus.NewRegistry() |
| 799 | _ = newThirdPartyLibFoo(firstReg) |
| 800 | firstCollector := prometheus.WrapCollectorWith(prometheus.Labels{"instance": "first"}, firstReg) |
| 801 | reg.MustRegister(firstCollector) |
| 802 | |
| 803 | secondReg := prometheus.NewRegistry() |
| 804 | _ = newThirdPartyLibFoo(secondReg) |
| 805 | secondCollector := prometheus.WrapCollectorWith(prometheus.Labels{"instance": "second"}, secondReg) |
| 806 | reg.MustRegister(secondCollector) |
| 807 | |
| 808 | // So far we have illustrated that we can create two instances of thirdPartyLibFoo, |
| 809 | // wrapping each one's metrics with some const label. |
| 810 | // This is something we could've achieved by doing: |
| 811 | // newThirdPartyLibFoo(prometheus.WrapRegistererWith(prometheus.Labels{"instance": "first"}, reg)) |
| 812 | metricFamilies, err := reg.Gather() |
| 813 | if err != nil { |
| 814 | panic("unexpected behavior of registry") |
| 815 | } |
| 816 | fmt.Println("Both instances:") |
| 817 | fmt.Println(toNormalizedJSON(sanitizeMetricFamily(metricFamilies[0]))) |
| 818 | |
| 819 | // Now we want to unregister first Foo's metrics, and then register them again. |
| 820 | // This is not possible by passing a wrapped Registerer to newThirdPartyLibFoo, |
| 821 | // because we have already lost track of the registered Collectors, |
| 822 | // however since we've collected Foo's metrics in it's own Registry, and we have registered that |
| 823 | // as a specific Collector, we can now de-register them: |
| 824 | unregistered := reg.Unregister(firstCollector) |
| 825 | if !unregistered { |
| 826 | panic("unexpected behavior of registry") |
| 827 | } |
| 828 | |
| 829 | metricFamilies, err = reg.Gather() |
| 830 | if err != nil { |
| 831 | panic("unexpected behavior of registry") |
| 832 | } |
| 833 | fmt.Println("First unregistered:") |
| 834 | fmt.Println(toNormalizedJSON(sanitizeMetricFamily(metricFamilies[0]))) |
| 835 | |
| 836 | // Now we can create another instance of Foo with {instance: "first"} label again. |
| 837 | firstRegAgain := prometheus.NewRegistry() |
| 838 | _ = newThirdPartyLibFoo(firstRegAgain) |
| 839 | firstCollectorAgain := prometheus.WrapCollectorWith(prometheus.Labels{"instance": "first"}, firstRegAgain) |
| 840 | reg.MustRegister(firstCollectorAgain) |
| 841 | |
| 842 | metricFamilies, err = reg.Gather() |
| 843 | if err != nil { |
| 844 | panic("unexpected behavior of registry") |
| 845 | } |
| 846 | fmt.Println("Both again:") |
| 847 | fmt.Println(toNormalizedJSON(sanitizeMetricFamily(metricFamilies[0]))) |
| 848 | |
| 849 | // Output: |
| 850 | // Both instances: |
nothing calls this directly
no test coverage detected