...

Source file src/go.opencensus.io/tag/example_test.go

Documentation: go.opencensus.io/tag

     1  // Copyright 2017, OpenCensus Authors
     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  
    16  package tag_test
    17  
    18  import (
    19  	"context"
    20  	"log"
    21  
    22  	"go.opencensus.io/tag"
    23  )
    24  
    25  var (
    26  	tagMap *tag.Map
    27  	ctx    context.Context
    28  	key    tag.Key
    29  )
    30  
    31  func ExampleNewKey() {
    32  	// Get a key to represent user OS.
    33  	key, err := tag.NewKey("example.com/keys/user-os")
    34  	if err != nil {
    35  		log.Fatal(err)
    36  	}
    37  	_ = key // use key
    38  }
    39  
    40  func ExampleMustNewKey() {
    41  	key := tag.MustNewKey("example.com/keys/user-os")
    42  	_ = key // use key
    43  }
    44  
    45  func ExampleNew() {
    46  	osKey := tag.MustNewKey("example.com/keys/user-os")
    47  	userIDKey := tag.MustNewKey("example.com/keys/user-id")
    48  
    49  	ctx, err := tag.New(ctx,
    50  		tag.Insert(osKey, "macOS-10.12.5"),
    51  		tag.Upsert(userIDKey, "cde36753ed"),
    52  	)
    53  	if err != nil {
    54  		log.Fatal(err)
    55  	}
    56  
    57  	_ = ctx // use context
    58  }
    59  
    60  func ExampleNew_replace() {
    61  	ctx, err := tag.New(ctx,
    62  		tag.Insert(key, "macOS-10.12.5"),
    63  		tag.Upsert(key, "macOS-10.12.7"),
    64  	)
    65  	if err != nil {
    66  		log.Fatal(err)
    67  	}
    68  
    69  	_ = ctx // use context
    70  }
    71  
    72  func ExampleNewContext() {
    73  	// Propagate the tag map in the current context.
    74  	ctx := tag.NewContext(context.Background(), tagMap)
    75  
    76  	_ = ctx // use context
    77  }
    78  
    79  func ExampleFromContext() {
    80  	tagMap := tag.FromContext(ctx)
    81  
    82  	_ = tagMap // use the tag map
    83  }
    84  
    85  func ExampleDo() {
    86  	ctx, err := tag.New(ctx,
    87  		tag.Insert(key, "macOS-10.12.5"),
    88  		tag.Upsert(key, "macOS-10.12.7"),
    89  	)
    90  	if err != nil {
    91  		log.Fatal(err)
    92  	}
    93  	tag.Do(ctx, func(ctx context.Context) {
    94  		_ = ctx // use context
    95  	})
    96  }
    97  

View as plain text