github.com/cs3org/reva/v2@v2.27.7/pkg/appctx/appctx.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package appctx
    20  
    21  import (
    22  	"context"
    23  
    24  	rtrace "github.com/cs3org/reva/v2/pkg/trace"
    25  	"github.com/go-chi/chi/v5/middleware"
    26  	"github.com/rs/zerolog"
    27  	"go.opentelemetry.io/otel/trace"
    28  )
    29  
    30  // deletingSharedResource flags to a storage a shared resource is being deleted not by the owner.
    31  type deletingSharedResource struct{}
    32  
    33  func WithDeletingSharedResource(ctx context.Context) context.Context {
    34  	return context.WithValue(ctx, deletingSharedResource{}, struct{}{})
    35  }
    36  func DeletingSharedResourceFromContext(ctx context.Context) bool {
    37  	return ctx.Value(deletingSharedResource{}) != nil
    38  }
    39  
    40  // WithLogger returns a context with an associated logger.
    41  func WithLogger(ctx context.Context, l *zerolog.Logger) context.Context {
    42  	return l.WithContext(ctx)
    43  }
    44  
    45  // GetLogger returns the logger associated with the given context
    46  // or a disabled logger in case no logger is stored inside the context.
    47  func GetLogger(ctx context.Context) *zerolog.Logger {
    48  	logger := zerolog.Ctx(ctx)
    49  	reqID := middleware.GetReqID(ctx)
    50  
    51  	if reqID != "" {
    52  		sublogger := logger.With().Str("request-id", reqID).Logger()
    53  		logger = &sublogger
    54  	}
    55  
    56  	return logger
    57  }
    58  
    59  // WithTracerProvider returns a context with an associated TracerProvider
    60  func WithTracerProvider(ctx context.Context, p trace.TracerProvider) context.Context {
    61  	return rtrace.ContextSetTracerProvider(ctx, p)
    62  }
    63  
    64  // GetTracerProvider returns the TracerProvider associated with
    65  // the given context. (Or the global default TracerProvider if there
    66  // is no TracerProvider in the context)
    67  func GetTracerProvider(ctx context.Context) trace.TracerProvider {
    68  	return rtrace.ContextGetTracerProvider(ctx)
    69  }