Skip to content

Panic on registering /metrics globally #373

@grishy

Description

@grishy

Have you read a contributing guide?

  • I have read CONTRIBUTING.md
  • I have searched the existing issues and didn't find any that were similar
  • I have considered creating a pull request with fixes instead of a bug report and want to proceed

Current Behavior

Hi,
Thanks for project ❤️
I am creating a build of an Anytype all-in-one, and one of the issues that I encountered was that some components register themselves globally, like metrics. You can't create two separate App instances and register them to separate metrics because they are using a global HTTP package.

Problem line -

http.Handle("/metrics", promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{}))

Error:

// ...
panic: pattern "/metrics" (registered at /Users/grishy/go/pkg/mod/github.com/anyproto/any-sync@v0.5.22/metric/metric.go:107) conflicts with pattern "/metrics" (registered at /Users/grishy/go/pkg/mod/github.com/anyproto/any-sync@v0.5.22/metric/metric.go:107):
        /metrics matches the same requests as /metrics

goroutine 1 [running]:
net/http.(*ServeMux).register(...)
        /opt/homebrew/Cellar/go/1.23.5/libexec/src/net/http/server.go:2797
net/http.Handle({0x104c7abfb?, 0x0?}, {0x105272e60?, 0x14000256cc0?})
        /opt/homebrew/Cellar/go/1.23.5/libexec/src/net/http/server.go:2781 +0x98
github.com/anyproto/any-sync/metric.(*metric).Run(0x140004d1d40, {0x1051cf9c0?, 0x1400050e880?})
        /Users/grishy/go/pkg/mod/github.com/anyproto/any-sync@v0.5.22/metric/metric.go:107 +0x18c
github.com/anyproto/any-sync/app.(*App).Start(0x14000a12120, {0x10527d590, 0x140008e8200})
        /Users/grishy/go/pkg/mod/github.com/anyproto/any-sync@v0.5.22/app/app.go:262 +0x490
main.main()
        /Users/grishy/personal/github/any-sync-bundle/main.go:35 +0x188
exit status 2

I want to ask for a way that you think would be better so I can create a PR.
Thx!

Expected Behavior

Allow specifying the path for metrics if they are registered globally to avoid conflicts or to merge metrics properly

Steps To Reproduce

Create two apps with separate instances of metric.

Environment

n/a

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions