Thank you. I moved this line before the loop, but still received the same issue.
On Tuesday, August 9, 2022 at 2:58:16 AM UTC+8 [email protected] wrote: > Move > > g1 = Gauge('ldap_query_success', 'LDAP query command', ['ldap_uri', > 'ldap_search_base'], registry=registry) > > before the loop - you don't want to initialize it each time. > On Monday, August 8, 2022 at 7:00:42 AM UTC-4 [email protected] wrote: > >> Thank you I have resolved the issue. >> >> I also tried to use the interfaces to create and record the metrics. I >> have tested with following codes and found that the value of the metrcis >> will be overrided by the last value of the metrics. >> >> For example: >> real situation is: >> service1 -> ldap_query_success{...} 0 >> service2 -> ldap_query_success{...} 0 >> service3 -> ldap_query_success{...} 1 >> >> but with the following codes: >> service1 -> ldap_query_success{...} 1 >> service2 -> ldap_query_success{...} 1 >> service3 -> ldap_query_success{...} 1 >> >> >> >> from prometheus_client import Gauge, write_to_textfile, CollectorRegistry >> >> for service in services: >> g1 = Gauge('ldap_query_success', 'LDAP query command', ['ldap_uri', >> 'ldap_search_base'], registry=registry) >> >> g1.labels(service,ldap_search_base,ldap_default_bind_dn).set(query_check) >> write_to_textfile("/var/log >> node_exporter/filecollector/ldap_query.prom", registry) >> >> >> >> On Monday, August 8, 2022 at 5:31:29 PM UTC+8 Stuart Clark wrote: >> >>> On 08/08/2022 09:58, nina guo wrote: >>> > But the following 3 lines should be appended to a file first, then >>> > next time override the old content. But how to make the old content be >>> > overried by previous ones? >>> > >>> > print (" # HELP ldap_query_success LDAP query command", >>> > file=open("/var/log/node_exporter/filecollector/ldap_query.prom", >>> "a+")) >>> > print (" # TYPE ldap_query_success gauge", >>> > file=open("/var/log/node_exporter/filecollector/ldap_query.prom", >>> "a+")) >>> > print >>> > >>> ('ldap_query_success'+'{'+'ldap_uri'+'='+service+','+'ldap_search_base'+'='+ldap_search_base+','+'} >>> >>> >>> > '+str(query_check), >>> > file=open("/var/log/node_exporter/filecollector/ldap_query.prom", >>> "a+")) >>> > >>> File mode "a" will open for appending (so preserve anything already in >>> the file). Instead to fully replace the file you'd need to use file mode >>> "w". >>> >>> -- >>> Stuart Clark >>> >>> -- You received this message because you are subscribed to the Google Groups "Prometheus Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/e3e93c3e-fff4-433c-a870-a661d4a8c81an%40googlegroups.com.

