Browse Source

init meter provider

master v0.0.1
andrey 8 months ago
parent
commit
55fdd0ca17
  1. 17
      go.mod
  2. 27
      go.sum
  3. 115
      meter/provider.go

17
go.mod

@ -0,0 +1,17 @@
module gitoa.ru/go-4devs/otel
go 1.22.2
require (
go.opentelemetry.io/otel v1.25.0
go.opentelemetry.io/otel/sdk v1.25.0
go.opentelemetry.io/otel/sdk/metric v1.25.0
)
require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel/metric v1.25.0 // indirect
go.opentelemetry.io/otel/trace v1.25.0 // indirect
golang.org/x/sys v0.18.0 // indirect
)

27
go.sum

@ -0,0 +1,27 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.opentelemetry.io/otel v1.25.0 h1:gldB5FfhRl7OJQbUHt/8s0a7cE8fbsPAtdpRaApKy4k=
go.opentelemetry.io/otel v1.25.0/go.mod h1:Wa2ds5NOXEMkCmUou1WA7ZBfLTHWIsp034OVD7AO+Vg=
go.opentelemetry.io/otel/metric v1.25.0 h1:LUKbS7ArpFL/I2jJHdJcqMGxkRdxpPHE0VU/D4NuEwA=
go.opentelemetry.io/otel/metric v1.25.0/go.mod h1:rkDLUSd2lC5lq2dFNrX9LGAbINP5B7WBkC78RXCpH5s=
go.opentelemetry.io/otel/sdk v1.25.0 h1:PDryEJPC8YJZQSyLY5eqLeafHtG+X7FWnf3aXMtxbqo=
go.opentelemetry.io/otel/sdk v1.25.0/go.mod h1:oFgzCM2zdsxKzz6zwpTZYLLQsFwc+K0daArPdIhuxkw=
go.opentelemetry.io/otel/sdk/metric v1.25.0 h1:7CiHOy08LbrxMAp4vWpbiPcklunUshVpAvGBrdDRlGw=
go.opentelemetry.io/otel/sdk/metric v1.25.0/go.mod h1:LzwoKptdbBBdYfvtGCzGwk6GWMA3aUzBOwtQpR6Nz7o=
go.opentelemetry.io/otel/trace v1.25.0 h1:tqukZGLwQYRIFtSQM2u2+yfMVTgGVeqRLPUYx1Dq6RM=
go.opentelemetry.io/otel/trace v1.25.0/go.mod h1:hCCs70XM/ljO+BeQkyFnbK28SBIJ/Emuha+ccrCRT7I=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

115
meter/provider.go

@ -0,0 +1,115 @@
package meter
import (
"context"
"fmt"
"os"
"path/filepath"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
)
func WithServiceName(name string) func(*Option) error {
return func(o *Option) error {
o.serviceName = name
return nil
}
}
func WithAttributes(attrs ...attribute.KeyValue) func(context.Context, *Option) error {
return func(_ context.Context, o *Option) error {
o.attributes = attrs
return nil
}
}
func WithExtraResource(opts ...resource.Option) func(context.Context, *Option) error {
return func(_ context.Context, o *Option) error {
o.extraResource = opts
return nil
}
}
func WithReader(reader metric.Reader) func(context.Context, *Option) error {
return func(_ context.Context, o *Option) error {
o.reader = reader
return nil
}
}
type Option struct {
reader metric.Reader
serviceName string
attributes []attribute.KeyValue
extraResource []resource.Option
}
func newOptions(ctx context.Context, opts ...func(context.Context, *Option) error) (Option, error) {
option := Option{
serviceName: filepath.Base(os.Args[0]),
extraResource: []resource.Option{
resource.WithOS(),
resource.WithProcess(),
resource.WithContainer(),
resource.WithHost(),
},
}
for idx, opt := range opts {
if err := opt(ctx, &option); err != nil {
return option, fmt.Errorf("option[%d]:%w", idx, err)
}
}
return option, nil
}
func New(ctx context.Context, opts ...func(context.Context, *Option) error) (*metric.MeterProvider, error) {
option, err := newOptions(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("configure:%w", err)
}
extraResources, eerr := resource.New(
ctx,
append(
option.extraResource,
resource.WithAttributes(
append(
option.attributes,
semconv.ServiceName(option.serviceName),
)...,
),
)...,
)
if eerr != nil {
return nil, fmt.Errorf("create extra resource: %w", eerr)
}
res, rerr := resource.Merge(
resource.Default(),
extraResources,
)
if rerr != nil {
return nil, fmt.Errorf("merge resource: %w", rerr)
}
provOption := make([]metric.Option, 0, 2)
if option.reader != nil {
provOption = append(provOption, metric.WithReader(option.reader))
}
meterProvider := metric.NewMeterProvider(append(provOption, metric.WithResource(res))...)
otel.SetMeterProvider(meterProvider)
return meterProvider, nil
}
Loading…
Cancel
Save