v1 Publish a message with OpenTelemetry tracing enabled (DEPRECATED)

(DEPRECATED) Publish a message with OpenTelemetry tracing enabled

Code sample

Go

Before trying this sample, follow the Go setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Go API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 "cloud.google.com/go/pubsub" 
  
 "go.opentelemetry.io/otel" 
  
 "google.golang.org/api/option" 
  
 texporter 
  
 "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace" 
  
 "go.opentelemetry.io/otel/sdk/resource" 
  
 sdktrace 
  
 "go.opentelemetry.io/otel/sdk/trace" 
  
 semconv 
  
 "go.opentelemetry.io/otel/semconv/v1.26.0" 
 ) 
 // publishOpenTelemetryTracing publishes a single message with OpenTelemetry tracing 
 // enabled, exporting to Cloud Trace. 
 func 
  
 publishOpenTelemetryTracing 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 topicID 
  
 string 
 , 
  
 sampling 
  
 float64 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // topicID := "my-topic" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 exporter 
 , 
  
 err 
  
 := 
  
 texporter 
 . 
 New 
 ( 
 texporter 
 . 
 WithProjectID 
 ( 
 projectID 
 ), 
  
 // Disable spans created by the exporter. 
  
 texporter 
 . 
 WithTraceClientOptions 
 ( 
  
 [] 
 option 
 . 
 ClientOption 
 { 
 option 
 . 
 WithTelemetryDisabled 
 ()}, 
  
 ), 
  
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "error instantiating exporter: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 resources 
  
 := 
  
 resource 
 . 
 NewWithAttributes 
 ( 
  
 semconv 
 . 
 SchemaURL 
 , 
  
 semconv 
 . 
 ServiceNameKey 
 . 
 String 
 ( 
 "publisher" 
 ), 
  
 ) 
  
 // Instantiate a tracer provider with the following settings 
  
 tp 
  
 := 
  
 sdktrace 
 . 
 NewTracerProvider 
 ( 
  
 sdktrace 
 . 
 WithBatcher 
 ( 
 exporter 
 ), 
  
 sdktrace 
 . 
 WithResource 
 ( 
 resources 
 ), 
  
 sdktrace 
 . 
 WithSampler 
 ( 
  
 sdktrace 
 . 
 ParentBased 
 ( 
 sdktrace 
 . 
 TraceIDRatioBased 
 ( 
 sampling 
 )), 
  
 ), 
  
 ) 
  
 defer 
  
 tp 
 . 
 ForceFlush 
 ( 
 ctx 
 ) 
  
 // flushes any pending spans 
  
 otel 
 . 
 SetTracerProvider 
 ( 
 tp 
 ) 
  
 // Create a new client with tracing enabled. 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
  NewClientWithConfig 
 
 ( 
 ctx 
 , 
  
 projectID 
 , 
  
& pubsub 
 . 
  ClientConfig 
 
 { 
  
 EnableOpenTelemetryTracing 
 : 
  
 true 
 , 
  
 }) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub: NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 t 
  
 := 
  
 client 
 . 
 Topic 
 ( 
 topicID 
 ) 
  
 result 
  
 := 
  
 t 
 . 
 Publish 
 ( 
 ctx 
 , 
  
& pubsub 
 . 
 Message 
 { 
  
 Data 
 : 
  
 [] 
 byte 
 ( 
 "Publishing message with tracing" 
 ), 
  
 }) 
  
 if 
  
 _ 
 , 
  
 err 
  
 := 
  
 result 
 . 
 Get 
 ( 
 ctx 
 ); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub: result.Get: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintln 
 ( 
 w 
 , 
  
 "Published a traced message" 
 ) 
  
 return 
  
 nil 
 } 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Create a Mobile Website
View Site in Mobile | Classic
Share by: