Skip to content

Commit ee2a4f8

Browse files
committed
Updated Logging
1 parent e957460 commit ee2a4f8

3 files changed

Lines changed: 102 additions & 33 deletions

File tree

cmd/main.go

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/matt-gp/otel-lgtm-proxy/internal/otel"
1818
"github.com/matt-gp/otel-lgtm-proxy/internal/util/cert"
1919
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
20+
"go.opentelemetry.io/otel/log"
2021
)
2122

2223
func main() {
@@ -41,7 +42,13 @@ func main() {
4142
tracerProvider := provider.TracerProvider.Tracer("traces")
4243

4344
// Start application
44-
logger.Info(ctx, loggingProvider, "Starting application")
45+
logger.Info(
46+
ctx,
47+
loggingProvider,
48+
"Starting application",
49+
log.KeyValue{Key: "service_name", Value: log.StringValue(cfg.Service.Name)},
50+
log.KeyValue{Key: "service_version", Value: log.StringValue(cfg.Service.Version)},
51+
)
4552

4653
// Initialize signal handling
4754
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
@@ -74,16 +81,34 @@ func main() {
7481
})
7582

7683
// register the logs handler.
77-
logger.Info(ctx, loggingProvider, "receiving logs on /v1/logs")
78-
router.HandleFunc("POST /v1/logs", h.Logs)
84+
logsEndpoint := "/v1/logs"
85+
logger.Info(
86+
ctx,
87+
loggingProvider,
88+
"receiving logs",
89+
log.KeyValue{Key: "endpoint", Value: log.StringValue(logsEndpoint)},
90+
)
91+
router.HandleFunc("POST "+logsEndpoint, h.Logs)
7992

8093
// register the metrics handler.
81-
logger.Info(ctx, loggingProvider, "receiving metrics on /v1/metrics")
82-
router.HandleFunc("POST /v1/metrics", h.Metrics)
94+
metricsEndpoint := "/v1/metrics"
95+
logger.Info(
96+
ctx,
97+
loggingProvider,
98+
"receiving metrics",
99+
log.KeyValue{Key: "endpoint", Value: log.StringValue(metricsEndpoint)},
100+
)
101+
router.HandleFunc("POST "+metricsEndpoint, h.Metrics)
83102

84103
// register the traces handler.
85-
logger.Info(ctx, loggingProvider, "receiving traces on /v1/traces")
86-
router.HandleFunc("POST /v1/traces", h.Traces)
104+
tracesEndpoint := "/v1/traces"
105+
logger.Info(
106+
ctx,
107+
loggingProvider,
108+
"receiving traces",
109+
log.KeyValue{Key: "endpoint", Value: log.StringValue(tracesEndpoint)},
110+
)
111+
router.HandleFunc("POST "+tracesEndpoint, h.Traces)
87112

88113
// Initialize TLS configuration
89114
tlsConfig := &tls.Config{
@@ -94,14 +119,24 @@ func main() {
94119
if cert.TLSEnabled(&cfg.HTTP.TLS) {
95120
certPair, err := tls.LoadX509KeyPair(cfg.HTTP.TLS.CertFile, cfg.HTTP.TLS.KeyFile)
96121
if err != nil {
97-
logger.Error(ctx, loggingProvider, err.Error())
122+
logger.Error(
123+
ctx,
124+
loggingProvider,
125+
"unable to read certificate or key file",
126+
log.KeyValue{Key: "error", Value: log.StringValue(err.Error())},
127+
)
98128
os.Exit(1)
99129
}
100130

101131
caPool := x509.NewCertPool()
102132
caCert, err := os.ReadFile(cfg.HTTP.TLS.CAFile)
103133
if err != nil {
104-
logger.Error(ctx, loggingProvider, err.Error())
134+
logger.Error(
135+
ctx,
136+
loggingProvider,
137+
"unable to read CA file",
138+
log.KeyValue{Key: "error", Value: log.StringValue(err.Error())},
139+
)
105140
os.Exit(1)
106141
}
107142

@@ -124,7 +159,9 @@ func main() {
124159
logger.Info(
125160
ctx,
126161
loggingProvider,
127-
fmt.Sprintf("starting https server on %s", cfg.HTTP.Address),
162+
"starting server",
163+
log.KeyValue{Key: "address", Value: log.StringValue(cfg.HTTP.Address)},
164+
log.KeyValue{Key: "tls_enabled", Value: log.BoolValue(true)},
128165
)
129166
if err := server.ListenAndServeTLS("", ""); err != nil {
130167
logger.Error(ctx, loggingProvider, err.Error())
@@ -134,7 +171,9 @@ func main() {
134171
logger.Info(
135172
ctx,
136173
loggingProvider,
137-
fmt.Sprintf("starting http server on %s", cfg.HTTP.Address),
174+
"starting server",
175+
log.KeyValue{Key: "address", Value: log.StringValue(cfg.HTTP.Address)},
176+
log.KeyValue{Key: "tls_enabled", Value: log.BoolValue(false)},
138177
)
139178
if err := server.ListenAndServe(); err != nil {
140179
logger.Error(ctx, loggingProvider, err.Error())

internal/processor/processor.go

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func (p *Processor[T]) Partition(ctx context.Context, resources []T) map[string]
202202

203203
// Dispatch sends all the requests to the target.
204204
func (p *Processor[T]) Dispatch(ctx context.Context, tenantMap map[string][]T) error {
205-
waitGroup := sync.WaitGroup{}
205+
wg := sync.WaitGroup{}
206206

207207
for tenant, resources := range tenantMap {
208208
ctx, span := p.tracer.Start(
@@ -215,10 +215,10 @@ func (p *Processor[T]) Dispatch(ctx context.Context, tenantMap map[string][]T) e
215215
)
216216
defer span.End()
217217

218-
waitGroup.Add(1)
218+
wg.Add(1)
219219

220220
go func(tenant string, resources []T) {
221-
defer waitGroup.Done()
221+
defer wg.Done()
222222

223223
tenantAttribute := attribute.String("signal.tenant", tenant)
224224

@@ -245,16 +245,18 @@ func (p *Processor[T]) Dispatch(ctx context.Context, tenantMap map[string][]T) e
245245
return
246246
}
247247

248+
signalResponseStatusCodeAttr := attribute.String(
249+
"signal.response.status.code",
250+
strconv.Itoa(resp.StatusCode),
251+
)
252+
248253
p.proxyRecordsMetric.Add(
249254
ctx,
250255
int64(len(resources)),
251256
metric.WithAttributes(
252257
p.signalTypeAttr(),
253258
tenantAttribute,
254-
attribute.String(
255-
"signal.response.status.code",
256-
strconv.Itoa(resp.StatusCode),
257-
),
259+
signalResponseStatusCodeAttr,
258260
),
259261
)
260262

@@ -264,37 +266,37 @@ func (p *Processor[T]) Dispatch(ctx context.Context, tenantMap map[string][]T) e
264266
metric.WithAttributes(
265267
p.signalTypeAttr(),
266268
tenantAttribute,
267-
attribute.String(
268-
"signal.response.status.code",
269-
strconv.Itoa(resp.StatusCode),
270-
),
269+
signalResponseStatusCodeAttr,
271270
),
272271
)
273272

274273
logger.Debug(
275274
ctx,
276275
p.logger,
277276
fmt.Sprintf(
278-
"sent %d records status %d for tenant %s",
277+
"sent %d records",
279278
len(resources),
280-
resp.StatusCode,
281-
tenant,
282279
),
283280
p.signalTypeLogAttr(),
281+
log.KeyValueFromAttribute(tenantAttribute),
282+
log.KeyValueFromAttribute(signalResponseStatusCodeAttr),
284283
)
285284

286285
logger.Trace(
287286
ctx,
288287
p.logger,
289288
fmt.Sprintf("%+v", resources),
290289
p.signalTypeLogAttr(),
290+
log.KeyValueFromAttribute(tenantAttribute),
291+
log.KeyValueFromAttribute(signalResponseStatusCodeAttr),
291292
)
292293

293294
span.SetStatus(codes.Ok, "sent successfully")
294295
}(tenant, resources)
295296
}
296297

297-
waitGroup.Wait()
298+
wg.Wait()
299+
298300
return nil
299301
}
300302

@@ -306,11 +308,13 @@ func (p *Processor[T]) send(
306308
) (http.Response, error) {
307309
start := time.Now()
308310

311+
signalTenantAttr := attribute.String("signal.tenant", tenant)
312+
309313
ctx, span := p.tracer.Start(ctx,
310314
fmt.Sprintf("%s.send", p.signalType),
311315
trace.WithAttributes(
312316
p.signalTypeAttr(),
313-
attribute.String("signal.tenant", tenant),
317+
signalTenantAttr,
314318
attribute.Int("signal.tenant.records", len(resources)),
315319
),
316320
)
@@ -319,6 +323,15 @@ func (p *Processor[T]) send(
319323
// Marshal resources to bytes
320324
body, err := p.marshalResources(resources)
321325
if err != nil {
326+
logger.Error(
327+
ctx,
328+
p.logger,
329+
err.Error(),
330+
p.signalTypeLogAttr(),
331+
log.KeyValueFromAttribute(signalTenantAttr),
332+
)
333+
span.RecordError(err)
334+
span.SetStatus(codes.Error, "failed to marshal data")
322335
return http.Response{}, fmt.Errorf("failed to marshal data: %w", err)
323336
}
324337

@@ -329,6 +342,15 @@ func (p *Processor[T]) send(
329342
io.NopCloser(bytes.NewReader(body)),
330343
)
331344
if err != nil {
345+
logger.Error(
346+
ctx,
347+
p.logger,
348+
err.Error(),
349+
p.signalTypeLogAttr(),
350+
log.KeyValueFromAttribute(signalTenantAttr),
351+
)
352+
span.RecordError(err)
353+
span.SetStatus(codes.Error, "failed to create request")
332354
return http.Response{}, fmt.Errorf("failed to create request: %w", err)
333355
}
334356

@@ -341,6 +363,13 @@ func (p *Processor[T]) send(
341363

342364
resp, err := p.client.Do(req)
343365
if err != nil {
366+
logger.Error(
367+
ctx,
368+
p.logger,
369+
err.Error(),
370+
p.signalTypeLogAttr(),
371+
log.KeyValueFromAttribute(signalTenantAttr),
372+
)
344373
span.RecordError(err)
345374
span.SetStatus(codes.Error, "failed to send")
346375
return http.Response{}, fmt.Errorf("failed to send request: %w", err)
@@ -354,26 +383,27 @@ func (p *Processor[T]) send(
354383
p.logger,
355384
fmt.Sprintf("failed to close response body: %v", closeErr),
356385
p.signalTypeLogAttr(),
386+
log.KeyValueFromAttribute(signalTenantAttr),
357387
)
358388
span.RecordError(closeErr)
359389
span.SetStatus(codes.Error, "failed to close response body")
360390
}
361391
}()
362392

363-
respAttr := attribute.String(
393+
signalResponseStatusCodeAttr := attribute.String(
364394
"signal.response.status.code",
365395
strconv.Itoa(resp.StatusCode),
366396
)
367397

368-
span.SetAttributes(respAttr)
398+
span.SetAttributes(signalResponseStatusCodeAttr)
369399
span.SetStatus(codes.Ok, "sent successfully")
370400

371401
p.proxyLatencyMetric.Record(ctx,
372402
time.Since(start).Milliseconds(),
373403
metric.WithAttributes(
374-
respAttr,
404+
signalResponseStatusCodeAttr,
375405
p.signalTypeAttr(),
376-
attribute.String("signal.tenant", tenant),
406+
signalTenantAttr,
377407
),
378408
)
379409

internal/util/request/request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ func AddHeaders(tenant string, req *http.Request, config *config.Config, headers
1515
req.Header.Add(config.Tenant.Header, fmt.Sprintf(config.Tenant.Format, tenant))
1616

1717
// Add custom headers
18-
customHeaders := strings.Split(headers, ",")
19-
for _, customHeader := range customHeaders {
18+
customHeaders := strings.SplitSeq(headers, ",")
19+
for customHeader := range customHeaders {
2020
kv := strings.SplitN(customHeader, "=", 2)
2121
if len(kv) == 2 {
2222
req.Header.Add(kv[0], kv[1])

0 commit comments

Comments
 (0)