Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

78 Zeilen
2.1 KiB

  1. // Copyright 2018, 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. package main
  15. import (
  16. "context"
  17. "log"
  18. "time"
  19. openzipkin "github.com/openzipkin/zipkin-go"
  20. "github.com/openzipkin/zipkin-go/reporter/http"
  21. "go.opencensus.io/exporter/zipkin"
  22. "go.opencensus.io/trace"
  23. )
  24. func main() {
  25. // The localEndpoint stores the name and address of the local service
  26. localEndpoint, err := openzipkin.NewEndpoint("example-server", "192.168.1.5:5454")
  27. if err != nil {
  28. log.Println(err)
  29. }
  30. // The Zipkin reporter takes collected spans from the app and reports them to the backend
  31. // http://localhost:9411/api/v2/spans is the default for the Zipkin Span v2
  32. reporter := http.NewReporter("http://localhost:9411/api/v2/spans")
  33. defer reporter.Close()
  34. // The OpenCensus exporter wraps the Zipkin reporter
  35. exporter := zipkin.NewExporter(reporter, localEndpoint)
  36. trace.RegisterExporter(exporter)
  37. // For example purposes, sample every trace. In a production application, you should
  38. // configure this to a trace.ProbabilitySampler set at the desired
  39. // probability.
  40. trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
  41. ctx := context.Background()
  42. foo(ctx)
  43. }
  44. func foo(ctx context.Context) {
  45. // Name the current span "/foo"
  46. ctx, span := trace.StartSpan(ctx, "/foo")
  47. defer span.End()
  48. // Foo calls bar and baz
  49. bar(ctx)
  50. baz(ctx)
  51. }
  52. func bar(ctx context.Context) {
  53. ctx, span := trace.StartSpan(ctx, "/bar")
  54. defer span.End()
  55. // Do bar
  56. time.Sleep(2 * time.Millisecond)
  57. }
  58. func baz(ctx context.Context) {
  59. ctx, span := trace.StartSpan(ctx, "/baz")
  60. defer span.End()
  61. // Do baz
  62. time.Sleep(4 * time.Millisecond)
  63. }