| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package client_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"net" |
| 10 |
|
| 11 |
"go.opentelemetry.io/collector/client" |
| 12 |
"go.opentelemetry.io/collector/consumer" |
| 13 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 14 |
) |
| 15 |
|
| 16 |
func Example_receiver() { |
| 17 |
// Your receiver get a next consumer when it's constructed |
| 18 |
next, err := consumer.NewTraces(func(_ context.Context, _ ptrace.Traces) error { |
| 19 |
return nil |
| 20 |
}) |
| 21 |
if err != nil { |
| 22 |
panic(err) |
| 23 |
} |
| 24 |
|
| 25 |
// You'll convert the incoming data into pipeline data |
| 26 |
td := ptrace.NewTraces() |
| 27 |
|
| 28 |
// You probably have a context with client metadata from your listener or |
| 29 |
// scraper |
| 30 |
ctx := context.Background() |
| 31 |
|
| 32 |
// Get the client from the context: if it doesn't exist, FromContext will |
| 33 |
// create one |
| 34 |
cl := client.FromContext(ctx) |
| 35 |
|
| 36 |
// Extract the client information based on your original context and set it |
| 37 |
// to Addr |
| 38 |
//nolint:govet |
| 39 |
cl.Addr = &net.IPAddr{ |
| 40 |
IP: net.IPv4(1, 2, 3, 4), |
| 41 |
} |
| 42 |
|
| 43 |
// When you are done, propagate the context down the pipeline to the next |
| 44 |
// consumer and handle error. |
| 45 |
if err = next.ConsumeTraces(ctx, td); err != nil { |
| 46 |
panic(err) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func Example_processor() { |
| 51 |
// Your processor or exporter will receive a context, from which you get the |
| 52 |
// client information |
| 53 |
ctx := context.Background() |
| 54 |
cl := client.FromContext(ctx) |
| 55 |
|
| 56 |
// And use the information from the client as you need |
| 57 |
fmt.Println(cl.Addr) |
| 58 |
} |
| 59 |
|
| 60 |
func Example_authenticator() { |
| 61 |
// Your configauth.AuthenticateFunc receives a context |
| 62 |
ctx := context.Background() |
| 63 |
|
| 64 |
// Get the client from the context: if it doesn't exist, FromContext will |
| 65 |
// create one |
| 66 |
cl := client.FromContext(ctx) |
| 67 |
|
| 68 |
// After a successful authentication, place the data you want to propagate |
| 69 |
// as part of an AuthData implementation of your own |
| 70 |
cl.Auth = &exampleAuthData{ |
| 71 |
username: "jdoe", |
| 72 |
} |
| 73 |
|
| 74 |
// Your configauth.AuthenticateFunc should return this new context |
| 75 |
_ = client.NewContext(ctx, cl) |
| 76 |
} |
| 77 |
|
| 78 |
type exampleAuthData struct { |
| 79 |
username string |
| 80 |
} |
| 81 |
|
| 82 |
func (e *exampleAuthData) GetAttribute(key string) any { |
| 83 |
if key == "username" { |
| 84 |
return e.username |
| 85 |
} |
| 86 |
return nil |
| 87 |
} |
| 88 |
|
| 89 |
func (e *exampleAuthData) GetAttributeNames() []string { |
| 90 |
return []string{"username"} |
| 91 |
} |