github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/store/layer_generators/opentracing_layer.go.tmpl (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  // Code generated by "make store-layers"
     5  // DO NOT EDIT
     6  
     7  package store
     8  
     9  import (
    10  	"context"
    11  
    12  	"github.com/vnforks/kid/v5/model"
    13  	"github.com/vnforks/kid/v5/services/tracing"
    14  	"github.com/opentracing/opentracing-go/ext"
    15  	spanlog "github.com/opentracing/opentracing-go/log"
    16  )
    17  
    18  type {{.Name}} struct {
    19  	Store
    20  {{range $index, $element := .SubStores}}	{{$index}}Store {{$index}}Store
    21  {{end}}
    22  }
    23  
    24  {{range $index, $element := .SubStores}}func (s *{{$.Name}}) {{$index}}() {{$index}}Store {
    25  	return s.{{$index}}Store
    26  }
    27  
    28  {{end}}
    29  
    30  {{range $index, $element := .SubStores}}type {{$.Name}}{{$index}}Store struct {
    31  	{{$index}}Store
    32  	Root *{{$.Name}}
    33  }
    34  
    35  {{end}}
    36  
    37  {{range $substoreName, $substore := .SubStores}}
    38  {{range $index, $element := $substore.Methods}}
    39  func (s *{{$.Name}}{{$substoreName}}Store) {{$index}}({{$element.Params | joinParamsWithType}}) {{$element.Results | joinResultsForSignature}} {
    40  	origCtx := s.Root.Store.Context()
    41  	span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "{{$substoreName}}Store.{{$index}}")
    42  	s.Root.Store.SetContext(newCtx)
    43  	defer func(){
    44  		s.Root.Store.SetContext(origCtx)
    45  	}()
    46  	{{range $paramName, $param := $element.Params}}
    47  		{{if index $element.ParamsToTrace $param.Name }}
    48  			span.SetTag("{{$param.Name}}", {{$param.Name}})
    49  		{{end}}
    50  	{{end}}
    51  	defer span.Finish()
    52  	{{- if $element.Results | len | eq 0}}
    53  	s.{{$substoreName}}Store.{{$index}}({{$element.Params | joinParams}})
    54  	{{else}}
    55  		{{$element.Results | genResultsVars}} := s.{{$substoreName}}Store.{{$index}}({{$element.Params | joinParams}})
    56  		{{- if $element.Results | errorPresent }}
    57  			if {{$element.Results | errorVar}} != nil {
    58  				span.LogFields(spanlog.Error({{$element.Results | errorVar}}))
    59  				ext.Error.Set(span, true)
    60  			}
    61  		{{end}}	
    62  		return {{ $element.Results | genResultsVars -}}
    63  	{{end}}
    64  }
    65  {{end}}
    66  {{end}}
    67  
    68  {{range $index, $element := .Methods}}
    69  func (s *{{$.Name}}) {{$index}}({{$element.Params | joinParamsWithType}}) {{$element.Results | joinResultsForSignature}} {
    70  	{{if $element.Results | len | eq 0}}s.Store.{{$index}}({{$element.Params | joinParams}})
    71  	{{else}}return s.Store.{{$index}}({{$element.Params | joinParams}})
    72  	{{end}}}
    73  {{end}}
    74  
    75  func New{{.Name}}(childStore Store, ctx context.Context) *{{.Name}} {
    76  	newStore := {{.Name}}{
    77  		Store: childStore,
    78  	}
    79  	{{range $substoreName, $substore := .SubStores}}
    80  	newStore.{{$substoreName}}Store = &{{$.Name}}{{$substoreName}}Store{{"{"}}{{$substoreName}}Store: childStore.{{$substoreName}}(), Root: &newStore}{{end}}
    81  	return &newStore
    82  }