Skip to content

Commit

Permalink
feat: es trace
Browse files Browse the repository at this point in the history
  • Loading branch information
fuqihao.foliet committed Dec 9, 2023
1 parent fcd0eb0 commit 9bb35c2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
6 changes: 6 additions & 0 deletions biz/infrastructure/mapper/cat/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/sortorder"
"github.com/mitchellh/mapstructure"
"github.com/zeromicro/go-zero/core/trace"
"go.mongodb.org/mongo-driver/bson/primitive"
oteltrace "go.opentelemetry.io/otel/trace"

"github.com/xh-polaris/meowchat-content/biz/infrastructure/config"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/consts"
Expand Down Expand Up @@ -50,6 +52,10 @@ func NewEsMapper(config *config.Config) IEsMapper {
}

func (m *EsMapper) Search(ctx context.Context, communityId, keyword string, skip, count int) ([]*Cat, int64, error) {
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "elasticsearch/Search", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient))
defer func() {
span.End(oteltrace.WithTimestamp(time.Now()))
}()
res, err := m.es.Search().From(skip).Size(count).Index(m.indexName).Request(&search.Request{
Query: &types.Query{
Bool: &types.BoolQuery{
Expand Down
17 changes: 13 additions & 4 deletions biz/infrastructure/mapper/moment/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import (
"net/http"
"time"

"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/typedapi/core/count"
"github.com/elastic/go-elasticsearch/v8/typedapi/core/search"
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
"github.com/mitchellh/mapstructure"
"github.com/xh-polaris/gopkg/pagination"
"github.com/xh-polaris/gopkg/pagination/esp"
"github.com/zeromicro/go-zero/core/trace"
"go.mongodb.org/mongo-driver/bson/primitive"
oteltrace "go.opentelemetry.io/otel/trace"

"github.com/xh-polaris/meowchat-content/biz/infrastructure/config"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/consts"

"github.com/elastic/go-elasticsearch/v8"
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type (
Expand Down Expand Up @@ -54,6 +55,10 @@ func NewEsMapper(config *config.Config) IEsMapper {
}

func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter esp.EsCursor) ([]*Moment, int64, error) {
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "elasticsearch/Search", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient))
defer func() {
span.End(oteltrace.WithTimestamp(time.Now()))
}()
p := esp.NewEsPaginator(pagination.NewRawStore(sorter), popts)
s, sa, err := p.MakeSortOptions(ctx)
if err != nil {
Expand Down Expand Up @@ -119,6 +124,10 @@ func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *Filte
}

func (m *EsMapper) CountWithQuery(ctx context.Context, query []types.Query, fopts *FilterOptions) (int64, error) {
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "elasticsearch/Count", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient))
defer func() {
span.End(oteltrace.WithTimestamp(time.Now()))
}()
f := makeEsFilter(fopts)
res, err := m.es.Count().Index(m.indexName).Request(&count.Request{
Query: &types.Query{
Expand Down
10 changes: 10 additions & 0 deletions biz/infrastructure/mapper/plan/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
"github.com/xh-polaris/gopkg/pagination"
"github.com/xh-polaris/gopkg/pagination/esp"
"github.com/zeromicro/go-zero/core/trace"
oteltrace "go.opentelemetry.io/otel/trace"

"github.com/xh-polaris/meowchat-content/biz/infrastructure/config"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/consts"
Expand Down Expand Up @@ -54,6 +56,10 @@ func NewEsMapper(config *config.Config) IEsMapper {
}

func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter esp.EsCursor) ([]*Plan, int64, error) {
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "elasticsearch/Search", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient))
defer func() {
span.End(oteltrace.WithTimestamp(time.Now()))
}()
p := esp.NewEsPaginator(pagination.NewRawStore(sorter), popts)
s, sa, err := p.MakeSortOptions(ctx)
if err != nil {
Expand Down Expand Up @@ -125,6 +131,10 @@ func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *Filte
}

func (m *EsMapper) CountWithQuery(ctx context.Context, query []types.Query, fopts *FilterOptions) (int64, error) {
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "elasticsearch/Conut", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient))
defer func() {
span.End(oteltrace.WithTimestamp(time.Now()))
}()
f := makeEsFilter(fopts)
res, err := m.es.Count().Index(m.indexName).Request(&count.Request{
Query: &types.Query{
Expand Down
10 changes: 10 additions & 0 deletions biz/infrastructure/mapper/post/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
"github.com/mitchellh/mapstructure"
"github.com/xh-polaris/gopkg/pagination"
"github.com/xh-polaris/gopkg/pagination/esp"
"github.com/zeromicro/go-zero/core/trace"
"go.mongodb.org/mongo-driver/bson/primitive"
oteltrace "go.opentelemetry.io/otel/trace"

"github.com/xh-polaris/meowchat-content/biz/infrastructure/config"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/consts"
Expand Down Expand Up @@ -53,6 +55,10 @@ func NewEsMapper(config *config.Config) IEsMapper {
}

func (m *EsMapper) CountWithQuery(ctx context.Context, query []types.Query, fopts *FilterOptions) (int64, error) {
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "elasticsearch/Count", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient))
defer func() {
span.End(oteltrace.WithTimestamp(time.Now()))
}()
filter := newPostFilter(fopts)
res, err := m.es.Count().Index(m.indexName).Request(&count.Request{
Query: &types.Query{
Expand All @@ -70,6 +76,10 @@ func (m *EsMapper) CountWithQuery(ctx context.Context, query []types.Query, fopt
}

func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter esp.EsCursor) ([]*Post, int64, error) {
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "elasticsearch/Search", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient))
defer func() {
span.End(oteltrace.WithTimestamp(time.Now()))
}()
p := esp.NewEsPaginator(pagination.NewRawStore(sorter), popts)
filter := newPostFilter(fopts)
s, sa, err := p.MakeSortOptions(ctx)
Expand Down

0 comments on commit 9bb35c2

Please sign in to comment.