SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
package factory
import (
"github.com/SigNoz/signoz/pkg/errors"
)
// Named is implemented by all types of factories.
type Named interface {
Name() Name
}
type NamedMap[T Named] struct {
factories map[Name]T
factoriesInOrder []T
}
// NewNamedMap creates a new NamedMap from a list of factories.
// It returns an error if the factories have duplicate names.
func NewNamedMap[T Named](factories ...T) (NamedMap[T], error) {
fmap := make(map[Name]T)
for _, factory := range factories {
if _, ok := fmap[factory.Name()]; ok {
return NamedMap[T]{}, errors.NewInvalidInputf(
errors.CodeInvalidInput,
"cannot build factory map, duplicate name %q found",
factory.Name(),
)
}
fmap[factory.Name()] = factory
}
return NamedMap[T]{factories: fmap, factoriesInOrder: factories}, nil
}
// MustNewNamedMap creates a new NamedMap from a list of factories.
// It panics if the factories have duplicate names.
func MustNewNamedMap[T Named](factories ...T) NamedMap[T] {
nm, err := NewNamedMap(factories...)
if err != nil {
panic(err)
}
return nm
}
// Get returns the factory for the given name by string.
// It returns an error if the factory is not found or the name is invalid.
func (n *NamedMap[T]) Get(namestr string) (t T, err error) {
name, err := NewName(namestr)
if err != nil {
return
}
factory, ok := n.factories[name]
if !ok {
err = errors.NewNotFoundf(
errors.CodeNotFound,
"factory %q not found or not registered",
name,
)
return
}
t = factory
return
}
// Add adds a factory to the NamedMap.
// It returns an error if the factory already exists.
func (n *NamedMap[T]) Add(factory T) (err error) {
name := factory.Name()
if _, ok := n.factories[name]; ok {
return errors.Newf(
errors.TypeAlreadyExists,
errors.CodeAlreadyExists,
"factory %q already exists",
name,
)
}
n.factories[name] = factory
n.factoriesInOrder = append(n.factoriesInOrder, factory)
return nil
}
// GetInOrder returns the factories in the order they were added.
func (n *NamedMap[T]) GetInOrder() []T {
return n.factoriesInOrder
}