| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package log_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/attribute" |
| 12 |
"go.opentelemetry.io/otel/log" |
| 13 |
) |
| 14 |
|
| 15 |
func TestNewLoggerConfig(t *testing.T) { |
| 16 |
version := "v1.1.1" |
| 17 |
schemaURL := "https://opentelemetry.io/schemas/1.37.0" |
| 18 |
attr := attribute.NewSet( |
| 19 |
attribute.String("user", "alice"), |
| 20 |
attribute.Bool("admin", true), |
| 21 |
) |
| 22 |
|
| 23 |
c := log.NewLoggerConfig( |
| 24 |
log.WithInstrumentationVersion(version), |
| 25 |
log.WithSchemaURL(schemaURL), |
| 26 |
log.WithInstrumentationAttributes(attr.ToSlice()...), |
| 27 |
) |
| 28 |
|
| 29 |
assert.Equal(t, version, c.InstrumentationVersion(), "instrumentation version") |
| 30 |
assert.Equal(t, schemaURL, c.SchemaURL(), "schema URL") |
| 31 |
assert.Equal(t, attr, c.InstrumentationAttributes(), "instrumentation attributes") |
| 32 |
} |
| 33 |
|
| 34 |
func TestWithInstrumentationAttributesNotLazy(t *testing.T) { |
| 35 |
attrs := []attribute.KeyValue{ |
| 36 |
attribute.String("service", "test"), |
| 37 |
attribute.Int("three", 3), |
| 38 |
} |
| 39 |
want := attribute.NewSet(attrs...) |
| 40 |
|
| 41 |
// WithInstrumentationAttributes is expected to immediately |
| 42 |
// create an immutable set from the attributes, so later changes |
| 43 |
// to attrs should not affect the config. |
| 44 |
opt := log.WithInstrumentationAttributes(attrs...) |
| 45 |
attrs[0] = attribute.String("service", "changed") |
| 46 |
|
| 47 |
c := log.NewLoggerConfig(opt) |
| 48 |
assert.Equal(t, want, c.InstrumentationAttributes(), "instrumentation attributes") |
| 49 |
} |
| 50 |
|
| 51 |
func TestWithInstrumentationAttributeSet(t *testing.T) { |
| 52 |
attrs := attribute.NewSet( |
| 53 |
attribute.String("service", "test"), |
| 54 |
attribute.Int("three", 3), |
| 55 |
) |
| 56 |
|
| 57 |
c := log.NewLoggerConfig( |
| 58 |
log.WithInstrumentationAttributeSet(attrs), |
| 59 |
) |
| 60 |
|
| 61 |
assert.Equal(t, attrs, c.InstrumentationAttributes(), "instrumentation attributes") |
| 62 |
} |
| 63 |
|
| 64 |
func TestWithInstrumentationAttributesMerge(t *testing.T) { |
| 65 |
aliceAttr := attribute.String("user", "Alice") |
| 66 |
bobAttr := attribute.String("user", "Bob") |
| 67 |
adminAttr := attribute.Bool("admin", true) |
| 68 |
|
| 69 |
alice := attribute.NewSet(aliceAttr) |
| 70 |
bob := attribute.NewSet(bobAttr) |
| 71 |
aliceAdmin := attribute.NewSet(aliceAttr, adminAttr) |
| 72 |
bobAdmin := attribute.NewSet(bobAttr, adminAttr) |
| 73 |
|
| 74 |
t.Run("SameKey", func(t *testing.T) { |
| 75 |
c := log.NewLoggerConfig( |
| 76 |
log.WithInstrumentationAttributes(aliceAttr), |
| 77 |
log.WithInstrumentationAttributes(bobAttr), |
| 78 |
) |
| 79 |
assert.Equal(t, bob, c.InstrumentationAttributes(), |
| 80 |
"Later values for the same key should overwrite earlier ones.") |
| 81 |
}) |
| 82 |
|
| 83 |
t.Run("DifferentKeys", func(t *testing.T) { |
| 84 |
c := log.NewLoggerConfig( |
| 85 |
log.WithInstrumentationAttributes(aliceAttr), |
| 86 |
log.WithInstrumentationAttributes(adminAttr), |
| 87 |
) |
| 88 |
assert.Equal(t, aliceAdmin, c.InstrumentationAttributes(), |
| 89 |
"Different keys should be merged.") |
| 90 |
}) |
| 91 |
|
| 92 |
t.Run("Mixed", func(t *testing.T) { |
| 93 |
c := log.NewLoggerConfig( |
| 94 |
log.WithInstrumentationAttributes(aliceAttr, adminAttr), |
| 95 |
log.WithInstrumentationAttributes(bobAttr), |
| 96 |
) |
| 97 |
assert.Equal(t, bobAdmin, c.InstrumentationAttributes(), |
| 98 |
"Combination of same and different keys should be merged.") |
| 99 |
}) |
| 100 |
|
| 101 |
t.Run("MergedEmpty", func(t *testing.T) { |
| 102 |
c := log.NewLoggerConfig( |
| 103 |
log.WithInstrumentationAttributes(aliceAttr), |
| 104 |
log.WithInstrumentationAttributes(), |
| 105 |
) |
| 106 |
assert.Equal(t, alice, c.InstrumentationAttributes(), |
| 107 |
"Empty attributes should not affect existing ones.") |
| 108 |
}) |
| 109 |
|
| 110 |
t.Run("SameKeyWithSet", func(t *testing.T) { |
| 111 |
c := log.NewLoggerConfig( |
| 112 |
log.WithInstrumentationAttributeSet(alice), |
| 113 |
log.WithInstrumentationAttributeSet(bob), |
| 114 |
) |
| 115 |
assert.Equal(t, bob, c.InstrumentationAttributes(), |
| 116 |
"Later values for the same key should overwrite earlier ones.") |
| 117 |
}) |
| 118 |
|
| 119 |
t.Run("DifferentKeysWithSet", func(t *testing.T) { |
| 120 |
c := log.NewLoggerConfig( |
| 121 |
log.WithInstrumentationAttributeSet(alice), |
| 122 |
log.WithInstrumentationAttributeSet(attribute.NewSet(adminAttr)), |
| 123 |
) |
| 124 |
assert.Equal(t, aliceAdmin, c.InstrumentationAttributes(), |
| 125 |
"Different keys should be merged.") |
| 126 |
}) |
| 127 |
|
| 128 |
t.Run("MixedWithSet", func(t *testing.T) { |
| 129 |
c := log.NewLoggerConfig( |
| 130 |
log.WithInstrumentationAttributeSet(aliceAdmin), |
| 131 |
log.WithInstrumentationAttributeSet(bob), |
| 132 |
) |
| 133 |
assert.Equal(t, bobAdmin, c.InstrumentationAttributes(), |
| 134 |
"Combination of same and different keys should be merged.") |
| 135 |
}) |
| 136 |
|
| 137 |
t.Run("MergedEmptyWithSet", func(t *testing.T) { |
| 138 |
c := log.NewLoggerConfig( |
| 139 |
log.WithInstrumentationAttributeSet(alice), |
| 140 |
log.WithInstrumentationAttributeSet(attribute.NewSet()), |
| 141 |
) |
| 142 |
assert.Equal(t, alice, c.InstrumentationAttributes(), |
| 143 |
"Empty attribute set should not affect existing ones.") |
| 144 |
}) |
| 145 |
|
| 146 |
t.Run("MixedAttributesAndSet", func(t *testing.T) { |
| 147 |
c := log.NewLoggerConfig( |
| 148 |
log.WithInstrumentationAttributes(aliceAttr), |
| 149 |
log.WithInstrumentationAttributeSet(attribute.NewSet(bobAttr, adminAttr)), |
| 150 |
) |
| 151 |
assert.Equal(t, bobAdmin, c.InstrumentationAttributes(), |
| 152 |
"Attributes and attribute sets should be merged together.") |
| 153 |
}) |
| 154 |
} |
| 155 |
|
| 156 |
func BenchmarkNewLoggerConfig(b *testing.B) { |
| 157 |
for _, bb := range []struct { |
| 158 |
name string |
| 159 |
options []log.LoggerOption |
| 160 |
}{ |
| 161 |
{ |
| 162 |
name: "with no options", |
| 163 |
}, |
| 164 |
{ |
| 165 |
name: "with an instrumentation version", |
| 166 |
options: []log.LoggerOption{ |
| 167 |
log.WithInstrumentationVersion("testing version"), |
| 168 |
}, |
| 169 |
}, |
| 170 |
{ |
| 171 |
name: "with a schema url", |
| 172 |
options: []log.LoggerOption{ |
| 173 |
log.WithSchemaURL("testing URL"), |
| 174 |
}, |
| 175 |
}, |
| 176 |
{ |
| 177 |
name: "with instrumentation attribute", |
| 178 |
options: []log.LoggerOption{ |
| 179 |
log.WithInstrumentationAttributes(attribute.String("foo", "value")), |
| 180 |
}, |
| 181 |
}, |
| 182 |
{ |
| 183 |
name: "with instrumentation attribute set", |
| 184 |
options: []log.LoggerOption{ |
| 185 |
log.WithInstrumentationAttributeSet(attribute.NewSet(attribute.String("bar", "value"))), |
| 186 |
}, |
| 187 |
}, |
| 188 |
} { |
| 189 |
b.Run(bb.name, func(b *testing.B) { |
| 190 |
b.ReportAllocs() |
| 191 |
b.ResetTimer() |
| 192 |
|
| 193 |
for b.Loop() { |
| 194 |
log.NewLoggerConfig(bb.options...) |
| 195 |
} |
| 196 |
}) |
| 197 |
} |
| 198 |
} |